Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^5: Near-free function currying in Perl

by stvn (Monsignor)
on Nov 17, 2004 at 18:14 UTC ( #408506=note: print w/replies, xml ) Need Help??


in reply to Re^4: Near-free function currying in Perl
in thread Near-free function currying in Perl

First, and most important, it requires a programmer to make a design-time decision to support currying. Currying hits its stride only when it is pervasive, and I don't want to limit its use to the functions that have been made "curry ready" ahead of time by having been anointed with the right prototypes of annotations.

I agree somewhat with this point. I think that pervasive currying (as you put it) is very useful only in some cases, but can wreak uncontrolled havoc in others (okay maybe not that bad). For instance, your technique would not work with OO, since the $self value cannot be known when you curry your function. And while most people don't think of currying and OO as going together, nothing could be further from the truth. IMO it can be used a very clean type of closure where the $self instance can not only carry data, but behavior as well. I use this technique myself.

I also take issue with the idea that there is something wrong with design-time descisions to support currying. Sure, in some cases you will want to curry a non-designed-for-currying function, but a designed-for-currying-function has the potential to be far superior. I would even argue (somewhat weakly) that a good functional programmer always designs for currying.

-stvn

Replies are listed 'Best First'.
Re^6: Near-free function currying in Perl
by tmoertel (Chaplain) on Nov 17, 2004 at 22:20 UTC
    stvn, thanks for your comments. Let me address this one, in particular:
    For instance, your technique would not work with OO, ...
    Why not? For example, here I use currying to pre-bind a method to a particular object instance:
    #!/usr/bin/perl package MyObj; use warnings; use strict; use AutoCurry ':all'; sub new { my $self = shift; my $name = shift; bless [$name, { @_ }], $self; } sub speak_keyvals { my ($name, $hash) = @{shift()}; print "$name sez: @{[ @$hash{@_} ]}", $/; } my $foo = new MyObj( "foo", a => "alpha", b => "beta" ); $foo->speak_keyvals( "a" ); # foo sez: alpha # for the next line, recall that $o->f(...) === f($o, ...) my $foo_speaker = $foo->speak_keyvals_c(); # binds to $foo $foo_speaker->("b"); # foo sez: beta $foo_speaker->(@$_) for ["a"], ["b"], ["a", "b"]; # foo sez: alpha # foo sez: beta # foo sez: alpha beta
    Also, regarding this:
    I also take issue with the idea that there is something wrong with design-time decisions to support currying.
    To clarify, I didn't say that there was anything wrong with design-time consideration of currying. What I said was that it was a bad idea to require design-time considerations in order to make a currying scheme work. With such a requirement, the vast existing wealth of CPAN is placed beyond the reach of currying.

    I don't want my ability to use currying to be dependent upon somebody else having designed it into the code I'm using. I want to be able to take old, existing code and curry it, as is.

    Cheers,
    Tom

      Why not? For example, here I use currying to pre-bind a method to a particular object instance:

      I stand corrected. Very nice example.

      To clarify, I didn't say that there was anything wrong with design-time consideration of currying. What I said was that it was a bad idea to require design-time considerations in order to make a currying scheme work. With such a requirement, the vast existing wealth of CPAN is placed beyond the reach of currying.

      I don't want my ability to use currying to be dependent upon somebody else having designed it into the code I'm using. I want to be able to take old, existing code and curry it, as is.

      I agree that it can be nice to not have to require design time considerations for currying to work in many cases. Although I think saying that the all of CPAN is now your curried oyster is maybe a little presumptious. For instance, any function which makes use of caller may or may not work properly, and stack traces will quite possibly get very messy. There is a lot of crazy code on CPAN (and not just TheDamians) and I would guess that some of it would not respond well to currying.

      I guess my point really is that currying is something which is not to be used lightly. It is, by its very nature, an advanced technique which is not easily accessible or understandable to most programmers who were trained in procedural or OO programming. A well crafted Haskell or Standard ML function will be built with the understanding, that it will almost certainly be curried at some point. Old perl code will almost certainly not be built with the same considerations. That is not to say that you shouldn't still use currying in those situtations, but I think it ill advised to imply that currying can breath new life into old code.

      -stvn
        I guess my point really is that currying is something which is not to be used lightly. It is, by its very nature, an advanced technique which is not easily accessible or understandable to most programmers who were trained in procedural or OO programming.

        I'm sorry, I have to disagree.

        I don't think that currying's a particularly advanced technique, or a particularly difficult one to grok. I do think that currying in particular, and "functional programming techniques" in general, have gotten a reputation for being difficult to understand, and that programmers approach them expecting difficulty... and creating that difficulty if they don't find it.

        One thing that really threw me when I was learning Haskell was the fact that most partial expressions are perfectly valid... they just return functions that (given the right arguments) complete the expression. That's not an example of currying being difficult, although currying is the mechanism I'm talking about: it's an example of Haskell doing something that most languages do not (that is, transparently currying everything). Currying is just a matter of caching an argument.

        A well crafted Haskell or Standard ML function will be built with the understanding, that it will almost certainly be curried at some point. Old perl code will almost certainly not be built with the same considerations. That is not to say that you shouldn't still use currying in those situtations, but I think it ill advised to imply that currying can breath new life into old code.

        I don't understand. None of the curry functions I've seen on PM does anything more than wrap the curried function in a closure that remembers what the curried argument was. I guess functions that make heavy use of caller or other recondite corners of Perl's introspection might break when curried, but that's about it. I can't think of any "plain old subroutine" convention that would break when curried. I think that most existing code would survive currying quite happily, though I'm willing to speculate that not everything would.

        --
        Yours in pedantry,
        F o x t r o t U n i f o r m

        "Anything you put in comments is not tested and easily goes out of date." -- tye

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2020-12-03 08:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (53 votes). Check out past polls.

    Notices?