1 # Copyright (c) 1997-2009 Graham Barr <gbarr@pobox.com>. All rights reserved.
2 # This program is free software; you can redistribute it and/or
3 # modify it under the same terms as Perl itself.
5 # Maintained since 2013 by Paul Evans <leonerd@leonerd.org.uk>
12 our @ISA = qw(Exporter);
14 all any first min max minstr maxstr none notall product reduce sum sum0 shuffle
15 pairmap pairgrep pairfirst pairs pairkeys pairvalues
17 our $VERSION = "1.41";
18 our $XS_VERSION = $VERSION;
19 $VERSION = eval $VERSION;
22 XSLoader::load('List::Util', $XS_VERSION);
28 # (RT88848) Touch the caller's $a and $b, to avoid the warning of
29 # Name "main::a" used only once: possible typo" warning
31 ${"${pkg}::a"} = ${"${pkg}::a"};
32 ${"${pkg}::b"} = ${"${pkg}::b"};
34 goto &Exporter::import;
37 # For objects returned by pairs()
38 sub List::Util::_Pair::key { shift->[0] }
39 sub List::Util::_Pair::value { shift->[1] }
47 List::Util - A selection of general-utility list subroutines
51 use List::Util qw(first max maxstr min minstr reduce shuffle sum);
55 C<List::Util> contains a selection of subroutines that people have expressed
56 would be nice to have in the perl core, but the usage would not really be high
57 enough to warrant the use of a keyword, and the size so small such that being
58 individual extensions would be wasteful.
60 By default C<List::Util> does not export any subroutines.
64 =head1 LIST-REDUCTION FUNCTIONS
66 The following set of functions all reduce a list down to a single value.
70 =head2 $result = reduce { BLOCK } @list
72 Reduces C<@list> by calling C<BLOCK> in a scalar context multiple times,
73 setting C<$a> and C<$b> each time. The first call will be with C<$a> and C<$b>
74 set to the first two elements of the list, subsequent calls will be done by
75 setting C<$a> to the result of the previous call and C<$b> to the next element
78 Returns the result of the last call to the C<BLOCK>. If C<@list> is empty then
79 C<undef> is returned. If C<@list> only contains one element then that element
80 is returned and C<BLOCK> is not executed.
82 The following examples all demonstrate how C<reduce> could be used to implement
83 the other list-reduction functions in this module. (They are not in fact
84 implemented like this, but instead in a more efficient manner in individual C
87 $foo = reduce { defined($a) ? $a :
88 $code->(local $_ = $b) ? $b :
89 undef } undef, @list # first
91 $foo = reduce { $a > $b ? $a : $b } 1..10 # max
92 $foo = reduce { $a gt $b ? $a : $b } 'A'..'Z' # maxstr
93 $foo = reduce { $a < $b ? $a : $b } 1..10 # min
94 $foo = reduce { $a lt $b ? $a : $b } 'aa'..'zz' # minstr
95 $foo = reduce { $a + $b } 1 .. 10 # sum
96 $foo = reduce { $a . $b } @bar # concat
98 $foo = reduce { $a || $code->(local $_ = $b) } 0, @bar # any
99 $foo = reduce { $a && $code->(local $_ = $b) } 1, @bar # all
100 $foo = reduce { $a && !$code->(local $_ = $b) } 1, @bar # none
101 $foo = reduce { $a || !$code->(local $_ = $b) } 0, @bar # notall
102 # Note that these implementations do not fully short-circuit
104 If your algorithm requires that C<reduce> produce an identity value, then make
105 sure that you always pass that identity value as the first argument to prevent
106 C<undef> being returned
108 $foo = reduce { $a + $b } 0, @values; # sum with 0 identity value
110 The remaining list-reduction functions are all specialisations of this generic
115 my $bool = any { BLOCK } @list;
117 I<Since version 1.33.>
119 Similar to C<grep> in that it evaluates C<BLOCK> setting C<$_> to each element
120 of C<@list> in turn. C<any> returns true if any element makes the C<BLOCK>
121 return a true value. If C<BLOCK> never returns true or C<@list> was empty then
124 Many cases of using C<grep> in a conditional can be written using C<any>
125 instead, as it can short-circuit after the first true result.
127 if( any { length > 10 } @strings ) {
128 # at least one string has more than 10 characters
133 my $bool = all { BLOCK } @list;
135 I<Since version 1.33.>
137 Similar to L</any>, except that it requires all elements of the C<@list> to
138 make the C<BLOCK> return true. If any element returns false, then it returns
139 false. If the C<BLOCK> never returns false or the C<@list> was empty then it
146 my $bool = none { BLOCK } @list;
148 my $bool = notall { BLOCK } @list;
150 I<Since version 1.33.>
152 Similar to L</any> and L</all>, but with the return sense inverted. C<none>
153 returns true only if no value in the C<@list> causes the C<BLOCK> to return
154 true, and C<notall> returns true only if not all of the values do.
158 my $val = first { BLOCK } @list;
160 Similar to C<grep> in that it evaluates C<BLOCK> setting C<$_> to each element
161 of C<@list> in turn. C<first> returns the first element where the result from
162 C<BLOCK> is a true value. If C<BLOCK> never returns true or C<@list> was empty
163 then C<undef> is returned.
165 $foo = first { defined($_) } @list # first defined value in @list
166 $foo = first { $_ > $value } @list # first value in @list which
167 # is greater than $value
173 Returns the entry in the list with the highest numerical value. If the list is
174 empty then C<undef> is returned.
176 $foo = max 1..10 # 10
177 $foo = max 3,9,12 # 12
178 $foo = max @bar, @baz # whatever
182 my $str = maxstr @list;
184 Similar to L</max>, but treats all the entries in the list as strings and
185 returns the highest string as defined by the C<gt> operator. If the list is
186 empty then C<undef> is returned.
188 $foo = maxstr 'A'..'Z' # 'Z'
189 $foo = maxstr "hello","world" # "world"
190 $foo = maxstr @bar, @baz # whatever
196 Similar to L</max> but returns the entry in the list with the lowest numerical
197 value. If the list is empty then C<undef> is returned.
200 $foo = min 3,9,12 # 3
201 $foo = min @bar, @baz # whatever
205 my $str = minstr @list;
207 Similar to L</min>, but treats all the entries in the list as strings and
208 returns the lowest string as defined by the C<lt> operator. If the list is
209 empty then C<undef> is returned.
211 $foo = minstr 'A'..'Z' # 'A'
212 $foo = minstr "hello","world" # "hello"
213 $foo = minstr @bar, @baz # whatever
217 my $num = product @list;
219 I<Since version 1.35.>
221 Returns the numerical product of all the elements in C<@list>. If C<@list> is
222 empty then C<1> is returned.
224 $foo = product 1..10 # 3628800
225 $foo = product 3,9,12 # 324
229 my $num_or_undef = sum @list;
231 Returns the numerical sum of all the elements in C<@list>. For backwards
232 compatibility, if C<@list> is empty then C<undef> is returned.
234 $foo = sum 1..10 # 55
235 $foo = sum 3,9,12 # 24
236 $foo = sum @bar, @baz # whatever
240 my $num = sum0 @list;
242 I<Since version 1.26.>
244 Similar to L</sum>, except this returns 0 when given an empty list, rather
249 =head1 KEY/VALUE PAIR LIST FUNCTIONS
251 The following set of functions, all inspired by L<List::Pairwise>, consume an
252 even-sized list of pairs. The pairs may be key/value associations from a hash,
253 or just a list of values. The functions will all preserve the original ordering
254 of the pairs, and will not be confused by multiple pairs having the same "key"
255 value - nor even do they require that the first of each pair be a plain string.
261 my @kvlist = pairgrep { BLOCK } @kvlist;
263 my $count = pairgrep { BLOCK } @kvlist;
265 I<Since version 1.29.>
267 Similar to perl's C<grep> keyword, but interprets the given list as an
268 even-sized list of pairs. It invokes the C<BLOCK> multiple times, in scalar
269 context, with C<$a> and C<$b> set to successive pairs of values from the
272 Returns an even-sized list of those pairs for which the C<BLOCK> returned true
273 in list context, or the count of the B<number of pairs> in scalar context.
274 (Note, therefore, in scalar context that it returns a number half the size of
275 the count of items it would have returned in list context).
277 @subset = pairgrep { $a =~ m/^[[:upper:]]+$/ } @kvlist
279 As with C<grep> aliasing C<$_> to list elements, C<pairgrep> aliases C<$a> and
280 C<$b> to elements of the given list. Any modifications of it by the code block
281 will be visible to the caller.
285 my ( $key, $val ) = pairfirst { BLOCK } @kvlist;
287 my $found = pairfirst { BLOCK } @kvlist;
289 I<Since version 1.30.>
291 Similar to the L</first> function, but interprets the given list as an
292 even-sized list of pairs. It invokes the C<BLOCK> multiple times, in scalar
293 context, with C<$a> and C<$b> set to successive pairs of values from the
296 Returns the first pair of values from the list for which the C<BLOCK> returned
297 true in list context, or an empty list of no such pair was found. In scalar
298 context it returns a simple boolean value, rather than either the key or the
301 ( $key, $value ) = pairfirst { $a =~ m/^[[:upper:]]+$/ } @kvlist
303 As with C<grep> aliasing C<$_> to list elements, C<pairfirst> aliases C<$a> and
304 C<$b> to elements of the given list. Any modifications of it by the code block
305 will be visible to the caller.
309 my @list = pairmap { BLOCK } @kvlist;
311 my $count = pairmap { BLOCK } @kvlist;
313 I<Since version 1.29.>
315 Similar to perl's C<map> keyword, but interprets the given list as an
316 even-sized list of pairs. It invokes the C<BLOCK> multiple times, in list
317 context, with C<$a> and C<$b> set to successive pairs of values from the
320 Returns the concatenation of all the values returned by the C<BLOCK> in list
321 context, or the count of the number of items that would have been returned in
324 @result = pairmap { "The key $a has value $b" } @kvlist
326 As with C<map> aliasing C<$_> to list elements, C<pairmap> aliases C<$a> and
327 C<$b> to elements of the given list. Any modifications of it by the code block
328 will be visible to the caller.
330 See L</KNOWN BUGS> for a known-bug with C<pairmap>, and a workaround.
334 my @pairs = pairs @kvlist;
336 I<Since version 1.29.>
338 A convenient shortcut to operating on even-sized lists of pairs, this function
339 returns a list of ARRAY references, each containing two items from the given
340 list. It is a more efficient version of
342 @pairs = pairmap { [ $a, $b ] } @kvlist
344 It is most convenient to use in a C<foreach> loop, for example:
346 foreach my $pair ( pairs @KVLIST ) {
347 my ( $key, $value ) = @$pair;
351 Since version C<1.39> these ARRAY references are blessed objects, recognising
352 the two methods C<key> and C<value>. The following code is equivalent:
354 foreach my $pair ( pairs @KVLIST ) {
355 my $key = $pair->key;
356 my $value = $pair->value;
362 my @keys = pairkeys @kvlist;
364 I<Since version 1.29.>
366 A convenient shortcut to operating on even-sized lists of pairs, this function
367 returns a list of the the first values of each of the pairs in the given list.
368 It is a more efficient version of
370 @keys = pairmap { $a } @kvlist
374 my @values = pairvalues @kvlist;
376 I<Since version 1.29.>
378 A convenient shortcut to operating on even-sized lists of pairs, this function
379 returns a list of the the second values of each of the pairs in the given list.
380 It is a more efficient version of
382 @values = pairmap { $b } @kvlist
386 =head1 OTHER FUNCTIONS
392 my @values = shuffle @values;
394 Returns the values of the input in a random order
396 @cards = shuffle 0..51 # 0..51 in a random order
404 L<https://rt.cpan.org/Ticket/Display.html?id=95409>
406 If the block of code given to L</pairmap> contains lexical variables that are
407 captured by a returned closure, and the closure is executed after the block
408 has been re-used for the next iteration, these lexicals will not see the
409 correct values. For example:
412 my $var = "$a is $b";
413 sub { print "$var\n" };
414 } one => 1, two => 2, three => 3;
418 Will incorrectly print
424 This is due to the performance optimisation of using C<MULTICALL> for the code
425 block, which means that fresh SVs do not get allocated for each call to the
426 block. Instead, the same SV is re-assigned for each iteration, and all the
427 closures will share the value seen on the final iteration.
429 To work around this bug, surround the code with a second set of braces. This
430 creates an inner block that defeats the C<MULTICALL> logic, and does get fresh
431 SVs allocated each time:
435 my $var = "$a is $b";
436 sub { print "$var\n"; }
438 } one => 1, two => 2, three => 3;
440 This bug only affects closures that are generated by the block but used
441 afterwards. Lexical variables that are only used during the lifetime of the
442 block's execution will take their individual values for each invocation, as
445 =head1 SUGGESTED ADDITIONS
447 The following are additions that have been requested, but I have been reluctant
448 to add due to them being very simple to implement in perl
450 # How many elements are true
452 sub true { scalar grep { $_ } @_ }
454 # How many elements are false
456 sub false { scalar grep { !$_ } @_ }
460 L<Scalar::Util>, L<List::MoreUtils>
464 Copyright (c) 1997-2007 Graham Barr <gbarr@pobox.com>. All rights reserved.
465 This program is free software; you can redistribute it and/or
466 modify it under the same terms as Perl itself.
468 Recent additions and current maintenance by
469 Paul Evans, <leonerd@leonerd.org.uk>.