Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by BrowserUk (Pope)
on Mar 13, 2014 at 08:36 UTC ( #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.


Comment on Re: How to understand chapter 6 of Higher Order Perl?
Select or Download Code
Re^2: How to understand chapter 6 of Higher Order Perl?
by McA (Curate) 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.
      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.
      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-09-02 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (21 votes), past polls