cats.labs.state
The State Monad.
eval-state
(eval-state state seed)
Given a State instance, execute the wrapped computation and return the resultant value, ignoring the state. Equivalent to taking the first value of the pair instance returned by run-state
function.
exec-state
(exec-state state seed)
Given a State instance, execute the wrapped computation and return the resultant state. Equivalent to taking the second value of the pair instance returned by run-state
function.
MonadState
protocol
A specific case of Monad abstraction for work with state in pure functional way.
members
put-state
(put-state newstate)
Return a State instance with computation that replaces the current state with specified new state.
run-state
(run-state state seed)
Given a State instance, execute the wrapped computation and returns a cats.data.Pair instance with result and new state.
(def computation (mlet [x (get-state) y (put-state (inc x))] (return y)))
(def initial-state 1) (run-state computation initial-state)
This should be return something to: #<Pair [1 2]>
state
(state f)
The State type constructor.
The purpose of State type is wrap a simple function that fullfill the state signature.
It exists just for avoid extend the clojure function type because is very generic type.
swap-state
(swap-state f)
Return a State instance with computation that applies the specified function to state and returns the old state.