Monad.Writer
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.
Summary↑
bind(writer, fun) | Callback implementation of |
return(value) | Callback implementation of |
runWriter(writer) | Returns the value and log from a writer monad |
writer(value) | Wraps |
writer(value, log) | Wraps |
Types ↑
Functions
Specs:
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"]}
Specs:
- 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}
Specs:
Returns the value and log from a writer monad.
iex> w = writer 42, "The answer"
iex> runWriter w
{42, "The answer"}
Specs:
- writer(term) :: t
Wraps value
into a writer monad.
iex> writer 42
%Monad.Writer{value: 42, log: :nil_monoid}
Specs:
Wraps value
and log
into a writer monad.
iex> writer 42, "The answer"
%Monad.Writer{value: 42, log: "The answer"}