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

Represents the output type of the monad in typespecs

Callbacks

Adds a new piece of output to the output list

Returns an initial output value

Types

output :: any

Represents the output type of the monad in typespecs.

Callbacks

combine(output, output)

Specs

combine(output, output) :: output

Adds a new piece of output to the output list.

initial()

Specs

initial :: output

Returns an initial output value.