"Adam P. Jenkins" <thorin@theshire.com> writes:
In all other languages I've used that have closures, such as Scheme,
ML, Haskell, OCaml, and even Javascript, a closure is just an object
like any other object, that happens to have a special literal syntax
for creating them.
[...]
The way ruby does it with blocks/yield has several disadvantages:
1) There's an arbitrary limit of 1 on the number of blocks you can
pass to a function. If you want to pass more than 1, you need to
convert all but the last block to a proc.
The languages you mentioned above all have this ``arbitrary limit.''
Consider O'Caml. You can only give one block to the `fun' keyword.
Or JavaScript. Certainly, the `function' keyword only takes one
argument list and one code block.
In fact, these languages have the additional ``arbitrary limit'' that
code blocks cannot be passed to user-defined methods.
What I mean is that Ruby allows this,
lambda { |a, b, c| ... }
as well as this
moomin { |a, b, c| ... }
and this ---
snufkin foo, bar do |a, b, c| ... end
Heck, Ruby 1.9 even allows this shorthand for lambdas,
{ |a, b, c| ... }
but that's beside the point. The point is that JavaScript only
allows this:
function (a, b, c) { ... }
That is to say, you cannot define `moomin' in a way that makes this
valid JavaScript code:
moomin (a, b, c) { ... }
The same goes for all the other languages. (Well, except for Scheme,
which really actually lets you define arbitrary syntactic forms, but
we all know that Lisp is inherently superior to everything else, so I
will just pretend you didn't mention it.)
Clearly, when it comes to blocks, Ruby's syntax is far more general
than that of other mortal (i.e., non-Lisp) languages.
While a JavaScript programmer would go like this,
array.sort(function (a, b) { ... })
a Ruby programmer could go like this ---
array.sort &lambda { |a, b| ... }
that would be the literal translation. But Ruby takes another step;
it lets you give code blocks directly to _any_ method,
array.sort { |a, b| ... }
not just `lambda'. This is _better_ than most languages can do.
···
--
Daniel Brockman <daniel@brockman.se>