Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: (almost) foldl

by ikegami (Patriarch)
on Jun 07, 2011 at 22:50 UTC ( [id://908594]=note: print w/replies, xml ) Need Help??


in reply to (almost) foldl

One can simplify your implementation

sub { ( map { splice @_, 0, 2, $_[0] + ($_[1] // 0) } @_ )[-1] }->(0, +@list)

such that one is left with a single splice and without the extra loop pass that necessitates // 0.

sub { ( splice(@_, 0, 0, 0), ( map $_[0] += $_, @_) )[-1] }->(@list)

Of course, that's really just the same as

sub { splice(@_, 0, 0, 0); map $_[0] += $_, @_; $_[0] }->(@list)

At which point, you a can give a better name to the variables you do create (despite claims to the contrary).

sub { my $acc = 0; map $acc += $_, @_; $acc }->(@list)

Finally, canonise.

sub { my $acc = 0; $acc += $_ for @_; $acc }->(@list)

Replies are listed 'Best First'.
Re^2: (almost) foldl
by dk (Chaplain) on Jun 08, 2011 at 04:55 UTC
    and no extra vars ))))
      Are you saying that creating vars named $_[0] (like you did) is better than creating a var named $acc, or did you just not read my post?
        $_[0] is not created, $acc is, hence it is an extra. The rules say "no extra vars", so your post is missing a point. Which means also that your sarcasm of me allegedly not reading your post is undeserved - I did read it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-19 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found