Class: Simulation

Inherits:
Object
  • Object
show all
Extended by:
Monad, PRNG
Defined in:
lib/do_notation/monads/simulations.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PRNG

next_state

Methods included from Monad

bind_const, compose, run

Constructor Details

#initialize(&b) ⇒ Simulation

Returns a new instance of Simulation.



17
18
19
# File 'lib/do_notation/monads/simulations.rb', line 17

def initialize(&b)
  @f = b
end

Instance Attribute Details

#fObject (readonly)

Returns the value of attribute f.



15
16
17
# File 'lib/do_notation/monads/simulations.rb', line 15

def f
  @f
end

Class Method Details

.bind(simulation, &b) ⇒ Object



31
32
33
34
35
36
# File 'lib/do_notation/monads/simulations.rb', line 31

def self.bind(simulation, &b)
  self.new do |s|
    x, s = simulation.f.call(s)
    b.call(x).f.call(s)
  end
end

.rand(n) ⇒ Object



25
26
27
28
29
# File 'lib/do_notation/monads/simulations.rb', line 25

def self.rand(n)
  self.new do |s|
    [s.abs % n, next_state(s)]
  end
end

.unit(x) ⇒ Object



21
22
23
# File 'lib/do_notation/monads/simulations.rb', line 21

def self.unit(x)
  new { |s| [x, s] }
end

Instance Method Details

#play(s = 12345) ⇒ Object



38
39
40
# File 'lib/do_notation/monads/simulations.rb', line 38

def play(s = 12345)
  @f.call(s).first
end