View Source Funx.Monoid protocol (funx v0.1.0)
A protocol defining the Monoid algebraic structure, which consists of an identity element and an associative binary operation for combining values.
This protocol provides four key functions:
Summary
Functions
Combines two monoid structs.
Returns the identity element for the given monoid struct.
Extracts the underlying value from the monoid struct.
Wraps a value into the given monoid struct.
Types
@type t() :: term()
All the types that implement this protocol.
Functions
Combines two monoid structs.
The operation must satisfy associativity:
append(append(a, b), c) == append(a, append(b, c))
Examples
iex> Funx.Monoid.append(%Funx.Monoid.Sum{value: 1}, %Funx.Monoid.Sum{value: 2})
%Funx.Monoid.Sum{value: 3}
Returns the identity element for the given monoid struct.
The identity element is a special value that satisfies the property:
append(empty(monoid_struct), x) == x
append(x, empty(monoid_struct)) == x
Examples
iex> Funx.Monoid.empty(%Funx.Monoid.Sum{})
%Funx.Monoid.Sum{value: 0}
Extracts the underlying value from the monoid struct.
Examples
iex> Funx.Monoid.unwrap(%Funx.Monoid.Sum{value: 10})
10
Wraps a value into the given monoid struct.
Examples
iex> Funx.Monoid.wrap(%Funx.Monoid.Sum{}, 10)
%Funx.Monoid.Sum{value: 10}