Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Data.Monad

Generic operations derived from the Applicative and Monad classes.

Like Data.Foldable, these are constrained free functions written once against the class methods (pure, ap, bind) and shared by every instance (List, Option, …). Side effects ride Prism’s effect system; this is structural sequencing over the container shape, not do-notation.

Functions and Values

join

join : forall a b. (a(a(b))) -> a(b) given Monad(a)

Collapse one level of nesting: m(m(a)) to m(a).

join([[1, 2], [3, 4]])
[1, 2, 3, 4]

map2

map2 : forall a b c d. ((a, b) -> c, d(a), d(b)) -> d(c) given Applicative(d)

Lift a binary function over two wrapped values (Applicative).

map2(\(x, y) -> x + y, Some(1), Some(2))
Some(3)

map3

map3 : forall a b c d e. ((a, b, c) -> d, e(a), e(b), e(c)) -> e(d) given Applicative(e)

Lift a ternary function over three wrapped values (Applicative).

map3(\(x, y, z) -> x + y + z, Some(1), Some(2), Some(3))
Some(6)

sequence

sequence : forall a b. (List(a(b))) -> a(List(b)) given Applicative(a)

Evaluate a list of wrapped values left to right, collecting the results (Applicative). Option short-circuits on None; List takes the cartesian product.

sequence([Some(1), Some(2), Some(3)])
Some([1, 2, 3])

traverse_list

traverse_list : forall a b c. ((a) -> b(c), List(a)) -> b(List(c)) given Applicative(b)

Apply f to each element and sequence the wrapped results (Applicative). This is sequence after a plain map.

traverse_list(\(x) -> if x > 0 then Some(x) else None, [1, 2, 3])
Some([1, 2, 3])