class Mon::Monad::Pending

Pending represents a Lazy value with pending operations. Unwrapping or finalizing with trigger said pending operations.

Public Class Methods

eventually(fun, args) 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 170
def self::eventually fun, args
  Pending.new(lambda { fun.call(*args.map { |v| (v.is_a? Lazy) ? v.unwrap : v }) })
end

Protected Class Methods

new(fun, target = nil) click to toggle source
# File lib/monads/lazy.rb, line 132
def initialize(fun, target = nil)
  case fun.arity
  when 1 then @fun = lambda { fun.call(target.unwrap) }
  when 0 then @fun = fun
  else raise ArgumentError.new("Bad function passed to #{ self }")
  end
end

Public Instance Methods

==(o) click to toggle source
# File lib/monads/lazy.rb, line 191
def == o
  eql? o
end
_() click to toggle source

Alias for unwrap

# File lib/monads/lazy.rb, line 162
def _
  unwrap
end
_canBind?(name) click to toggle source
# File lib/monads/lazy.rb, line 145
def _canBind?(name)
  true
end
bind(&fun) click to toggle source

Add an operation to be performed on the wrapped value, only if necessary

# File lib/monads/lazy.rb, line 141
def bind(&fun)
  Pending::eventually(fun, [self])
end
eql?(o) click to toggle source
# File lib/monads/lazy.rb, line 178
def eql? o
  # Time to collapse
  if o.is_a? Lazy
    self.unwrap == o.unwrap
  else
    self.unwrap == o
  end
end
equals?(o) click to toggle source
# File lib/monads/lazy.rb, line 187
def equals? o
  eql? o
end
finalize() click to toggle source

Complete any pending operations and return the result, wrapped in a Final. Eg: Lazy(10).bind { |i| i * i }.finalize # ==> Final[100]

# File lib/monads/lazy.rb, line 151
def finalize
  Final[unwrap]
end
to_s() click to toggle source
# File lib/monads/lazy.rb, line 174
def to_s
  "Pending[#{ @fun }]"
end
unwrap() click to toggle source

Complete any pending operations and return the result, unwrapped. Eg: Lazy(10).bind { |i| i * i }.unwrap # ==> 100

# File lib/monads/lazy.rb, line 157
def unwrap
  @fun.call
end