call | -> | update |
Use a Proc as an observable.
CREDIT: Tim Pease |
Operator for Proc#compose and Integer#times_collect/of.
a = lambda { |x| x + 4 } b = lambda { |y| y / 2 } (a * b).call(4) #=> 6 (b * a).call(4) #=> 4
CREDIT: Dave
Bind a Proc to an object returning a Method.
NOTE: This version comes from Rails. The old Facets
version used thread.rb, but I no longer think the implementaiton is thread critical. Please make a bug report if this proves wrong.
Returns a new proc that is the functional composition of two procs, in order.
a = lambda { |x| x + 4 } b = lambda { |y| y / 2 } a.compose(b).call(4) #=> 6 b.compose(a).call(4) #=> 4
CREDIT: Dave
Convert a Proc object into new partial Proc object.
a = proc { |a,b,c| a+b+c } b = a.partial(X,2,X) b[1,3] #=> 6 a = proc { |a,b,c| a+b+c } b = a.partial(__,2,__) b[1,3] #=> 6
This method is similar to Proc#curry.
CREDT Trans
Convert Proc to method.
plusproc = lambda { |x| x + 1 } plusproc.to_method(self, 'foo') X.new.foo(1) #=> 2
Translates a Proc into an OpenObject. By droping an OpenObject into the Proc, the resulting assignments incured as the procedure is evaluated produce the OpenObject. This technique is simlar to that of MethodProbe.
p = lambda { |x| x.word = "Hello" } o = p.to_openobject o.word #=> "Hello"
NOTE The Proc must have an arity of one —no more and no less.