NAME "List::UtilsBy" - higher-order list utility functions SYNOPSIS use List::UtilsBy qw( nsortby minby ); use File::stat qw( stat ); my @files_by_age = nsortby { stat($_)->mtime } @files; my $shortest_name = minby { length } @names; DESCRIPTION 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 "sortby" function sorts them according to the string value returned by the extra function, when given each value. my @names_sorted = sort @names; my @people_sorted = sortby { $_->name } @people; FUNCTIONS @vals = sortby { KEYFUNC } @vals 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 sortby { $_->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. @vals = nsortby { KEYFUNC } @vals Equivalent to "sortby" but compares its key values numerically. $optimal = maxby { KEYFUNC } @vals Returns the (first) value from @vals that gives the numerically largest result from the key function. my $tallest = maxby { $_->height } @people use File::stat qw( stat ); my $newest = maxby { stat($_)->mtime } @files; In the case of a tie, the first value to give the largest result is returned. To obtain the last, reverse the input list. my $longest = maxby { length $_ } reverse @strings; If called on an empty list, "undef" is returned. $optimal = minby { KEYFUNC } @vals Equivalent to "maxby" but returns the first value which gives the numerically smallest result from the key function. @vals = uniqby { KEYFUNC } @vals 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 = uniqby { $_->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 uniqby { $_->colour } reverse @fruit; %parts = partitionby { KEYFUNC } @vals Returns a hash of ARRAY refs, containing all the original values distributed according to the result of the key function block. Each ARRAY ref will contain all the values which returned the same string from the key function, in their original order. my %balls_by_colour = partitionby { $_->colour } @balls; Because the values of 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). @values = zipby { ITEMFUNC } $arr0, $arr1, $arr2,... 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. my @transposition = zipby { [ @_ ] } @matrix; my @names = zipby { "$_[1], $_[0]" } \@firstnames, \@surnames; print zipby { "$_[0] => $_[1]\n" } [ keys %hash ], [ values %hash ]; If some of the arrays are shorter than others, the function will behave as if they had "undef" in the trailing positions. The following two lines are equivalent: zipby { f(@_) } [ 1, 2, 3 ], [ "a", "b" ] f( 1, "a" ), f( 2, "b" ), f( 3, undef ) (A function having this behaviour is sometimes called "zipWith", e.g. in Haskell, but that name would not fit the naming scheme used by this module). TODO * XS implementations These functions are currently all written in pure perl. Some at least, may benefit from having XS implementations to speed up their logic. * List-context "maxby" and "minby" Consider whether "maxby" and "minby" ought to return a list of all the optimal values, in the case of a tie. * Merge into List::Util or List::MoreUtils 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. AUTHOR Paul Evans