in reply to scalarmap - some new perl syntax

This is fun to play with. A few comments: Anyway, I like this. Thanks for posting it.

Replies are listed 'Best First'.
RE (tilly) 2: scalarmap - some new perl syntax
by tilly (Archbishop) on Sep 01, 2000 at 07:29 UTC
    An advantage to $a and $b is that they [utomatically pass strict. (Specifically because of sort.)

    This is also a disadvantage, but oh well.

      That is why I chose $a and $b, because of the sort magic.

      I did consider setting $_ to a 2 element anonymous array, then you'd use it like this.

      scalarmap { $_->[0] + $_->[1] } 0, @z;
      But that isn't as elegant.

      I considered using $a and $_ but that offended me too!

      I see from merlyn's post below that the maker of List::Util::reduce came to the same conclusions...

RE: RE: scalarmap - some new perl syntax
by ncw (Friar) on Sep 01, 2000 at 11:48 UTC
    You are quite right about the join thing! I just wanted an example with a string and I didn't intend it to be a full blown join replacement!

    See my reply to tilly for $a and $b.

    I agree with you about having the initial value first, but I couldn't get it to work. Trying to prototype the subroutine as ($&@) meant that I couldn't call it for some reason.

    If I try this :-

    sub scalarmap ($&@) { local ($a, $b) = ( shift ); my $code = shift; foreach $b (@_) { $a = &$code; } return $a; } my @array = 1..10; my $sum = scalarmap 0, { $a + $b } @array;
    Then when I run it I get
    Array found where operator expected at scalarmap2.pl line 32, near " +} " (Missing operator before ?) syntax error at scalarmap2.pl line 32, near "} @array"
    Which is puzzling...