Monad.State

The State monad.

The State monad allows for stateful computations while using pure functions. Computations of this kind can be represented by state transformers, i.e. by functions that map an initial state to a result value paired with a final state.

Examples

iex> require Monad.State, as: State
iex> import State
iex> s = State.m do
...>       a <- get
...>       put (a + 1)
...>       return a + 10
...>     end
iex> State.run(2, s)
{12, 3}

Summary

Functions

Callback implementation for Monad.bind/2

Get the state

Modify the state

Set a new state

Inject x into a State monad

Run the State monad m with x as the value of the initial state

Macros

Monad do-notation

Types

state :: any

Functions

bind(s, f)

Specs

bind(state_m, (any -> state_m)) :: state_m

Callback implementation for Monad.bind/2.

get()

Specs

get :: state_m

Get the state.

modify(f)

Specs

modify((state -> state)) :: state_m

Modify the state.

Returns nil.

put(st)

Specs

put(state) :: state_m

Set a new state.

Returns nil.

return(x)

Specs

return(any) :: state_m

Inject x into a State monad.

run(x, m)

Specs

run(any, state_m) :: {any, state}

Run the State monad m with x as the value of the initial state.

Returns a tuple where the first element is the result of the computation and the second element is the final state.

Macros

m(list)

Monad do-notation.

See the Monad module documentation and the Monad.Statemodule documentation