Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

List::Util

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides list utility functions (first, sum, min, max, reduce, etc.)

Perl Equivalent

This module provides Rust-native implementations of functions from List::Util. Since PetaPerl does not support XS, these are implemented directly in Rust.

Block-Based Functions

Functions like first, any, all, none, notall, and reduce in Perl take a block argument. These use the &@ prototype which tells the parser to treat func { block } @list syntax correctly.

Synopsis

use List::Util qw(sum min max first any all reduce uniq);
my $total = sum @numbers;
my $smallest = min @numbers;
my $largest  = max @numbers;
my $found = first { $_ > 10 } @numbers;
my $has_neg = any { $_ < 0 } @numbers;
my $all_pos = all { $_ > 0 } @numbers;
my $product = reduce { $a * $b } @numbers;
my @unique  = uniq @strings;

Functions

all

Returns true if all elements in LIST satisfy the given block. Sets $_ to each element in turn. Short-circuits on the first false result.

See also: any, none, notall

any

Returns true if any element in LIST satisfies the given block. Sets $_ to each element in turn. Short-circuits on the first true result.

See also: all, none, notall, first

first(&@)

Returns the first element in LIST for which the block returns true. Sets $_ to each element in turn. Returns undef if no match is found.

See also: any, none, reduce

Returns the first SIZE elements from LIST. If SIZE is negative, returns all but the last -SIZE elements.

See also: tail

max

Returns the numerically largest element of LIST. Returns undef for an empty list.

See also: min, minstr, maxstr

maxstr

Returns the string-wise maximum of LIST. Uses gt comparison. Returns undef for an empty list.

See also: minstr, max, min

mesh

Returns a flat list of interleaved elements from the given array references. Also available as mesh_longest. Shorter arrays are padded with undef.

See also: mesh_shortest, zip, zip_shortest

mesh_longest

mesh_shortest

Like mesh, but stops at the shortest input array instead of padding shorter arrays with undef.

See also: mesh, zip_shortest

min

Returns the numerically smallest element of LIST. Returns undef for an empty list.

See also: max, minstr, maxstr

minstr

Returns the string-wise minimum of LIST. Uses lt comparison. Returns undef for an empty list.

See also: maxstr, min, max

none

Returns true if no element in LIST satisfies the given block. The logical negation of any. Short-circuits on the first true result.

See also: any, all, notall

notall

Returns true if not all elements in LIST satisfy the given block. The logical negation of all. Short-circuits on the first false result.

See also: all, any, none

pairfirst

Like pairgrep, but returns only the first matching pair (as a 2-element list) and stops searching. Returns an empty list if no pair matches.

See also: pairgrep, pairmap, pairs

pairgrep

Calls BLOCK for each consecutive pair of elements in LIST, with $a set to the key and $b set to the value. Returns the key/value pairs for which the block returns true. In scalar context, returns the count of matching pairs.

See also: pairmap, pairfirst, pairs

pairkeys

Returns a list of the first values (keys) of each pair in LIST. LIST is treated as an even-sized list of key/value pairs.

See also: pairvalues, pairs, unpairs

pairmap

Calls BLOCK for each consecutive pair of elements in LIST, with $a set to the key and $b set to the value. Returns a flat list of all values returned by each block invocation.

See also: pairgrep, pairfirst, pairs

pairs

Returns a list of array references, each containing two items from the given list. It is a convenient shortcut to operating on even-sized lists of key/value pairs.

If the list has an odd number of elements, the final pair will have undef as the value. Each returned array reference is a plain ARRAY ref (not blessed).

See also: unpairs, pairkeys, pairvalues, pairmap, pairgrep, pairfirst

pairvalues

Returns a list of the second values (values) of each pair in LIST. LIST is treated as an even-sized list of key/value pairs.

See also: pairkeys, pairs, unpairs

product

Returns the numeric product of all elements in LIST. Returns undef for an empty list, 1 for a single element.

reduce

Reduces LIST by calling BLOCK in a scalar context, with $a and $b set to successive pairs of values. Returns the final value of $a, or undef for an empty list. For a one-element list, returns that element without calling the block.

See also: reductions, sum, product

reductions

Like reduce, but returns a list of all intermediate accumulator values as well as the final result. The first element is always the first value of the list.

See also: reduce

sample

Returns COUNT random elements from LIST without replacement. Each element can be selected at most once.

If COUNT exceeds the list size, returns the entire list shuffled. Croaks if COUNT is negative.

See also: shuffle

shuffle

Returns the elements of LIST in a random order.

Uses the Fisher-Yates shuffle algorithm for uniform distribution.

See also: sample

sum

Returns the numeric sum of all elements in LIST. Returns undef for an empty list.

See also: sum0, product, reduce

sum0

Like sum, but returns 0 instead of undef for an empty list.

See also: sum

tail

Returns the last SIZE elements from LIST. If SIZE is negative, returns all but the first -SIZE elements.

See also: head

uniq

Filters a list of values to remove subsequent duplicates, as determined by a string comparison. Preserves the order of first occurrence. Also available as uniqstr.

Compares values as strings to determine uniqueness. In scalar context, returns the number of unique elements. undef is treated as a distinct value (not equal to empty string).

See also: uniqnum, uniqint

uniqint

Filters a list of values to remove subsequent duplicates, as determined by an integer comparison. Values are truncated to integers before comparing. Preserves the order of first occurrence.

In scalar context, returns the number of unique elements.

See also: uniq, uniqnum

uniqnum

Filters a list of values to remove subsequent duplicates, as determined by a numeric comparison. Preserves the order of first occurrence.

Compares values numerically. 0 and -0 are considered equal. NaN values are considered equal to each other. In scalar context, returns the number of unique elements.

See also: uniq, uniqint

uniqstr

unpairs

The inverse of pairs. Takes a list of array references and returns a flat list of their elements. Each array ref contributes its first two elements (key and value).

See also: pairs, pairkeys, pairvalues

zip

Returns a list of array references, each containing one element from each input array. Also available as zip_longest. Shorter arrays are padded with undef.

See also: zip_shortest, mesh, mesh_shortest

zip_longest

zip_shortest

Like zip, but stops at the shortest input array instead of padding shorter arrays with undef.

See also: zip, mesh_shortest