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 | |
by ikegami (Patriarch) on Jun 08, 2011 at 07:46 UTC | |
by dk (Chaplain) on Jun 08, 2011 at 15:46 UTC | |
by ikegami (Patriarch) on Jun 14, 2011 at 22:14 UTC |