Sat07Aug

  1. Stabby Lambdas & Curried Functions

    Starting a new project at work in Ruby 1.9, I noticed there is a new syntax for creating lambdas:

    a = ->(a,b) { a**b }

    This is equivalent to the conventional

    b = lambda { |a,b| a**b }

    Thus:

    a[2,5] == b[2,5]

    My opinion is split on the aesthetic appeal of this new syntax, sure it’s brief but it’s also erlang-y. Fortunately it does have a useful feature, i.e. the ability to give default values to block parameter. This is not possible:

    lambda { |a,b=2| a**b }

    This is:

    ->(a,b=2) { a**b }

    Currying, i.e. partial application of functions also appear

    pow = ->(a,b) { a**b }.curry
    => #<Proc:0x00000100b021b8 (lambda)>
    square = pow[2]
    => #<Proc:0x00000100aff2b0 (lambda)>
    square[2]
    => 4
    square[4]
    => 16
    square[16]
    => 256
    

    Proc#call has also been aliased as Proc#=== meaning one can use lambdas in case statements… WUUUUUTTT!

    S