Module: LambdaDriver::Liftable
Constant Summary collapse
- DEFAULT_CONTEXT =
This is a default context-function. default behaivior is compose function with checking g(x) is mzoro
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) lambda{|f, x| mzero_method = x.respond_to?(:mzero?) ? :mzero? : :nil? x.send(mzero_method) ? x : f.call(x) }
Class Method Summary collapse
Instance Method Summary collapse
- #>=(g) ⇒ Object
-
#compose_with_lifting(g) ⇒ Object
Compose self and given function on the context-function.
- #lambda_driver_liftable_context ⇒ Object
-
#lift(g = DEFAULT_CONTEXT) ⇒ Object
Lift this function to the given context-function.
Class Method Details
.included(klass) ⇒ Object
101 102 103 |
# File 'lib/lambda_driver/liftable.rb', line 101 def self.included(klass) klass.send(:alias_method, :<=, :compose_with_lifting) end |
Instance Method Details
#>=(g) ⇒ Object
97 98 99 |
# File 'lib/lambda_driver/liftable.rb', line 97 def >=(g) g.to_proc.lift(lambda_driver_liftable_context) <= self end |
#compose_with_lifting(g) ⇒ Object
Compose self and given function on the context-function. The context-funciton is passed by lift method.
This method returns composed funciton like bellow.
lambda{|args| context(self, g(*args)) }
For example, set context-function that logging the result.
hash = {:a => "foo"}
f = lambda{|x| x.length}
g = lambda{|y| hash[y]}
ctx = lambda{|f,x|
puts "g(x) -> #{x}"
y = f.call(x)
puts "f(g(x)) -> #{y}"
y
}
lifted = f.lift(ctx)
h = lifted.compose_with_lifting g
h.(:a)
#=> g(x) -> foo
#=> f(g(x)) -> 3
#=> 3
if context-function does not given, default behaivior is compose function with checking g(x) is mzoro
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)
51 52 53 54 55 |
# File 'lib/lambda_driver/liftable.rb', line 51 def compose_with_lifting(g) context = lambda_driver_liftable_context lambda{|*args| context.call(self.to_proc, g.to_proc.call(*args)) }.lift(context) end |
#lambda_driver_liftable_context ⇒ Object
93 94 95 |
# File 'lib/lambda_driver/liftable.rb', line 93 def lambda_driver_liftable_context @lambda_driver_liftable_context || DEFAULT_CONTEXT end |
#lift(g = DEFAULT_CONTEXT) ⇒ Object
Lift this function to the given context-function. The lifted fucntion can compose other function with context-fucntion.
The given context-fuction used by compose_with_lifting to compose other fucntion.
The context-funciton should recieve 2 arguments.
- first one is a function that reciver function of `compose_with_lifting` method.
- second arg is a result of g(x)
-- g is a function passed to `compose_with_lifting`
88 89 90 91 |
# File 'lib/lambda_driver/liftable.rb', line 88 def lift(g = DEFAULT_CONTEXT) @lambda_driver_liftable_context = g self end |