Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: Re: Re: Re: Summing the elements of multiple arrays into a new arrayby TheDamian (Vicar) |
on Nov 02, 2001 at 02:30 UTC ( [id://122702]=note: print w/replies, xml ) | Need Help?? |
For my next hope-to-see, that reduce will DWIM with lists
I don't think any of the syntaxes you suggested will be the way to do this, but it might be that reduce will honour the parameter contexts specified by its reduction block, so this would work:
How? Well, the @^a and @^b make the reduction block a curried subroutine, whose parameter list consists of two unflattened arrays. That is: is shorthand for: (modulo the currying (which isn't used here (so forget I mentioned it))). With that parameter list on its reduction block, reduce would expect arrays instead of scalars as its remaining arguments. Note that the square brackets are critical here, since reduce always applies the reduction in scalar context, but we need to get back something that can be treated like an array in the next reduction (i.e. an array reference). That is: the result of the first reduction step has to be a scalar, but that result -- when it becomes the first argument to the reduction block in the second step of the reduction -- has to be an array. Perl 6's auto-intra-conversion of arrays and array refs makes that possible. Having reduce and other list ops honour their block's parameter list would be very cool in other ways too:
In this example the reduction block has two parameters: a scalar and an unflattened array. So it sucks corresponding elements from the argument list (i.e. 0 and @foo), finds the maximum value in @foo, and adds it to the partial sum. The result becomes the first argument of the next reduction step, which then grabs @bar for its second argument, finds @bar's maximum and adds it in. That result becomes the first argument to the third reduction, which grabs @etc, and adds its maximum as well. Having exhausted all its arguments, reduce returns the final reduction result, which turns out to be the sum of all maxima. Pushing the envelope a little, we could also write:
Here, the mapping block is equivalent to: So each mapping step would pull two scalar arguments off the map's argument list. If either is 'moo', the block returns the pair; otherwise it returns an empty list (which is flattened out of existence in the resultant list). The outcome is that the %cow hash is assigned a list of pairs, which become its entries. Where to the scalar keys and values come from? They're flattened out of the %animals hash by the scalar contexts imposed by the two scalar arguments of the reduction block (!) BTW, that map { $test ? $result : () } @data construct is a trick you can use in Perl 5 too. It's the infamous "grepping map" (i.e. a floor wax and a dessert topping). Update: Fixed the missing second colon on the ternary operator in the last example. Thanks blakem!
In Section
Seekers of Perl Wisdom
|
|