Wednesday, July 28, 2010

how to call lambdas without call()

One thing that is rather nifty in javascript is the ability to assign anonymous functions to variables and then simply call them. For example, Protovis has this nifty method for creating a mapping function from the specified domain to the specified range:
var y = pv.Scale.linear(0, 100).range(0, 640);
y(100); // 640
y(0); // 0
y(50); // 320
Neat!! Well, how about Ruby?

Well, I've been reading Metaprogramming Ruby (which is a really fun book so far) and we have lambdas. However, with a lambda, you usually have assign the lambda and then call() the lambda.

It looks like this:
f = lambda {|x| x} # 10

But I wanted this to be more like javascript's syntax, so I did some tinkering. Here's what I came up with: