lib/core/facets/proc/compose.rb in facets-2.0.2 vs lib/core/facets/proc/compose.rb in facets-2.0.3

- old
+ new

@@ -1,43 +1,46 @@ class Proc - # CREDIT Dave - # Returns a new proc that is the functional - # compostion of two procs, in order. + # composition of two procs, in order. # - # a = lambda { |x| x + 4 } - # b = lambda { |y| y / 2 } + # a = lambda { |x| x + 4 } + # b = lambda { |y| y / 2 } # - # a.compose(b).call(4) #=> 6 - # b.compose(a).call(4) #=> 4 + # a.compose(b).call(4) #=> 6 + # b.compose(a).call(4) #=> 4 # + # CREDIT Dave + def compose(g) raise ArgumentError, "arity count mismatch" unless arity == g.arity lambda{ |*a| self[ *g[*a] ] } end # Operator for Proc#compose and Integer#times_collect/of. # - # a = lambda { |x| x + 4 } - # b = lambda { |y| y / 2 } + # a = lambda { |x| x + 4 } + # b = lambda { |y| y / 2 } # - # (a * b).call(4) #=> 6 - # (b * a).call(4) #=> 4 + # (a * b).call(4) #=> 6 + # (b * a).call(4) #=> 4 # + # CREDIT Dave + def *(x) if Integer===x # collect times a = []; x.times{|i| a << call(i)}; a else # compose procs lambda{|*a| self[x[*a]]} end end + # Use a Proc as an observable. + # # CREDIT Tim Pease - # Use a Proc as an observable. alias_method :update, :call end