class Mon::Monad::Lazy
Lazy
is the parent class of Pending
and Final
, the two states a Lazy
monad can be in. Use with: lazyValue = Lazy[5] # Seems pointless so far... lazyCalc = lazyValue.bind { |i| (0..i).map { |n| n.factorial } }.bind { |factlist| factlist.map { |i| i * i }.... # Keep right on going! # We still haven't done any work! puts lazyCalc.unwrap # Time to have a nap...
Or: lazyProc = Lazy.eventually(5)
{ (0..5).map { |i| call_some_remote_service_ondemand(i) } } # Haven't done anything yet… lazyProc.sample.unwrap.map { |v| “A random response: #{ v }” } # Do one of 5 possible service calls</tt>
Public Class Methods
[](obj = nil)
click to toggle source
Wrap a value in Lazy
# File lib/monads/lazy.rb, line 38 def self::[](obj = nil) if obj.is_a? Proc eventually(obj) else Final[obj] end end
eventually(*args, &fun)
click to toggle source
Perform an operation, if necessary: Lazy.eventually { 10 * 10 }
Or: Lazy.eventually(10) { |n| n * 10 }
# File lib/monads/lazy.rb, line 50 def self::eventually(*args, &fun) Pending::eventually(fun, args) end
valid?(v)
click to toggle source
For contracts. Deprecated!
# File lib/monads/lazy.rb, line 55 def self::valid?(v) v.is_a? Mon::Lazy end