Class: Fr::Random

Inherits:
Object show all
Extended by:
Monad
Defined in:
lib/fr/monad/random.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



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

def bind(f, &g)
  lambda do |scale|
    value, scale = f.call(scale)
    g.call(value).call(scale)
  end
end

.eval(computation, scale = 1.0) ⇒ Object



13
14
15
# File 'lib/fr/monad/random.rb', line 13

def eval(computation, scale = 1.0)
  computation.call(scale)[0]
end

.rand(limit = nil) ⇒ Object

Actions



36
37
38
39
40
# File 'lib/fr/monad/random.rb', line 36

def rand(limit = nil)
  lambda do |scale|
    [Kernel.rand(limit), scale]
  end
end

.run(computation, scale = 1.0) ⇒ Object

Evaluators



9
10
11
# File 'lib/fr/monad/random.rb', line 9

def run(computation, scale = 1.0)
  computation.call(scale)
end

.scale(number, zero) ⇒ Object



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

def scale(number, zero)
  # Linearly scale given number towards zero
  lambda do |scale|
    [zero + scale * (number - zero), scale]
  end
end

.unit(value) ⇒ Object

Combinators



20
21
22
23
24
# File 'lib/fr/monad/random.rb', line 20

def unit(value)
  lambda do |scale|
    [value, scale]
  end
end