in reply to Re: Golf: Adding up array elements
in thread Golf: Adding up array elements

eval join"+",@_

Of course, that's not safe if @_ includes a text element with a ";" in it.

But in the spirit of silly shortenings, how about some recursion?

sub sum { @_ && pop(@_) + sum(@_) }

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re^3: Golf: Adding up array elements
by Skeeve (Parson) on Nov 02, 2005 at 18:29 UTC

    Regarding recursion: I also thought about it, but it's longer. Your's can be shortened:

    sub sum {@_&&(pop)+sum(@_)}


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
      And that can be shortened even more.
      sub sum{@_&&(pop)+&sum}

        What a beautiful catch. ++

        On a tangent, as I was looking at that, my thinking got a little meta and I started wondering about it’s mathematical semantics. Its behaviour is wrong, no? Wouldn’t the sum of an empty set be undefined?

        Makeshifts last the longest.