Monad.Writer behaviour
The Writer monad.
Allows saving output values “under the hood”.
To use this you’ll first need to create a writer module with the desired combining semantics.
The writer is lazy, only when you run it will the functions in the monad be evaluated.
Note that the writer monad module needs to be defined outside of the module that uses it due to an Elixir restriction.
Examples
# Outside the module.
defmodule ListWriter do
def initial, do: []
def combine(new, acc), do: acc ++ new
end
# In the module.
alias ListWriter, as: LW
use Monad
w = ListWriter.m do
LW.tell [1]
a <- return 2
LW.tell [2]
return a + 1
end
w.run()
# {3, [1|2]}
Summary
Types
Callbacks
Adds a new piece of output to the output list.