Class: Simulation

Inherits:
Object
  • Object
show all
Extended by:
PRNG
Includes:
Monad
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, included

Constructor Details

#initialize(&b) ⇒ Simulation

Returns a new instance of Simulation.



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

def initialize(&b)
  @f = b
end

Instance Attribute Details

#fObject (readonly)

Returns the value of attribute f.



13
14
15
# File 'lib/do_notation/monads/simulations.rb', line 13

def f
  @f
end

Class Method Details

.rand(n) ⇒ Object



23
24
25
26
27
# File 'lib/do_notation/monads/simulations.rb', line 23

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

.unit(x) ⇒ Object



19
20
21
# File 'lib/do_notation/monads/simulations.rb', line 19

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

Instance Method Details

#bind(&b) ⇒ Object



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

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

#play(s = 12345) ⇒ Object



36
37
38
# File 'lib/do_notation/monads/simulations.rb', line 36

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