in reply to (almost) foldl
First, it's not equivalent since you don't handle the empty list.
my $sum = sub { (map {splice @_, 0, 2, $_[0] + ($_[1] // 0)} @_)[-1] } +->(@list);
should be
my $sum = sub { (map {splice @_, 0, 2, $_[0] + ($_[1] // 0)} @_)[-1] } +->(0, @list);
A general form of this exists as reduce in List::Util. It's is easier to use (since handling one-element lists is easier) and easier to read (since it uses $a and $b instead of $_[0] and $_[1] and uses less extraneous code).
my $sum = reduce { $a + $b } 0, @list;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: (almost) foldl
by dk (Chaplain) on Jun 08, 2011 at 04:54 UTC | |
by Anonymous Monk on Jun 08, 2011 at 06:13 UTC | |
by ikegami (Patriarch) on Jun 08, 2011 at 07:45 UTC | |
by dk (Chaplain) on Jun 08, 2011 at 15:40 UTC | |
by ikegami (Patriarch) on Jun 14, 2011 at 22:37 UTC |
In Section
Obfuscated Code