Class: Simulation
- Inherits:
-
Object
- Object
- Simulation
- Defined in:
- lib/do_notation/monads/simulations.rb
Instance Attribute Summary collapse
-
#f ⇒ Object
readonly
Returns the value of attribute f.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(&b) ⇒ Simulation
constructor
A new instance of Simulation.
- #play(s = 12345) ⇒ Object
Methods included from PRNG
Methods included from Monad
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
#f ⇒ Object (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 |