Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: adding items, but with interim results

by moritz (Cardinal)
on Mar 30, 2013 at 16:27 UTC ( #1026291=note: print w/ replies, xml ) Need Help??


in reply to adding items, but with interim results

I know you probably want a solution in Perl 5, but here is one in Perl 6, because it hapepns to be very short:

$ perl6 -e 'say [\+] 0, 1, 3, 2' 0 1 4 6

Enclosing an infix operator like + with square brackets applies it to a list, as if you had written  0 + 1 + 4 + 6. The [\infix] for also returns the intermediate results.

Mnemonic: the [\ has the shape of a triangle, just like the list of expressions that is generated from it:

0 0 + 1 0 + 1 + 4 0 + 1 + 4 + 6


Comment on Re: adding items, but with interim results
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-09-23 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (242 votes), past polls