cat/instances/monoid

Monoid instances: Unit, Bool (All and Any), List, Option, Tuple, Triple, Function.

Functions

pub fn all_monoid() -> Monoid(Bool)

Monoid instance for Bool with (&&).

Examples

let bool_and_monoid = all_monoid()
True
|> bool_and_monoid.mappend(False)
|> bool_and_monoid.mappend(bool_and_monoid.mempty)
// -> False
pub fn any_monoid() -> Monoid(Bool)

Monoid instance for Bool with (||).

Examples

let bool_or_monoid = any_monoid()
False
|> bool_or_monoid.mappend(False)
|> bool_or_monoid.mappend(bool_or_monoid.mempty)
// -> False
pub fn function_monoid(mono_b: Monoid(a)) -> Monoid(fn(b) -> a)

Monoid instance for functions where the result type must be a Monoid instance.

pub fn list_monoid() -> Monoid(List(a))

Returns the canonical implementation of the monoid type for List.

Examples

let mono_list = list_monoid()
[1, 2]
|> mono_list.mappend([3, 4, 5])
|> mono_list.mappend(mono_list.mempty)
|> mono_list.mappend([6])
// -> [1, 2, 3, 4, 5, 6]
pub fn option_monoid(mono_a: Monoid(a)) -> Monoid(Option(a))

Returns the canonical implementation of the monoid type for Option(a).
We must have a Monoid(a) type instance.

Examples

let mono_string = Monoid(mempty: "", mappend: fn(x: String, y: String) -> String { x <> y })
let mono_maybe = option_monoid(mono_string)

Some("ab")
|> mono_maybe.mappend(Some("cd"))
// -> Some("abcd")
mono_maybe.mappend(Some("abc"), maybe.mempty)
// -> None
pub fn triple_monoid(
  mono_a: Monoid(a),
  mono_b: Monoid(b),
  mono_c: Monoid(c),
) -> Monoid(#(a, b, c))

Returns the canonical implementation of the monoid type for Triple.
We must have a Monoid type instance for a, b, and c.

pub fn tuple_monoid(
  mono_a: Monoid(a),
  mono_b: Monoid(b),
) -> Monoid(#(a, b))

Returns the canonical implementation of the monoid type for Tuple.
We must have a Monoid(a) and a Monoid(b) type instance.

pub fn unit_monoid() -> Monoid(Nil)

Returns the canonical implementation of the monoid type for Nil (unit type).

Examples

let mono_unit = unit_monoid()
mono_unit.mappend(mono_unit.mempty, Nil)
// -> Nil
Search Document