Class: Expectation

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

Returns a new instance of Expectation.



93
94
95
# File 'lib/do_notation/monads/simulations.rb', line 93

def initialize(&b)
  @f = b
end

Instance Attribute Details

#fObject (readonly)

Returns the value of attribute f.



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

def f
  @f
end

Class Method Details

.bind(expectation, &b) ⇒ Object



112
113
114
# File 'lib/do_notation/monads/simulations.rb', line 112

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

.rand(n) ⇒ Object



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

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



101
102
103
# File 'lib/do_notation/monads/simulations.rb', line 101

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

.wrap(&proc) ⇒ Object



97
98
99
# File 'lib/do_notation/monads/simulations.rb', line 97

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

Instance Method Details

#play(x) ⇒ Object



116
117
118
# File 'lib/do_notation/monads/simulations.rb', line 116

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