Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: (almost) foldl

by dk (Chaplain)
on Jun 08, 2011 at 04:56 UTC ( [id://908617]=note: print w/replies, xml ) Need Help??


in reply to Re: (almost) foldl
in thread (almost) foldl

negative numbers?

Replies are listed 'Best First'.
Re^3: (almost) foldl (-map)
by tye (Sage) on Jun 08, 2011 at 19:45 UTC
    my @list = ( -4 .. 6 ); print map((1)x$_,@list) - map((1)x-$_,@list);

    - tye        

      Wow. That's a exactly (and I mean, exactly) the first idea I had to solve the negative numbers problem. I didn't post it, though, because it doesn't handle decimal numbers.
Re^3: (almost) foldl
by Grimy (Pilgrim) on Jun 08, 2011 at 10:07 UTC
    sub sum { eval 'pop(@_)+' x ($#_ + 1) . '0'; } @list = (-4, 15, -5, 2.5); print sum @list; # 8.5, as expected print sum -4, 15, -5, 2.5; # Works the same print sum; # 0 (not undef)
    EDIT: replaced $#list with $#_ , so that it works with lists of any length. Silly me.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found