This module provides a number of list utility functions, all of which take an initial code block to control their behaviour. They are variations on similar core perl or List::Util functions of similar names, but which use the block to control their behaviour. For example, the core Perl function sort takes a list of values and returns them, sorted into order by their string value. The sort_by function sorts them according to the string value returned by the extra function, when given each value.

Returns the list of values sorted according to the string values returned by the KEYFUNC block or function. A typical use of this may be to sort objects according to the string value of some accessor, such as

sort_by { $_->name } @people

The key function is called in scalar context, being passed each value in turn as both $_ and the only argument in the parameters, @_. The values are then sorted according to string comparisons on the values returned.

This is equivalent to

sort { $a->name cmp $b->name } @people

except that it guarantees the name accessor will be executed only once per value.

One interesting use-case is to sort strings which may have numbers embedded in them "naturally", rather than lexically.

sort_by { s/(\d+)/sprintf "%09d", $1/eg; $_ } @strings

This sorts strings by generating sort keys which zero-pad the embedded numbers to some level (9 digits in this case), helping to ensure the lexical sort puts them in the correct order.

In scalar context, the first maximal value is returned. In list context, a list of all the maximal values is returned. This may be used to obtain positions other than the first, if order is significant.

If called on an empty list, an empty list is returned.

For symmetry with the nsort_by function, this is also provided under the name nmax_by since it behaves numerically.

Returns a list of the subset of values for which the key function block returns unique values. The first value yielding a particular key is chosen, subsequent values are rejected.

my @some_fruit = uniq_by { $_->colour } @fruit;

To select instead the last value per key, reverse the input list. If the order of the results is significant, don't forget to reverse the result as well:

my @some_fruit = reverse uniq_by { $_->colour } reverse @fruit;

Because the values returned by the key function are used as hash keys, they ought to either be strings, or at least well-behaved as strings (such as numbers, or object references which overload stringification in a suitable manner).

Returns a key/value list of ARRAY refs containing all the original values distributed according to the result of the key function block. Each value will be an ARRAY ref containing all the values which returned the string from the key function, in their original order.

my %balls_by_colour = partition_by { $_->colour } @balls;

Because the values returned by the key function are used as hash keys, they ought to either be strings, or at least well-behaved as strings (such as numbers, or object references which overload stringification in a suitable manner).

Returns a key/value list of integers, giving the number of times the key function block returned the key, for each value in the list.

my %count_of_balls = count_by { $_->colour } @balls;

Because the values returned by the key function are used as hash keys, they ought to either be strings, or at least well-behaved as strings (such as numbers, or object references which overload stringification in a suitable manner).

Returns a list of each of the values returned by the function block, when invoked with values from across each each of the given ARRAY references. Each value in the returned list will be the result of the function having been invoked with arguments at that position, from across each of the arrays given.

Returns a list of ARRAY references containing the values returned by the function block, when invoked for each of the values given in the input list. Each of the returned ARRAY references will contain the values returned at that corresponding position by the function block. That is, the first returned ARRAY reference will contain all the values returned in the first position by the function block, the second will contain all the values from the second position, and so on.

my ( $firstnames, $lastnames ) = unzip_by { m/^(.*?) (.*)$/ } @names;

If the function returns lists of differing lengths, the result will be padded with undef in the missing elements.

This function is an inverse of zip_by, if given a corresponding inverse function.

Removes elements from the referenced array on which the selection function returns true, and returns a list containing those elements. This function is similar to grep, except that it modifies the referenced array to remove the selected values from it, leaving only the unselected ones.

A hybrid between extract_by and List::Util::first. Removes the first element from the referenced array on which the selection function returns true, returning it.

As with extract_by, this function requires a real array and not just a list, and is also implemented using splice() so that weak references are not disturbed.

If this function fails to find a matching element, it will return an empty list in list context. This allows a caller to distinguish the case between no matching element, and the first matching element being undef.

Returns the list of values shuffled into a random order. The randomisation is not uniform, but weighted by the value returned by the WEIGHTFUNC. The probabilty of each item being returned first will be distributed with the distribution of the weights, and so on recursively for the remaining items.

This module shouldn't really exist. The functions should instead be part of one of the existing modules that already contain many list utility functions. Having Yet Another List Utilty Module just worsens the problem.

I have attempted to contact the authors of both of the above modules, to no avail; therefore I decided it best to write and release this code here anyway so that it is at least on CPAN. Once there, we can then see how best to merge it into an existing module.

Updated 2015/07/16: As I am now the maintainer of List::Util, some amount of merging/copying should be possible. However, given the latter's key position in the core perl distribution and head of the "CPAN River" I am keen not to do this wholesale, but a selected pick of what seems best, by a popular consensus.