Andrew Wilson wrote:
I'm sure I'm missing something fairly fundamental, but could someone
shed more light on the example:
# reduce list three-at-a-time
$sum_of_powers = reduce { $^partial_sum + $^x ** $^y } 0, xs_and_ys;
specifically what is being iterated over, what gets bound and what does
it return?
See below.
I thought I understood this, the reduce keyword imples (to me anyway)
that it's going to take a binary operator and map it over a list so that
it reduces it to a single value.
Not quite. In Perl 6, Creduce takes an N-argument closure, maps it over the
list (N elements at a time) so that it reduces it to a single value.
The example looks like it's going to produce a list of values. Is that right?
No. Creduce *always* produces a single scalar result (unless you
hyperoperate it, of course ;-)
my xs_and_ys = [1, 2, 3, 4, 5, 6, 7, 8];
does that give
$sum_of_powers = 3; # (0, 1, 2) (3, 4, 5) (6, 7, 8)
$sum_of_powers = 4; # (0, 1, 2) (1, 3, 4) (82, 5, 6) (15707, 7, 8)
or
$sum_of_powers = 5780508; (0, 1, 2) (1, 3, 4) (82, 5, 6) (15707, 7, 8)
The latter.
I get the impression it's supposed to be that last one, but can't figure
out how it's supposed to work.
Like this:
1. Determine how many parameters the closure (i.e. first arg) takes.
Call that number N.
2. Grab the first N elements from the list being reduced.
3. Call the closure in a scalar context with the N arguments.
4. Cache the scalar result.
5. If there are no more elements in the list being reduced, return the
cached result.
6. Otherwise, grab the next N-1 arguments from the list being reduced
(padding with Cundef's if necessary).
7. Prepend the cached result, to create a list of N arguments
8. Go to 3.
Also was reduce defined anywhere i.e. is it a built in or a subroutine?
It's a built-in.
Damian