MonadEx v1.1.3 Monad.Writer View Source

The writer monad keeps track of a calculation and a “log”.

The log can be anything that conforms to the Monoid protocol.

It’s often useful to combine the writer monad with others. For example, you can use a Monad.Maybe as the value of the writer monad. This offers the benefit of having a log of a writer monad and the control flow of a maybe monad.

Link to this section Summary

Functions

Callback implementation of Monad.Behaviour.bind/2

Callback implementation of Monad.Behaviour.return/1

Returns the value and log from a writer monad

Wraps value into a writer monad

Wraps value and log into a writer monad

Link to this section Types

Link to this section Functions

Link to this function bind(writer, fun) View Source
bind(t(), (term() -> t())) :: t()

Callback implementation of Monad.Behaviour.bind/2.

Unwraps the value from writer and applies it to fun. The log from writer and from the resulting writer monad are combined.

iex> m = writer 4, ["Four"]
iex> n = bind m, (& writer &1 * 2, ["Doubled"])
iex> runWriter n
{8, ["Four", "Doubled"]}
Link to this function return(value) View Source
return(term()) :: t()

Callback implementation of Monad.Behaviour.return/1.

Wraps value into a writer monad.

iex> return 42
%Monad.Writer{value: 42, log: :nil_monoid}
Link to this function runWriter(writer) View Source
runWriter(t()) :: {term(), Monoid.t()}

Returns the value and log from a writer monad.

iex> w = writer 42, "The answer"
iex> runWriter w
{42, "The answer"}
Link to this function writer(value) View Source
writer(term()) :: t()

Wraps value into a writer monad.

iex> writer 42
%Monad.Writer{value: 42, log: :nil_monoid}
Link to this function writer(value, log) View Source
writer(term(), Monoid.t()) :: t()

Wraps value and log into a writer monad.

iex> writer 42, "The answer"
%Monad.Writer{value: 42, log: "The answer"}