reduce#
Collapse a list to a single value by repeated pairwise combination.
Σύνοψη#
my $result = reduce { BLOCK } @list;
my $sum = reduce { $a + $b } 1..10; # 55
my $max = reduce { $a > $b ? $a : $b } @xs;
The block is called in scalar context with $a set to the running accumulator and $b set to the next element. On the first call, $a and $b are the first two elements; on each subsequent call, $a is whatever the previous call returned.
Τι επιστρέφεται#
The value of the last block invocation. If the list is empty, undef is returned. If the list contains exactly one element, that element is returned and the block is not called.
Παραδείγματα#
my $concat = reduce { "$a,$b" } 'a'..'c'; # 'a,b,c'
my $best = reduce { $a->score > $b->score ? $a : $b } @players;
my $total = reduce { $a + length $b } 0, @strings; # sum of lengths
## Guarantee a numeric identity so an empty @values doesn't yield undef:
my $sum = reduce { $a + $b } 0, @values;
Διαφορές από το upstream#
Fully compatible with upstream.
Δείτε επίσης#
reductions- likereduce, but also keeps the intermediate values.sum/product/min/max- specialised, faster forms.first/any/all- element-test reductions over$_.