```{index} single: reduce; List::Util function ``` ```{index} single: List::Util::reduce; Perl function ``` # reduce Collapse a list to a single value by repeated pairwise combination. ## Synopsis ```perl 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. ## What you get back 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. ## Examples ```perl 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 ``` ```perl ## Guarantee a numeric identity so an empty @values doesn't yield undef: my $sum = reduce { $a + $b } 0, @values; ``` ## Differences from upstream Fully compatible with upstream. ## See also - `reductions` — like `reduce`, but also keeps the intermediate values. - `sum` / `product` / `min` / `max` — specialised, faster forms. - `first` / `any` / `all` — element-test reductions over `$_`.