How to use the reduce function from List::Util in Perl?
Question
How to use the reduce function from List::Util in Perl?
The reduce function from Perl's List::Util module provides a powerful way to process lists by successively combining their elements. It takes a code block and a list, then applies the code block iteratively to aggregate the list into a single scalar value. This function is particularly useful for computations like sums, products, finding minimums or maximums, or any custom folding operation over a list.
Understanding reduce Usage
At its core, reduce applies a block of code that receives two arguments each iteration: the running accumulator and the current element from the list. The returned value from the block becomes the new accumulator for the next iteration. After processing all elements, the accumulated result is returned.
Here is the general form:
use List::Util 'reduce';
my $result = reduce {
# $_[0] is accumulator, $_[1] is current element
# return next accumulator
} @list;
Note: By default, for each iteration:
$_[0]is the accumulated value so far.$_[1]is the next item from the list.
You can name these using my ($acc, $val) = @_; inside the block for clarity.
Example: Sum of a List
The most common example is summing array elements:
use strict;
use warnings;
use List::Util 'reduce';
my @numbers = (1, 2, 3, 4, 5);
my $sum = reduce { $a + $b } @numbers; # $a and $b are aliases for $_[0], $_[1]
print "Sum: $sum\n";
This code adds pairs of numbers repeatedly until one sum remains. Here $a and $b are more readable aliases (introduced by List::Util), and the block equivalent to { $_[0] + $_[1] }.
Detailed Example: Finding the Longest String
reduce is very flexible. Imagine finding the longest string from a list:
use strict;
use warnings;
use List::Util 'reduce';
my @words = qw(apple banana peanut mango);
my $longest = reduce {
length($a) > length($b) ? $a : $b
} @words;
print "Longest word: $longest\n";
Here, at each step, reduce compares two strings, keeping the longer one as the accumulator.
Important Details & Gotchas
- Importing:
reduceis not imported by default inList::Util. You must ask for it explicitly:use List::Util 'reduce';. - Initial accumulator: Unlike some languages,
reduceuses the first two elements of the list for the initial call; there is no accumulator seed argument. If the list is empty,reducereturnsundef. - Single element list: If the list has only one element, that element is returned immediately.
- Context:
reduceis evaluated in scalar context. - Perl version: Available since Perl 5.8 as part of
List::Util. The$aand$baliases are supported throughout. These aliases behave like$_[0]and$_[1]respectively.
Summary
reduce allows you to implement various list reductions in a concise way by specifying the combining logic inside a single block. It exemplifies Perl's TMTOWTDI ("There's More Than One Way To Do It") philosophy for list processing steps that would otherwise require manual loops.
By combining the simplicity of list iteration with powerful functional programming style, reduce empowers expressive, readable code for aggregation tasks.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 10ms
(empty)(empty)