View Source Funx.Foldable protocol (funx v0.1.0)

The Funx.Foldable protocol defines two core folding operations: fold_l/3 (fold left) and fold_r/3 (fold right).

These functions allow structures to be collapsed into a single value by applying functions in a specific order. Depending on the structure, folding can be done from the left (fold_l/3) or from the right (fold_r/3).

Summary

Types

t()

All the types that implement this protocol.

Functions

Folds the structure from the left, applying func_a if a condition is met, otherwise applying func_b.

Folds the structure from the right, applying func_a if a condition is met, otherwise applying func_b.

Types

@type t() :: term()

All the types that implement this protocol.

Functions

Link to this function

fold_l(structure, func_a, func_b)

View Source

Folds the structure from the left, applying func_a if a condition is met, otherwise applying func_b.

This function collapses a structure by recursively applying the provided functions from the leftmost element to the rightmost.

Parameters:

  • structure: The structure to fold.
  • func_a: The function to apply in case of a matching condition.
  • func_b: The function to apply if the condition is not met.

Examples

iex> Funx.Foldable.fold_l(Funx.Monad.Maybe.just(5), fn x -> x + 1 end, fn -> 0 end)
6

iex> Funx.Foldable.fold_l(Funx.Monad.Maybe.nothing(), fn _ -> 1 end, fn -> 0 end)
0
Link to this function

fold_r(structure, func_a, func_b)

View Source

Folds the structure from the right, applying func_a if a condition is met, otherwise applying func_b.

This function collapses a structure by recursively applying the provided functions from the rightmost element to the leftmost.

Parameters:

  • structure: The structure to fold.
  • func_a: The function to apply in case of a matching condition.
  • func_b: The function to apply if the condition is not met.

Examples

iex> Funx.Foldable.fold_r(Funx.Monad.Maybe.just(5), fn x -> x + 1 end, fn -> 0 end)
6

iex> Funx.Foldable.fold_r(Funx.Monad.Maybe.nothing(), fn _ -> 1 end, fn -> 0 end)
0