After reading FoxtrotUniform's excellent node on currying, I've been pondering the differences between a closure and currying a function. I want to try to describe what I see as the differences, thereby exposing myself to the critique of the monastery :)
Anyhow. Given this (toy) closure:
sub toggle { my @states = @_; my $index = 0; return sub { return $states[ $index++ % ( scalar @states ) ]; } }
This little snippet takes a list of arguments and returns a function reference. Calling the reference will return the next element in the argument list. Simple use:
my $t = &toggle qw( alfa beta gamma ); print &$t(), "\n" foreach ( 1 .. 20 );
The point is that @states and $index are unique context to each of the function references, and calling &toggle with different argument lists will give us independent function references that have distinct behaviour.
Now, given this function for currying:
sub curry { my ( $fnc, @arguments ) = @_; return sub { return &$fnc( @arguments, @_ ); } }
And this toy function for echoing the contents of a list.
sub echo { return join( " ", @_ ); }
With currying, we can "prime" &echo with a set of arguments that will be prepended to everything it prints out later:
my $boss = &curry( \&echo, "My", "boss:" ); my $friend = &curry( \&echo, "Johnny:" ); print &$boss qw( has pointy hair ), "\n"; # "My boss: has pointy hair print &$friend qw( likes rock and roll ), "\n"; # etc.
Obviously, both the &toggle closure and the &curry function are implementet in the same way, namely Perl's closures. Why the difference in naming? Why not just talk about functions like &curry as closures?
I think it is a good idea to separate these two concepts, as currying includes a function as an argument. This seems to me to be the key, as a closure in itself is a method for giving a function context (A poor man's object, as some say. Others call objects a poor man's closure.) so each closure is explicitly created and tailored to it's use.
Since currying receives its function from the outside as an argument, it does not care about what it does to whom. This makes it to me a more flexible programming technique, since I don't have to explicitly set up the inner workings of a closure. Borrowing from object oriented terminology, maybe currying can be considered as a kind of closure constructor, or factory?
In closing, I want to stress that this is my understanding of these concepts, and that I've been using the last few days to grasp what it means. If any wiser monks would point out the errors in my argumentation, I'd be more than happy.
pernod
--
Mischief. Mayhem. Soap.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Closures versus Currying
by PodMaster (Abbot) on Aug 31, 2004 at 10:00 UTC | |
by pernod (Chaplain) on Aug 31, 2004 at 10:29 UTC | |
|
Re: Closures versus Currying
by grinder (Bishop) on Aug 31, 2004 at 10:33 UTC | |
by FoxtrotUniform (Prior) on Sep 06, 2004 at 08:47 UTC | |
by tilly (Archbishop) on Sep 07, 2004 at 17:04 UTC | |
|
Re: Closures versus Currying
by hardburn (Abbot) on Aug 31, 2004 at 13:03 UTC | |
|
Re: Closures versus Currying
by tilly (Archbishop) on Aug 31, 2004 at 15:40 UTC | |
by Aristotle (Chancellor) on Sep 01, 2004 at 00:18 UTC | |
by adrianh (Chancellor) on Sep 01, 2004 at 10:06 UTC | |
|
Re: Closures versus Currying
by FoxtrotUniform (Prior) on Aug 31, 2004 at 22:00 UTC |