Module: Fr::State
- Extended by:
- Monad
- Defined in:
- lib/fr/monad/state.rb
Class Method Summary
collapse
Methods included from Monad
applyM, composeM, filterM, foldM, forM, join, liftM, mapM, sequence, unlessM, whenM, zipM
Methods included from Functor
#void
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
|
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
|
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
12
13
14
|
# File 'lib/fr/monad/state.rb', line 12
def run(computation, state)
computation.call(state)
end
|
.unit(value) ⇒ Object
27
28
29
30
31
|
# File 'lib/fr/monad/state.rb', line 27
def unit(value)
lambda do |state|
Hash[state: state, value: value]
end
end
|
55
56
57
58
59
|
# File 'lib/fr/monad/state.rb', line 55
def update
lambda do |state|
Hash[state: yield(state), value: nil]
end
end
|