Sha256: 63a5ad2c7d9e49e4cfd9ea54b406773b92670497c62fe244fa541c60e79dccc0
Contents?: true
Size: 785 Bytes
Versions: 20
Compression:
Stored size: 785 Bytes
Contents
class Proc # 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 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 * b).call(4) #=> 6 # (b * a).call(4) #=> 4 # # CREDIT: Dave def *(x) if Integer===x # collect times c = [] x.times{|i| c << call(i)} c else # compose procs lambda{|*a| self[x[*a]]} end end end
Version data entries
20 entries across 19 versions & 2 rubygems