Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: (almost) foldl

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


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
    no extra modules! )
      Are core modules like List::Util "extra" or not?

      Your reply doesn't make sense. I pointed out that someone already wrote your code, and wrote it way better. The choice isn't between using a module and using no module, the choice is between using your code and using List::Util's.

        Well, actually, it's your reply that doesn't make sense. This section is obfuscation, i.e. pure fun, and not asking how to make the code better.

        Your statement that you pointed out that someone wrote the code is not true. Where is it? Is it your code? It's not better, because it needs an extra 0 to the list.

        Also, the topic clearly states "no extra modules". Yes, thank you, I know very well about List::Util, a great module. But you ignored that and answered I question I didn't ask, missing the point altogether.

Log In?
Username:
Password:

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

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

    No recent polls found