MonadEx v1.1.3 Monad.Reader View Source
The reader monad is great for situations when you need to share some environment or state between several operations.
Reader monads are great for dependency injection. The environment is specified separately from the operations.
Example
iex> import Curry
iex> use Monad.Operators
iex> env = %{dev: %{url: "http://www.example.com/dev"}, prod: %{url: "https://www.example.com/prod"}}
iex> r = reader(& env[&1])
...> ~>> (& return(&1[:url]))
...> ~>> (& return(&1 <> "/index.html"))
iex> fun = runReader r
iex> fun.(:dev)
"http://www.example.com/dev/index.html"
iex> fun.(:prod)
"https://www.example.com/prod/index.html"
iex> reader = curry(& &1 + &2) <|> reader(& &1 * 2) <~> reader(& &1 * &1)
iex> fun = runReader reader
iex> fun.(2)
8
iex> fun.(-12)
120
Link to this section Summary
Functions
Callback implementation of Monad.Behaviour.bind/2
Wraps fun
in a reader monad
Callback implementation of Monad.Behaviour.return/1
Unwraps the function from the reader
Link to this section Types
Link to this section Functions
Callback implementation of Monad.Behaviour.bind/2
.
iex> r = reader &(&1 * 2)
iex> bind r, &(&1 + 3)
iex> f = runReader r
iex> f.(10)
20
Wraps fun
in a reader monad.
iex> r = reader &(&1)
iex> r.fun.(4)
4
Callback implementation of Monad.Behaviour.return/1
.
Converts value
into a function that always returns value
. Then wraps that
function into a reader monad.
iex> r = return 42
iex> r.fun.(:unused)
42
Unwraps the function from the reader.
iex> r = reader &(&1 * 2) iex> f = runReader r iex> f.(3) 6