Class: Mon::Monad::Lazy
- Inherits:
-
Mon::Monad
- Object
- Mon::Monad
- Mon::Monad::Lazy
- Includes:
- ChainableMonad
- Defined in:
- lib/monads/lazy.rb
Overview
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>
Class Method Summary collapse
-
.[](obj = nil) ⇒ Object
Wrap a value in Lazy.
-
.eventually(*args, &fun) ⇒ Object
Perform an operation, if necessary:
Lazy.eventually { 10 * 10 }
Or:Lazy.eventually(10) { |n| n * 10 }
. -
.valid?(v) ⇒ Boolean
For contracts.
Methods included from ChainableMonad
#_, #coerce, #method_missing, #respond_to?
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Mon::Monad::ChainableMonad
Class Method Details
.[](obj = nil) ⇒ Object
Wrap a value in Lazy
38 39 40 41 42 43 44 |
# 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) ⇒ Object
Perform an operation, if necessary: Lazy.eventually { 10 * 10 }
Or: Lazy.eventually(10) { |n| n * 10 }
50 51 52 |
# File 'lib/monads/lazy.rb', line 50 def self::eventually(*args, &fun) Pending::eventually(fun, args) end |
.valid?(v) ⇒ Boolean
For contracts. Deprecated!
55 56 57 |
# File 'lib/monads/lazy.rb', line 55 def self::valid?(v) v.is_a? Mon::Lazy end |