[Wellington-pm] Sum in one line

michael at diaspora.gen.nz michael at diaspora.gen.nz
Mon Feb 28 19:18:34 PST 2005


Finlay Thompson writes:
>Anyway, does anyone know of a good oneliner ? The best I can do is:
>
>   my $total = sub {my $s = 0; $s += $_ for @_; $s }->(@numbers);

I'd try:

    # remove the init
    my $total = sub {my $s; $s += $_ for @_; $s }->(@numbers);
    # remove the sub!
    my $total; $total += $_ for @numbers;

And lastly, for total inefficency, but functional programming like foo:

    my $total = scalar map { (1) x $_ } @numbers;


More information about the Wellington-pm mailing list