I had just started trying to get my head around the idea of the lambda calculus when the author brought up the subject of "currying", and discussed the "Lambda Calculus Way"™ of writing an addition function built on the concept that functions can only take a single argument. The author showed how to express such a thing in Perl:
sub add {
my $x = shift;
my $f = sub {
my $y = shift;
return $x + $y;
}
return $f;
}
Groovy supports writing code like this too, so I thought I'd see what I could come up with in Groovy syntax. Here's the beast:
def add (x) {
return {
y -> return x + y
}
}
And the test...
assert 7 == add(3)(4) // whew.
If there's a Groovy expert out there reading this blog, let me know if there's a better way. All right, back to the essay...
What the hell?
ReplyDelete