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
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"]}
Callback implementation of Monad.Behaviour.return/1
.
Wraps value
into a writer monad.
iex> return 42
%Monad.Writer{value: 42, log: :nil_monoid}
Returns the value and log from a writer monad.
iex> w = writer 42, "The answer"
iex> runWriter w
{42, "The answer"}
Wraps value
into a writer monad.
iex> writer 42
%Monad.Writer{value: 42, log: :nil_monoid}
Wraps value
and log
into a writer monad.
iex> writer 42, "The answer"
%Monad.Writer{value: 42, log: "The answer"}