Module: LambdaDriver::Liftable

Included in:
Method, Proc
Defined in:
lib/lambda_driver/liftable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



32
33
34
# File 'lib/lambda_driver/liftable.rb', line 32

def self.included(klass)
  klass.send(:alias_method, :<=, :compose_with_lifting)
end

Instance Method Details

#>=(g) ⇒ Object



28
29
30
# File 'lib/lambda_driver/liftable.rb', line 28

def >=(g)
  g.to_proc <= self
end

#compose_with_lifting(g) ⇒ Object

compose self and give fuction with checking g(x) is mzero. if g(x) is mzero, it does not call self and return g(x), otherwise returns f(g(x)).

mzero means the object is nil or emtpy

hash = {:a => "foo"}
f = lambda{|y| y.length }
g = lambda{|y| hash[y]}
h = f.compose_with_lifting g
h.(:a) # => 3
h.(:b) # => nil (it does not called f)

This method is aliased as ‘<=`.

f <= g # => f.compose_with_lifting(g)


20
21
22
23
24
25
26
# File 'lib/lambda_driver/liftable.rb', line 20

def compose_with_lifting(g)
  lambda{|*args|
    result = g.to_proc.call(*args)
    mzero_method = result.respond_to?(:mzero?) ? :mzero? : :nil?
    result.send(mzero_method) ? result : self.to_proc.call(result)
  }
end