Introduction

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.

What I'm trying to say

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.


In reply to Closures versus Currying by pernod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.