MonadEx v1.1.0 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

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

Types

t()
t

Functions

bind(writer, fun)
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"]}
return(value)
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}
runWriter(writer)
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"}
writer(value)
writer(term) :: t

Wraps value into a writer monad.

iex> writer 42
%Monad.Writer{value: 42, log: :nil_monoid}
writer(value, log)
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"}