in reply to RE: RE: scalarmap - some new perl syntax
in thread scalarmap - some new perl syntax

You wrote:
> It calls your sub with $a and $b set to the first two > elements of the array which is neater in some ways, > but would make it harder to write the factorial example > above.
How so? Your initial value is, in this case, really just the first element of the array, in the case of reduce (I didn't realize this until looking at reduce, granted). So you'd just make your array 1..$N for a factorial. Like this:
sub factorial { reduce { $a * $b } 1..$_[0] }

Replies are listed 'Best First'.
RE: RE: RE: RE: scalarmap - some new perl syntax
by ncw (Friar) on Sep 01, 2000 at 12:01 UTC
    Yes you are right!

    Having an extra parameter on scalarmap is exactly equivalent to adding this parameter onto the front of the list you pass to reduce.

    EG

    $z = scalarmap { $a * $b } 1, 2..10; # is the same as use List::Util; $z = reduce { $a * $b } 1, 2..10;
    All hail to perl's unfancy list flattening subroutine parameter passing!

    This makes scalarmap completely redundant :-( It was fun inventing it though :-)