Class: Expectation

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) ⇒ Expectation

Returns a new instance of Expectation.



91
92
93
# File 'lib/do_notation/monads/simulations.rb', line 91

def initialize(&b)
  @f = b
end

Instance Attribute Details

#fObject (readonly)

Returns the value of attribute f.



89
90
91
# File 'lib/do_notation/monads/simulations.rb', line 89

def f
  @f
end

Class Method Details

.rand(n) ⇒ Object



103
104
105
106
107
108
# File 'lib/do_notation/monads/simulations.rb', line 103

def self.rand(n)
  wrap do |k|
    sum = (0..n-1).map{|x| k.call(x)}.inject{|s,x| s+x}
    1.0 * sum / n
  end
end

.unit(x) ⇒ Object



99
100
101
# File 'lib/do_notation/monads/simulations.rb', line 99

def self.unit(x)
  wrap{ |f| f.call(x) }
end

.wrap(&proc) ⇒ Object



95
96
97
# File 'lib/do_notation/monads/simulations.rb', line 95

def self.wrap(&proc)
  new(&proc)
end

Instance Method Details

#bind(&b) ⇒ Object



110
111
112
# File 'lib/do_notation/monads/simulations.rb', line 110

def bind(&b)
  self.class.wrap{|k| @f.call(lambda{|x| b.call(x).f.call(k)}) }
end

#play(x) ⇒ Object



114
115
116
# File 'lib/do_notation/monads/simulations.rb', line 114

def play(x)
  @f.call(lambda{|x1| x1 == x ? 1.0 : 0.0})
end