I'm not a huge user of it, but I think you can look at currying a multi-arg function as quite similar to constructing an object from a factory.

In both cases, you don't need to do it. Instead of creating the object, you could pass around all the parameters needed to construct the object everywhere you'd pass the object instead.

Both approaches "bundle" a bunch of information into a single context which can operate on things and be handily passed around.

Another way of looking at is both approaches allow you to make a specific tool (tailored object from factory or curried function) from a general one (factory or multi-arg function).

It's also very useful in 'map' or 'filter' (i.e. grep in perl), like situations, where you want a single-arg function to run the map or filter. e.g.

sub mk_less_than_filter { my $limit = shift; return sub { my $elt = shift; return $elt < $limit; }; } sub find_less_than { my $limit = shift; my $vals = shift; my $filter = mk_less_than_filter($limit); return grep {$filter->($_)} @$vals; } print join(", ", find_less_than(4, [1, 2, 7, 10, 3, 8])), "\n"; # Prints 1, 2, 3
It's still a bit contrived, but I hope you can see the idea. In a more functional language 'grep' is written as 'filter' and would generally take a one-arg function as it's first argument.

In reply to Re: Currying--useful examples? by jbert
in thread Currying--useful examples? by macrobat

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.