Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How to understand chapter 6 of Higher Order Perl?

by BrowserUk (Patriarch)
on Mar 13, 2014 at 08:36 UTC ( [id://1078136]=note: print w/replies, xml ) Need Help??


in reply to How to understand chapter 6 of Higher Order Perl?

It doesn't help that the examples are really boring and kind of obfuscated. For instance: sub promise(&) { $_[0] } That's right, that thing does nothing. I find this really annoying.

It doesn't "do nothing". It constructs a 'promise' from an anonymous block.

That is, it takes an anonymous block of code and returns a code reference to it, so that it can be executed at sometime in the future:

sub promise(&) { $_[0] };; ... $later = promise{ say 'Keeping my' };; ... $later->();; ## or just &$later;; Keeping my

In other words, it constructs a callback.

To create an 'infinite' stream of values starting at 123 and incrementing by 2:

sub countFromBy{ my( $from, $step ) = @_; return promise{ $from += $st +ep } };; ... $counter = countFromBy( 123, 2 );; ... say &$counter for 1 .. 10;; 125 127 129 131 133 135 137 139 141 143

Maybe that clarifies things a little for you?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: How to understand chapter 6 of Higher Order Perl?
by McA (Priest) on Mar 13, 2014 at 08:44 UTC

    May I add somthing?

    The book says that this is "syntactic sugar". So, BrowserUk's example is the same as:

    $later = sub { say 'Keeping my' }; ... $later->();

    McA

Re^2: How to understand chapter 6 of Higher Order Perl?
by Anonymous Monk on Mar 13, 2014 at 09:00 UTC
    Nah, I actually understand what it does (which is nothing useful). I just find it very annoying. That's not the real problem, of course. The problem is that I need some kind of tool or something to track all these function calls. So far I'm doing it manually, and that's kind of difficult when there is stuff like
    my $hamming; $hamming = node(1, promise { merge(scale($hamming, 2), merge(scale($hamming, 3), scale($hamming, 5), )) } );
    I find it kind of unreadable even without promise.
      Nah, I actually understand what it does (which is nothing useful). I just find it very annoying.

      I don't disagree with you.

      That's not the real problem, of course. The problem is that I need some kind of tool or something to track all these function calls. So far I'm doing it manually, and that's kind of difficult when there is stuff like ... I find it kind of unreadable even without promise.

      Again, I don't disagree. I call it obfuscation through over-encapsulation.

      The misguided attempt to gloss over moderately "difficult" perl syntax with myriad small, trivial named subroutines or methods. All it does is obscure the underlying algorithm; complicate maintenance and debugging; and throw performance to the dogs of dogma.

      This is far simpler, clearer and way more efficient.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        This is far simpler, clearer and more efficient
        You're right. And "syntax with myriad small ... subroutines" is not good for me at all. Especially when many of these subroutines tell that they're called __ANON__. I'll do as the fellow Anonimous Monk recommended. Thanks everyone.
      time for a week long break?
        I dunno, maybe this functional thing is simply too difficult to learn for what it does? It's not like I'm going to use it in real world. Perl is already write-only, heh.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-03-19 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found