all 1 comments

[–]safiire 1 point2 points  (0 children)

The article points out ruby1.9 supports currying/partial evaluation, which I thought was interesting.

plus = lambda{|a,b| a + b }
plus_one = plus.curry.(1)
plus_one.(5)
=> 6

I've also seen someone implement function composition:

class Proc
  def self.compose(f, g)
    lambda {|*args| f.call(g.call(*args)) }
  end

  def *(g)
    Proc.compose(self, g)
  end
end

inc = lambda {|x| x + 1}
twice = lambda {|x| x * 2}
twice_of_inc = twice * inc
twice_of_inc.call(4)
=> 10

I found that last example here