Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Near-free function currying in Perl

by fergal (Chaplain)
on Nov 17, 2004 at 12:22 UTC ( #408374=note: print w/replies, xml ) Need Help??


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

I think you've got things reversed. log_to_handle() is not part of the framework, it's a function that Tom already has lying around. The framework does it's logging by running a coderef you provide and passing in the log message as the signle argument.
# deep inside the framework &$logger("frobnication successful");

Tom's problem is that his log_to_handle() function is expecting several arguments including a filehandle and so he can't reuse in the framework. He would need it to be

# deep inside the framework &$logger($LOGHANDLE, "frobnication successful");

Currying allows Tom to take log_to_handle and turn it into a function that already knows what handle to use and only needs a single argument. One way to do that is

my $logger = sub { my $msg = shift; log_to_handle($MYHANDLE, $msg); };
and then pass $logger into the framework. Of course that's a lot more typing than we'd like so with AutoCurry you can just do
my $logger = log_to_handle_c($MYHANDLE);
the _c at the end means that rather than running the log_to_handle function we want a new coderef which will be suitable for the framework.

Replies are listed 'Best First'.
Re^3: Near-free function currying in Perl
by BrowserUk (Pope) on Nov 17, 2004 at 12:31 UTC

    Do you know of a module that takes a callback for logging purposes?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      I can't think of one off hand that uses it for logging but why not?

      You can't just pass a filehandle because then you cannot do interesting things with a message before logging it or maybe decide not to log it at all (unless you tie the filehandle but please don't go there).

      The only other way that has the same flexibility is to pass in a logging object, which has a log() method.

      When it comes down to it, a closure and an object with one method are pretty much equivalent, they both store a state and they both have a behaviour which can be invoked. The difference is that it's a lot easier to construct a closure on the fly whereas an object needs an entire class.

      The Tk framework is full of things that take a callback. Tk programmers have to write lots of closures. I used to do loads of Tk work (back in the 2oth century :-) and looking back, I frequently found myself currying functions although I didn't have a name for it at the time.

      Frameworks in functional languages like lisp and Haskell do this too because it's easy to do for them as currying is built in and is the "natural" way to do this for their programmers.

        (unless you tie the filehandle but please don't go there).

        Why not?

        Frameworks in functional languages like lisp and Haskell do this too because it's easy to do for them as currying is built in and is the "natural" way to do this for their programmers.

        I agree (partially), but tieing is a "natural" way in Perl.

        Lisp and Haskell have to use currying, because FP languages eshew the concept of variables and state. So much so, that they had to invent "functions that you can only run once" (fancifully named 'monads') in order to get around the inconvenient reality that you cannot represent the real world as mathematical functions who's return values are always defined in terms of their inputs.

        Files don't work that way; neither do databases or network connections or users. Perl very much lives in the real world. As LW put it "good chemistry is complicated, and a little bit messy". Perl has constructs for dealing with the real world very efficiently.

        It just strikes me that trying to force-fit the FP function passing style on top of Perl is a kind square peg in a round hole thing.

        Don't get me wrong. I'm really glad that Tom is posting this Haskell inspired stuff. It's helping me in my attempt to get to grips with Haskell. And I wouldn't try and stop anyone else from using it if they see what I don't. I'm just having a hard time trying to envisage the time when I would want to use currying in a Perl program.

        All the examples I find of currying show (the equivalent of) things like

        my $double = sub{ mult( 2, @_ ) }; my $doubled = $double->( 3 ); print $doubled; ## ouputs '6'

        To which my reaction is "Whoo-pee?". And what's wring with print 2 * 3; ?

        Tom at least came up with a vaguely plausible use for it, despite that I can't actually think of any time (with the possible exception of Tk; which I have issues with anyway:) that I might hav eused up till now.

        Maybe the penny will drop for me one day soon. Or maybe I'll just have to confine myself to the backwater of inherently procedural thinkers--although I make full use of map, grep, List::Util::reduce et al. as well as having a growing collection of similarly FP-like functions of my own devizing.

        So I have picked up on some of the concepts and grasped them with both hands. It's just finding a real-world "killer application" for some of the others, like currying, that leave me cold so far.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo
        "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2020-12-03 07:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (52 votes). Check out past polls.

    Notices?