cat/monad
This module contains monad definitions.
Types
Encapsulates a pair whose first component is a value
of arbitrary type a and the second component is a string
.
Used to embellish
the return values of functions.
Examples
// Original function
f = fn(x) {x * 2}
// Embellished function
f = fn(x) {Writer(x * 2, "doubled ")}
pub type Writer(a) {
Writer(a, String)
}
Constructors
-
Writer(a, String)
Functions
pub fn fish(
m1: fn(a) -> Writer(b),
m2: fn(b) -> Writer(c),
) -> fn(a) -> Writer(c)
Composition
for the embellished functions that return the Writer type.
(>=>) :: (a -> Writer b) -> (b -> Writer c) -> (a -> Writer c)
m1 >=> m2 = \x ->
let (y, s1) = m1 x
(z, s2) = m2 y
in (z, s1 ++ s2)
Examples
let up_case = fn(s: String) { Writer(string.uppercase(s), "upCase ") }
let to_words = fn(s: String) { Writer(string.split(s, " "), "toWords ") }
let process = fish(up_case, to_words)
process("Anna has apples")
// -> Writer(["ANNA", "HAS", "APPLES"], "upCase toWords ")