class Proc
def self.compose(f, g)
lambda { |*args| f[g[*args]] }
end
def *(g)
Proc.compose(self, g)
end
end
inc = lambda { |x| x + 1 }
thrice = lambda { |x| 3 * x }
thrice_of_inc = thrice * inc
thrice_of_inc[1]
=> 6
twice_of_dec = lambda { |x| 2 * x } * lambda { |x| x - 1 }
twice_of_dec[3]
=> 4
thrice_of_inc_of_thrice = thrice * inc * thrice
thrice_of_inc_of_thrice[1]
=> 12
It almost feels as if it were meant to be that way. ;-)
I love Ruby.
Cheers,
Tom
Have a nice week end!
You know, there's a lesson in there somewhere. ;-)
Cheers,
Tom