Module: Fr::Writer

Extended by:
Monad
Defined in:
lib/fr/monad/writer.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



32
33
34
35
36
37
38
# File 'lib/fr/monad/writer.rb', line 32

def bind(f, &g)
  lambda do |state, monoid|
    tuple = f.call(state, monoid)
    g.call(tuple[:value])
     .call(tuple[:state], tuple[:monoid])
  end
end

.eval(computation, monoid) ⇒ Object



15
16
17
# File 'lib/fr/monad/writer.rb', line 15

def eval(computation, monoid)
  computation.call(monoid.zero, monoid)[:value]
end

.exec(computation, monoid) ⇒ Object



19
20
21
# File 'lib/fr/monad/writer.rb', line 19

def exec(computation, monoid)
  computation.call(monoid.zero, monoid)[:state]
end

.run(computation, monoid) ⇒ Object

Evaluators



11
12
13
# File 'lib/fr/monad/writer.rb', line 11

def run(computation, monoid)
  computation.call(monoid.zero, monoid)
end

.tell(write) ⇒ Object

Actions



43
44
45
46
47
# File 'lib/fr/monad/writer.rb', line 43

def tell(write)
  lambda do |state, monoid|
    Hash[state: monoid.plus(state, write), monoid: monoid, value: nil]
  end
end

.unit(value) ⇒ Object

Combinators



26
27
28
29
30
# File 'lib/fr/monad/writer.rb', line 26

def unit(value)
  lambda do |state, monoid|
    Hash[state: state, monoid: monoid, value: value]
  end
end