Pond v0.2.0 Pond.Acc

Functions for accumulating state.

State accumulators are useful when combined with Pond.Next for piping while preserving previous invocations state.

For example, piping our hello world example and accumulating its values into a list:

iex> f = pond(:hello, fn
...>   pond, state = :hello ->
...>     {state, pond.(:world)}
...>   pond, state ->
...>     {state, pond.(state)}
...> end)
...>
...> f
...> |> Acc.into(Acc.list())
...> |> next()
...> |> next()
...> |> Acc.value()
[:hello, :world]

Pond.Next can use this module’s functions in order to accumulate state for functions that follow the convention of returning {value, next_fun}.

This module provides some accumulators for common cases. However, if your function return a different structure you can easily use these as reference to build your own.

Accumulators are themselves just ponds. So they can be used independently. For example:

iex> f = Acc.list()
...> f = f.(:hello)
...> f = f.(:world)
...> Acc.value(f)
[:hello, :world]

Link to this section Summary

Functions

Combines a function and an accumulator in a tuple as expected by Pond.Next

Creates an accumulator that stores only the last value given to it

Creates a new list accumulator

Creates an accumulator that reduces state

Creates an accumulator that reduces state starting with an initial value

Extracts the current value from the accumulator

Link to this section Types

Link to this type acc()
acc() :: (term() -> term())
Link to this type acc_and_pond()
acc_and_pond() :: {acc_pond(), pond()}
Link to this type acc_pond()
acc_pond() :: (term() -> acc())
Link to this type pond()
pond() :: (... -> term())
Link to this type reducer()
reducer() :: (term(), term() -> term())

Link to this section Functions

Link to this function into(pond, acc)
into(pond :: pond(), acc :: acc_pond()) :: acc_and_pond()

Combines a function and an accumulator in a tuple as expected by Pond.Next.

iex> f = pond(:hello, fn
...>   pond, state = :hello ->
...>     {state, pond.(:world)}
...>   pond, state ->
...>     {state, pond.(state)}
...> end)
...>
...> assert {acc, ^f} = f |> Acc.into(Acc.list())
...> assert acc == Acc.list()
...> assert is_function(acc, 1)
true

Creates an accumulator that stores only the last value given to it.

iex> f = pond(:hello, fn
...>   pond, state = :hello ->
...>     {state, pond.(:world)}
...>   pond, state ->
...>     {state, pond.(state)}
...> end)
...>
...> f
...> |> Acc.into(Acc.last())
...> |> next()
...> |> next()
...> |> Acc.value()
:world

Creates a new list accumulator.

Extracting value from this accumulator returns a list of all values yield to it.

See this module doc.

Link to this function reduce(reducer)
reduce(reducer()) :: acc_pond()

Creates an accumulator that reduces state

iex> f = pond(:hello, fn
...>   pond, state = :hello ->
...>     {state, pond.(:world)}
...>   pond, state ->
...>     {state, pond.(state)}
...> end)
...>
...> f
...> |> Acc.into(Acc.reduce(&"#{&1} #{&2}"))
...> |> next()
...> |> next()
...> |> Acc.value()
"hello world"
Link to this function reduce(reducer, initial_value)
reduce(reducer(), initial_value :: term()) :: acc_pond()

Creates an accumulator that reduces state starting with an initial value.

iex> f = pond(:hello, fn
...>   pond, state = :hello ->
...>     {state, pond.(:world)}
...>   pond, state ->
...>     {state, pond.(state)}
...> end)
...>
...> f
...> |> Acc.into(Acc.reduce(&"#{&1} #{&2}", "yay"))
...> |> next()
...> |> next()
...> |> Acc.value()
"yay hello world"
Link to this function value(acc)
value(acc() | acc_and_pond()) :: term()

Extracts the current value from the accumulator.

Normally, this will be the last step of a pipe, in order to extract the accumulated state.

See this module doc.