Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Elegantly map fizz to buzz

by raiph (Deacon)
on May 17, 2016 at 05:40 UTC ( [id://1163181]=note: print w/replies, xml ) Need Help??


in reply to Re: Elegantly map fizz to buzz
in thread Elegantly map fizz to buzz

With two simplifications:
say [+] grep * %% (5|3), 1..10

Simplification #1. The code $_ %% 5 or $_ %% 3 can use a Junction instead: $_ %% (5|3). Junctions are designed mostly with the performance boost from automatic parallelism in mind but they can also aid readability.

Simplification #2. The compiler recognizes when a Whatever pronoun is used as an argument of an operator. For almost all operators it makes that operation its own block, replacing the Whatever with It ($_). So these are equivalent:

say [+] grep * %% (3|5), 1..10; say [+] grep { $_ %% (3|5) }, 1..10;

Replies are listed 'Best First'.
Re^3: Elegantly map fizz to buzz
by Laurent_R (Canon) on May 17, 2016 at 14:55 UTC
    Thank you, raiph, these are good ideas.

    Using a junction is indeed a nice idea, and does improve readability, but I doubt there can be a performance boost in this specific case, because I can't see how the compiler or the VM could run this in parallel (or if it does, it would need to add a duplicate removal phase, it seems to me that this might be too much for a compiler optimization).

    As for the * whatever pronoun, I should confess that I have been trying to use it from time to time, but am still uneasy with its syntax. Essentially, I haven't really figured out how the compiler can tell the difference between * as the multiply sign and * as the whatever pronoun, so that I have been using it so far with a try and error method, rather than with a true understanding of where and how this should work.

      Rakudo does not yet attempt parallelization of any Junctions. Even if/when it one day does, overhead will sometimes dominate, especially when trying to parallelize really tiny computations. I'm curious what you meant by "duplicate removal phase" and would enjoy reading an elaboration of that.


      Here are the two rules which drive how the grammar distinguishes a Whatever * from a multiplication *:

      1. If a token appears where an operator is allowed and there's a corresponding operator definition then it's that operator.

        For example, an operator is allowed between values, and an infix:<+> definition is in scope in ordinary code (here's the built in Numeric role's contribution to that definition), so the token + in 2 + 3 is treated as invoking infix:<+>. The same thing applies for other infix operators including multiplication with Infix:<*>.

      2. If an asterisk is NOT treated as an operator and appears where a value is allowed then it's a Whatever.

        For example, 2 + 3 includes the two values 2 and 3 and there is no prefix:<*> so one could write * + * to express "Whatever plus Whatever".

        Thanks Raiph for your explanations about the whatever. It still looks very magical to me.
        I'm curious what you meant by "duplicate removal phase" and would enjoy reading an elaboration of that.
        If you grep multiples of 5 and multiples of 5 separately (in parallel, assuming Rakudo would parallelize junctions), you would end up with multiples of 15 on both streams, so you would need to remove those duplicate before proceeding to the summation.
      I have been using it so far with a try and error method, rather than with a true understanding of where and how this should work.

      That was my experience of much of the syntax; albeit a long time since.

      A language designed for computers to understand; and humans to be in awe of those few that can dedicate their lives to learning it.

      (It's not unique in that sense; there are plenty of others around that are equally guru only territory.)


      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". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Hi BrowserUk

        I fully understand your point and appreciate it, but I have to disagree. I admit that there are a couple of things that are still obscure to me among those that I have studied, but it is really very few.

        One of the reasons for such difficulties is that the documentation available is still insufficient. The official documentation is rather good (although still a bit incomplete), but sometimes it is a bit terse and you (or I) would need some further explanations, such as what you would find in the Llama and the Camel book for Perl 5, not to speak of the numerous tutorials available for Perl 5. As an example, really understanding Perl 5 references and associated data structures took me some time, but the available articles helped a lot, understanding it from just the official Perl documentation might have been more difficult.

        As a relatively early adopter of Perl 6, I know that it is a bit more complicated for me than it was twelve or thirteen years ago when I picked up Perl 5, because the documentation is much less abundant, but that's OK, I expected that, and I am spending quite a bit of free time trying to help produce better documentation for Perl 6 (well, mostly in French so far, but some in English to come out soon).

        But, really, Perl 6 is implementing the DWIM principle just as Perl 5, it is not difficult, you just have to pass the barrier of the (relatively few) differences between the two versions of the language. It does take a bit of efforts, but no big deal, really.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-03-28 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found