Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^4: Specializing Functions with Currying

by stvn (Monsignor)
on Aug 06, 2004 at 13:46 UTC ( [id://380534]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^5: Specializing Functions with Currying
by Aristotle (Chancellor) on Aug 06, 2004 at 22:55 UTC

    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.

Re^5: Specializing Functions with Currying
by jryan (Vicar) on Aug 06, 2004 at 21:03 UTC

    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 }
      Why recurse when you can iterate instead? :)

      *gasp* .... thats not very functional! (I wont even bring up the fact you are using variable assignment, tssk tssk)

      If we really want to be "efficient" about it, we would avoid the overhead of the wrapper subroutine, and just use straight perl's closures.

      sub compose { my (@funcs) = @_; return sub { my (@args) = @_; foreach my $func (@funcs) { @args = $func->(@args); } return @args; }; }
      But really, that takes all the fun out of it ;-)

      -stvn

        Why be functional when you can be efficient? Recursion might be elegant, but computers don't really understand elegance. :) Functional programming has a lot of great and useful concepts in it, and luckily we use a language that doesn't *force* us to be purists about it.

        P.S.: The variable assignment is very necessary in my second example, because otherwise you're going to get a circularly referenced subroutine, which will make perl hit a infinite recursion warning pretty damned quick.

        P.P.S: And, you're right, your last version is even better!

        For full equivalence you need to not spoil the aliasing of @_ for the first call. Replace all @args with @_ and you're home safe. (The @_ assignment later breaks the aliasing as it should.)

        Cheers,
        ihb

        Read argumentation in its context!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found