Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
No such thing as a small change
 
PerlMonks  

Re: Specializing Functions with Currying

by leriksen (Curate)
on Aug 06, 2004 at 02:53 UTC ( #380440=note: print w/ replies, xml ) Need Help??


in reply to Specializing Functions with Currying

good description of currying - it took me ages to understand this concept (through studies of Scheme, Lisp and Haskell over the years), and it didnt finally twig until I was having to extend some legacy C code here - at one point in the code I knew I would eventually call a key function, but I didn't have all the information in scope right then. I remember thinking 'if only I could have a new function that called that one, using the arguments I have now, and the arguments the new function _will_ have in scope when it gets invoked - I could just pass a function pointer to that new function, instead of chaining all these parameters together and passing long parameter lists around...hey!!'

another good name is 'partial application of functions' - your calling it with a partial list of parameters...

any idea how you would do multiple application of these attributes - e.g. bold _and_ italic

use brain;


Comment on Re: Specializing Functions with Currying
Re^2: Specializing Functions with Currying
by Aristotle (Chancellor) on Aug 06, 2004 at 03:12 UTC

    It's not really doable with the simpleminded curry() used here. You'll have to construct this manually.

    'boldital' => do { my $i = curry \&wrap_with_html => ( 'i' ); my $b = curry \&wrap_with_html => ( 'b' ); sub { $b->( $i->( @_ ) ) } },

    Makeshifts last the longest.

      'boldital' => do { my $i = curry \&wrap_with_html => ( 'i' ); my $b = curry \&wrap_with_html => ( 'b' ); sub { $b->( $i->( @_ ) ) } },

      Neat use of =>. As for composition, as an inveterate Haskell hacker (well, hacker-wannabe), I'd prefer to have that as a primitive, too:

      sub compose { my ($f, $g) = @_; return sub { $f->($g->(@_)); } } # ... 'boldital' => &compose(&curry(\&wrap_with_html, 'i'), &curry(\&wrap_with_html, 'b')),
      The idea of functions that operate on functions and return other functions is much more useful than it first seemed to me. If you never think about functions as first-class objects, having a compose function seems incredibly redundant. Once you start building functions on-the-fly, composition becomes indispensible.

      I guess I should write part 2 of my Perlesque Intro to Haskell, then.

      --
      F o x t r o t U n i f o r m
      Found a typo in this node? /msg me
      % man 3 strfry

        I had seen that some primitive exists for this kind of thing in functional programming, but I have never gotten past a brief foray into that world. My first contact with a number of functional concepts was through Perl, and ever since I've meant to learn a serious functional language. It hasn't happened yet…

        For anyone looking for an introduction to functional concepts in Perl, Mark-Jason Dominus' Higher Order Perl book is well worth a look… or will be once it's finished.

        Makeshifts last the longest.

        Why limit compose to just two functions?

        sub compose { my ($f, $f2, @rest) = @_; return $f unless defined $f2; return &compose(sub { $f2->($f->(@_)) }, @rest); } *bold_italic_and_underlined_paragraph = compose( &curry(\&wrap_with_html, 'u'), &curry(\&wrap_with_html, 'b'), &curry(\&wrap_with_html, 'i'), &curry(\&wrap_with_html, 'p') ); print bold_italic_and_underlined_paragraph("test");

        -stvn
      It's not really doable with the simpleminded curry() used here.
      Perhaps I'm missing something, but what if you did this:
      my %handler = ( "bold_italic" = curry(\&wrap_with_html,"b><i"), );

      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      With all of us waiting, your kingdom will come

        Err, yes, but that wasn't the point. It's a funny hack, but a hack. :-) It doesn't apply to the general case where one wants to compose functions.

        Makeshifts last the longest.

        The closing tag would be incorrect if you just add a slash before the string.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://380440]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-04-20 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls