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:

  • empty/1: Returns the identity element for the given monoid.
  • append/2: Combines two monoid structs.
  • wrap/2: Wraps a value into the monoid struct.
  • unwrap/1: Extracts the underlying value from the monoid struct.

Summary

Types

t()

All the types that implement this protocol.

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

Link to this function

append(monoid_struct_a, monoid_struct_b)

View Source
@spec append(t(), t()) :: t()

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}
@spec empty(t()) :: t()

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}
@spec unwrap(t()) :: any()

Extracts the underlying value from the monoid struct.

Examples

iex> Funx.Monoid.unwrap(%Funx.Monoid.Sum{value: 10})
10
Link to this function

wrap(monoid_struct, value)

View Source
@spec wrap(t(), any()) :: t()

Wraps a value into the given monoid struct.

Examples

iex> Funx.Monoid.wrap(%Funx.Monoid.Sum{}, 10)
%Funx.Monoid.Sum{value: 10}