Module: Fr::State
- Extended by:
- Monad
- Defined in:
- lib/fr/monad/state.rb
Class Method Summary collapse
- .bind(f, &g) ⇒ Object
- .eval(computation, state) ⇒ Object
- .exec(computation, state) ⇒ Object
- .get ⇒ Object
-
.put(n) ⇒ Object
Actions.
-
.run(computation, state) ⇒ Object
Evaluators.
-
.unit(value) ⇒ Object
Combinators.
- .update ⇒ Object
Methods included from Monad
applyM, composeM, filterM, foldM, forM, join, liftM, mapM, sequence, unlessM, whenM, zipM
Methods included from Functor
Class Method Details
.bind(f, &g) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/fr/monad/state.rb', line 33 def bind(f, &g) lambda do |state| tuple = f.call(state) g.call(tuple[:value]).call(tuple[:state]) end end |
.eval(computation, state) ⇒ Object
16 17 18 |
# File 'lib/fr/monad/state.rb', line 16 def eval(computation, state) computation.call(state)[:value] end |
.exec(computation, state) ⇒ Object
20 21 22 |
# File 'lib/fr/monad/state.rb', line 20 def exec(computation, state) computation.call(state)[:state] end |
.get ⇒ Object
49 50 51 52 53 |
# File 'lib/fr/monad/state.rb', line 49 def get lambda do |state| Hash[state: state, value: state] end end |
.put(n) ⇒ Object
Actions
43 44 45 46 47 |
# File 'lib/fr/monad/state.rb', line 43 def put(n) lambda do |state| Hash[state: n, value: nil] end end |
.run(computation, state) ⇒ Object
Evaluators
12 13 14 |
# File 'lib/fr/monad/state.rb', line 12 def run(computation, state) computation.call(state) end |