Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Specializing Functions with Currying

by Aristotle (Chancellor)
on Aug 06, 2004 at 03:12 UTC ( [id://380442]=note: print w/replies, xml ) Need Help??


in reply to Re: Specializing Functions with Currying
in thread Specializing Functions with Currying

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.

Replies are listed 'Best First'.
Re^3: Specializing Functions with Currying
by FoxtrotUniform (Prior) on Aug 06, 2004 at 03:23 UTC
    '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

      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

        Of course, that's just asking for it.

        my $biup = compose map curry( \&wrap_with_html, $_ ), qw( p u i b );

        Now that actually looks like a construct you're likely to see in some flavour of functional programming language.

        Makeshifts last the longest.

        Why recurse when you can iterate instead? :)

        use List::Util qw(reduce); sub compose { reduce { sub { $b->($a->(@_)) } } @_; }

        Of course, that doesn't actually work because Perl's reduce uses global variables ($a,$b). But, we can roll our own:

        sub compose { my $c = shift; foreach my $s (@_) { my $o = $c; $c = sub { $s->($o->(@_)) } } $c }

      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.

Re^3: Specializing Functions with Currying
by thor (Priest) on Aug 09, 2004 at 15:03 UTC
    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.
        You're absolutely correct.

        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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (None)
    As of 2024-04-25 00:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found