• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

dev-bin/H23-Apr-2021-2013

git/H23-Apr-2021-4429

lib/List/H23-Apr-2021-1,60433

t/H23-Apr-2021-422324

xt/H23-Apr-2021-363269

CODE_OF_CONDUCT.mdH A D23-Apr-20213.2 KiB7656

CONTRIBUTING.mdH A D23-Apr-20214.1 KiB11176

ChangesH A D23-Apr-20212.8 KiB11960

INSTALLH A D23-Apr-20212.2 KiB7346

LICENSEH A D23-Apr-20218.8 KiB208154

MANIFESTH A D23-Apr-2021753 4140

META.jsonH A D23-Apr-202137.7 KiB1,1111,109

META.ymlH A D23-Apr-202124.3 KiB827826

Makefile.PLH A D23-Apr-20211.6 KiB6553

README.mdH A D23-Apr-202149.6 KiB1,517984

azure-pipelines.ymlH A D23-Apr-2021668 2924

cpanfileH A D23-Apr-20211.6 KiB5650

dist.iniH A D23-Apr-20211.1 KiB5149

perlcriticrcH A D23-Apr-20211.9 KiB7149

perltidyrcH A D23-Apr-2021301 2322

precious.tomlH A D23-Apr-20211 KiB4036

README.md

1# NAME
2
3List::AllUtils - Combines List::Util, List::SomeUtils and List::UtilsBy in one bite-sized package
4
5# VERSION
6
7version 0.19
8
9# SYNOPSIS
10
11    use List::AllUtils qw( first any );
12
13    # _Everything_ from List::Util, List::SomeUtils, and List::UtilsBy
14    use List::AllUtils qw( :all );
15
16    my @numbers = ( 1, 2, 3, 5, 7 );
17    # or don't import anything
18    return List::AllUtils::first { $_ > 5 } @numbers;
19
20# DESCRIPTION
21
22Are you sick of trying to remember whether a particular helper is defined in
23[List::Util](https://metacpan.org/pod/List%3A%3AUtil), [List::SomeUtils](https://metacpan.org/pod/List%3A%3ASomeUtils) or [List::UtilsBy](https://metacpan.org/pod/List%3A%3AUtilsBy)? I sure am. Now you
24don't have to remember. This module will export all of the functions that
25either of those three modules defines.
26
27Note that all function documentation has been shamelessly copied from
28[List::Util](https://metacpan.org/pod/List%3A%3AUtil), [List::SomeUtils](https://metacpan.org/pod/List%3A%3ASomeUtils) and [List::UtilsBy](https://metacpan.org/pod/List%3A%3AUtilsBy).
29
30## Which One Wins?
31
32Recently, [List::Util](https://metacpan.org/pod/List%3A%3AUtil) has started including some of the subs that used to
33only be in [List::SomeUtils](https://metacpan.org/pod/List%3A%3ASomeUtils). Similarly, [List::SomeUtils](https://metacpan.org/pod/List%3A%3ASomeUtils) has some small
34overlap with [List::UtilsBy](https://metacpan.org/pod/List%3A%3AUtilsBy).
35
36`List::AllUtils` use to always favors the subroutine provided by
37[List::Util](https://metacpan.org/pod/List%3A%3AUtil), [List::SomeUtils](https://metacpan.org/pod/List%3A%3ASomeUtils) or [List::UtilsBy](https://metacpan.org/pod/List%3A%3AUtilsBy) in that order. However,
38as of [List::Util](https://metacpan.org/pod/List%3A%3AUtil) 1.56, it included some functions, `mesh` and `zip` with
39the same name as [List::SomeUtils](https://metacpan.org/pod/List%3A%3ASomeUtils) functions, but different behavior.
40
41So going forward, we will always prefer backwards compatibility. This means
42that `mesh` and `zip` will always come from [List::SomeUtils](https://metacpan.org/pod/List%3A%3ASomeUtils). If other
43incompatible functions are added to [List::Util](https://metacpan.org/pod/List%3A%3AUtil), those will also be skipped
44in favor of the [List::SomeUtils](https://metacpan.org/pod/List%3A%3ASomeUtils) version.
45
46The docs below come from [List::Util](https://metacpan.org/pod/List%3A%3AUtil) 1.56, [List::SomeUtils](https://metacpan.org/pod/List%3A%3ASomeUtils) 0.58, and
47[List::UtilsBy](https://metacpan.org/pod/List%3A%3AUtilsBy) 0.11.
48
49# WHAT IS EXPORTED?
50
51All this module does is load [List::Util](https://metacpan.org/pod/List%3A%3AUtil), [List::SomeUtils](https://metacpan.org/pod/List%3A%3ASomeUtils), and
52[List::UtilsBy](https://metacpan.org/pod/List%3A%3AUtilsBy), and then re-export everything that they provide. That means
53that regardless of the documentation below, you will get any subroutine that
54your installed version provides.
55
56# LIST-REDUCTION FUNCTIONS
57
58The following set of functions all apply a given block of code to a list of
59values.
60
61## reduce
62
63    $result = reduce { BLOCK } @list
64
65Reduces `@list` by calling `BLOCK` in a scalar context multiple times,
66setting `$a` and `$b` each time. The first call will be with `$a` and `$b`
67set to the first two elements of the list, subsequent calls will be done by
68setting `$a` to the result of the previous call and `$b` to the next element
69in the list.
70
71Returns the result of the last call to the `BLOCK`. If `@list` is empty then
72`undef` is returned. If `@list` only contains one element then that element
73is returned and `BLOCK` is not executed.
74
75The following examples all demonstrate how `reduce` could be used to implement
76the other list-reduction functions in this module. (They are not in fact
77implemented like this, but instead in a more efficient manner in individual C
78functions).
79
80    $foo = reduce { defined($a)            ? $a :
81                    $code->(local $_ = $b) ? $b :
82                                             undef } undef, @list # first
83
84    $foo = reduce { $a > $b ? $a : $b } 1..10       # max
85    $foo = reduce { $a gt $b ? $a : $b } 'A'..'Z'   # maxstr
86    $foo = reduce { $a < $b ? $a : $b } 1..10       # min
87    $foo = reduce { $a lt $b ? $a : $b } 'aa'..'zz' # minstr
88    $foo = reduce { $a + $b } 1 .. 10               # sum
89    $foo = reduce { $a . $b } @bar                  # concat
90
91    $foo = reduce { $a || $code->(local $_ = $b) } 0, @bar   # any
92    $foo = reduce { $a && $code->(local $_ = $b) } 1, @bar   # all
93    $foo = reduce { $a && !$code->(local $_ = $b) } 1, @bar  # none
94    $foo = reduce { $a || !$code->(local $_ = $b) } 0, @bar  # notall
95       # Note that these implementations do not fully short-circuit
96
97If your algorithm requires that `reduce` produce an identity value, then make
98sure that you always pass that identity value as the first argument to prevent
99`undef` being returned
100
101    $foo = reduce { $a + $b } 0, @values;             # sum with 0 identity value
102
103The above example code blocks also suggest how to use `reduce` to build a
104more efficient combined version of one of these basic functions and a `map`
105block. For example, to find the total length of all the strings in a list,
106we could use
107
108    $total = sum map { length } @strings;
109
110However, this produces a list of temporary integer values as long as the
111original list of strings, only to reduce it down to a single value again. We
112can compute the same result more efficiently by using `reduce` with a code
113block that accumulates lengths by writing this instead as:
114
115    $total = reduce { $a + length $b } 0, @strings
116
117The other scalar-returning list reduction functions are all specialisations of
118this generic idea.
119
120## reductions
121
122    @results = reductions { BLOCK } @list
123
124_Since version 1.54._
125
126Similar to `reduce` except that it also returns the intermediate values along
127with the final result. As before, `$a` is set to the first element of the
128given list, and the `BLOCK` is then called once for remaining item in the
129list set into `$b`, with the result being captured for return as well as
130becoming the new value for `$a`.
131
132The returned list will begin with the initial value for `$a`, followed by
133each return value from the block in order. The final value of the result will
134be identical to what the `reduce` function would have returned given the same
135block and list.
136
137    reduce     { "$a-$b" }  "a".."d"    # "a-b-c-d"
138    reductions { "$a-$b" }  "a".."d"    # "a", "a-b", "a-b-c", "a-b-c-d"
139
140## any
141
142    my $bool = any { BLOCK } @list;
143
144_Since version 1.33._
145
146Similar to `grep` in that it evaluates `BLOCK` setting `$_` to each element
147of `@list` in turn. `any` returns true if any element makes the `BLOCK`
148return a true value. If `BLOCK` never returns true or `@list` was empty then
149it returns false.
150
151Many cases of using `grep` in a conditional can be written using `any`
152instead, as it can short-circuit after the first true result.
153
154    if( any { length > 10 } @strings ) {
155        # at least one string has more than 10 characters
156    }
157
158Note: Due to XS issues the block passed may be able to access the outer @\_
159directly. This is not intentional and will break under debugger.
160
161## all
162
163    my $bool = all { BLOCK } @list;
164
165_Since version 1.33._
166
167Similar to ["any"](#any), except that it requires all elements of the `@list` to
168make the `BLOCK` return true. If any element returns false, then it returns
169false. If the `BLOCK` never returns false or the `@list` was empty then it
170returns true.
171
172Note: Due to XS issues the block passed may be able to access the outer @\_
173directly. This is not intentional and will break under debugger.
174
175## none
176
177## notall
178
179    my $bool = none { BLOCK } @list;
180
181    my $bool = notall { BLOCK } @list;
182
183_Since version 1.33._
184
185Similar to ["any"](#any) and ["all"](#all), but with the return sense inverted. `none`
186returns true only if no value in the `@list` causes the `BLOCK` to return
187true, and `notall` returns true only if not all of the values do.
188
189Note: Due to XS issues the block passed may be able to access the outer @\_
190directly. This is not intentional and will break under debugger.
191
192## first
193
194    my $val = first { BLOCK } @list;
195
196Similar to `grep` in that it evaluates `BLOCK` setting `$_` to each element
197of `@list` in turn. `first` returns the first element where the result from
198`BLOCK` is a true value. If `BLOCK` never returns true or `@list` was empty
199then `undef` is returned.
200
201    $foo = first { defined($_) } @list    # first defined value in @list
202    $foo = first { $_ > $value } @list    # first value in @list which
203                                          # is greater than $value
204
205## max
206
207    my $num = max @list;
208
209Returns the entry in the list with the highest numerical value. If the list is
210empty then `undef` is returned.
211
212    $foo = max 1..10                # 10
213    $foo = max 3,9,12               # 12
214    $foo = max @bar, @baz           # whatever
215
216## maxstr
217
218    my $str = maxstr @list;
219
220Similar to ["max"](#max), but treats all the entries in the list as strings and
221returns the highest string as defined by the `gt` operator. If the list is
222empty then `undef` is returned.
223
224    $foo = maxstr 'A'..'Z'          # 'Z'
225    $foo = maxstr "hello","world"   # "world"
226    $foo = maxstr @bar, @baz        # whatever
227
228## min
229
230    my $num = min @list;
231
232Similar to ["max"](#max) but returns the entry in the list with the lowest numerical
233value. If the list is empty then `undef` is returned.
234
235    $foo = min 1..10                # 1
236    $foo = min 3,9,12               # 3
237    $foo = min @bar, @baz           # whatever
238
239## minstr
240
241    my $str = minstr @list;
242
243Similar to ["min"](#min), but treats all the entries in the list as strings and
244returns the lowest string as defined by the `lt` operator. If the list is
245empty then `undef` is returned.
246
247    $foo = minstr 'A'..'Z'          # 'A'
248    $foo = minstr "hello","world"   # "hello"
249    $foo = minstr @bar, @baz        # whatever
250
251## product
252
253    my $num = product @list;
254
255_Since version 1.35._
256
257Returns the numerical product of all the elements in `@list`. If `@list` is
258empty then `1` is returned.
259
260    $foo = product 1..10            # 3628800
261    $foo = product 3,9,12           # 324
262
263## sum
264
265    my $num_or_undef = sum @list;
266
267Returns the numerical sum of all the elements in `@list`. For backwards
268compatibility, if `@list` is empty then `undef` is returned.
269
270    $foo = sum 1..10                # 55
271    $foo = sum 3,9,12               # 24
272    $foo = sum @bar, @baz           # whatever
273
274## sum0
275
276    my $num = sum0 @list;
277
278_Since version 1.26._
279
280Similar to ["sum"](#sum), except this returns 0 when given an empty list, rather
281than `undef`.
282
283# KEY/VALUE PAIR LIST FUNCTIONS
284
285The following set of functions, all inspired by [List::Pairwise](https://metacpan.org/pod/List%3A%3APairwise), consume an
286even-sized list of pairs. The pairs may be key/value associations from a hash,
287or just a list of values. The functions will all preserve the original ordering
288of the pairs, and will not be confused by multiple pairs having the same "key"
289value - nor even do they require that the first of each pair be a plain string.
290
291**NOTE**: At the time of writing, the following `pair*` functions that take a
292block do not modify the value of `$_` within the block, and instead operate
293using the `$a` and `$b` globals instead. This has turned out to be a poor
294design, as it precludes the ability to provide a `pairsort` function. Better
295would be to pass pair-like objects as 2-element array references in `$_`, in
296a style similar to the return value of the `pairs` function. At some future
297version this behaviour may be added.
298
299Until then, users are alerted **NOT** to rely on the value of `$_` remaining
300unmodified between the outside and the inside of the control block. In
301particular, the following example is **UNSAFE**:
302
303    my @kvlist = ...
304
305    foreach (qw( some keys here )) {
306       my @items = pairgrep { $a eq $_ } @kvlist;
307       ...
308    }
309
310Instead, write this using a lexical variable:
311
312    foreach my $key (qw( some keys here )) {
313       my @items = pairgrep { $a eq $key } @kvlist;
314       ...
315    }
316
317## pairs
318
319    my @pairs = pairs @kvlist;
320
321_Since version 1.29._
322
323A convenient shortcut to operating on even-sized lists of pairs, this function
324returns a list of `ARRAY` references, each containing two items from the
325given list. It is a more efficient version of
326
327    @pairs = pairmap { [ $a, $b ] } @kvlist
328
329It is most convenient to use in a `foreach` loop, for example:
330
331    foreach my $pair ( pairs @kvlist ) {
332       my ( $key, $value ) = @$pair;
333       ...
334    }
335
336Since version `1.39` these `ARRAY` references are blessed objects,
337recognising the two methods `key` and `value`. The following code is
338equivalent:
339
340    foreach my $pair ( pairs @kvlist ) {
341       my $key   = $pair->key;
342       my $value = $pair->value;
343       ...
344    }
345
346Since version `1.51` they also have a `TO_JSON` method to ease
347serialisation.
348
349## unpairs
350
351    my @kvlist = unpairs @pairs
352
353_Since version 1.42._
354
355The inverse function to `pairs`; this function takes a list of `ARRAY`
356references containing two elements each, and returns a flattened list of the
357two values from each of the pairs, in order. This is notionally equivalent to
358
359    my @kvlist = map { @{$_}[0,1] } @pairs
360
361except that it is implemented more efficiently internally. Specifically, for
362any input item it will extract exactly two values for the output list; using
363`undef` if the input array references are short.
364
365Between `pairs` and `unpairs`, a higher-order list function can be used to
366operate on the pairs as single scalars; such as the following near-equivalents
367of the other `pair*` higher-order functions:
368
369    @kvlist = unpairs grep { FUNC } pairs @kvlist
370    # Like pairgrep, but takes $_ instead of $a and $b
371
372    @kvlist = unpairs map { FUNC } pairs @kvlist
373    # Like pairmap, but takes $_ instead of $a and $b
374
375Note however that these versions will not behave as nicely in scalar context.
376
377Finally, this technique can be used to implement a sort on a keyvalue pair
378list; e.g.:
379
380    @kvlist = unpairs sort { $a->key cmp $b->key } pairs @kvlist
381
382## pairkeys
383
384    my @keys = pairkeys @kvlist;
385
386_Since version 1.29._
387
388A convenient shortcut to operating on even-sized lists of pairs, this function
389returns a list of the the first values of each of the pairs in the given list.
390It is a more efficient version of
391
392    @keys = pairmap { $a } @kvlist
393
394## pairvalues
395
396    my @values = pairvalues @kvlist;
397
398_Since version 1.29._
399
400A convenient shortcut to operating on even-sized lists of pairs, this function
401returns a list of the the second values of each of the pairs in the given list.
402It is a more efficient version of
403
404    @values = pairmap { $b } @kvlist
405
406## pairgrep
407
408    my @kvlist = pairgrep { BLOCK } @kvlist;
409
410    my $count = pairgrep { BLOCK } @kvlist;
411
412_Since version 1.29._
413
414Similar to perl's `grep` keyword, but interprets the given list as an
415even-sized list of pairs. It invokes the `BLOCK` multiple times, in scalar
416context, with `$a` and `$b` set to successive pairs of values from the
417`@kvlist`.
418
419Returns an even-sized list of those pairs for which the `BLOCK` returned true
420in list context, or the count of the **number of pairs** in scalar context.
421(Note, therefore, in scalar context that it returns a number half the size of
422the count of items it would have returned in list context).
423
424    @subset = pairgrep { $a =~ m/^[[:upper:]]+$/ } @kvlist
425
426As with `grep` aliasing `$_` to list elements, `pairgrep` aliases `$a` and
427`$b` to elements of the given list. Any modifications of it by the code block
428will be visible to the caller.
429
430## pairfirst
431
432    my ( $key, $val ) = pairfirst { BLOCK } @kvlist;
433
434    my $found = pairfirst { BLOCK } @kvlist;
435
436_Since version 1.30._
437
438Similar to the ["first"](#first) function, but interprets the given list as an
439even-sized list of pairs. It invokes the `BLOCK` multiple times, in scalar
440context, with `$a` and `$b` set to successive pairs of values from the
441`@kvlist`.
442
443Returns the first pair of values from the list for which the `BLOCK` returned
444true in list context, or an empty list of no such pair was found. In scalar
445context it returns a simple boolean value, rather than either the key or the
446value found.
447
448    ( $key, $value ) = pairfirst { $a =~ m/^[[:upper:]]+$/ } @kvlist
449
450As with `grep` aliasing `$_` to list elements, `pairfirst` aliases `$a` and
451`$b` to elements of the given list. Any modifications of it by the code block
452will be visible to the caller.
453
454## pairmap
455
456    my @list = pairmap { BLOCK } @kvlist;
457
458    my $count = pairmap { BLOCK } @kvlist;
459
460_Since version 1.29._
461
462Similar to perl's `map` keyword, but interprets the given list as an
463even-sized list of pairs. It invokes the `BLOCK` multiple times, in list
464context, with `$a` and `$b` set to successive pairs of values from the
465`@kvlist`.
466
467Returns the concatenation of all the values returned by the `BLOCK` in list
468context, or the count of the number of items that would have been returned in
469scalar context.
470
471    @result = pairmap { "The key $a has value $b" } @kvlist
472
473As with `map` aliasing `$_` to list elements, `pairmap` aliases `$a` and
474`$b` to elements of the given list. Any modifications of it by the code block
475will be visible to the caller.
476
477See ["KNOWN BUGS"](#known-bugs) for a known-bug with `pairmap`, and a workaround.
478
479# OTHER FUNCTIONS
480
481## shuffle
482
483    my @values = shuffle @values;
484
485Returns the values of the input in a random order
486
487    @cards = shuffle 0..51      # 0..51 in a random order
488
489This function is affected by the `$RAND` variable.
490
491## sample
492
493    my @items = sample $count, @values
494
495_Since version 1.54._
496
497Randomly select the given number of elements from the input list. Any given
498position in the input list will be selected at most once.
499
500If there are fewer than `$count` items in the list then the function will
501return once all of them have been randomly selected; effectively the function
502behaves similarly to ["shuffle"](#shuffle).
503
504This function is affected by the `$RAND` variable.
505
506## uniq
507
508    my @subset = uniq @values
509
510_Since version 1.45._
511
512Filters a list of values to remove subsequent duplicates, as judged by a
513DWIM-ish string equality or `undef` test. Preserves the order of unique
514elements, and retains the first value of any duplicate set.
515
516    my $count = uniq @values
517
518In scalar context, returns the number of elements that would have been
519returned as a list.
520
521The `undef` value is treated by this function as distinct from the empty
522string, and no warning will be produced. It is left as-is in the returned
523list. Subsequent `undef` values are still considered identical to the first,
524and will be removed.
525
526## uniqint
527
528    my @subset = uniqint @values
529
530_Since version 1.55._
531
532Filters a list of values to remove subsequent duplicates, as judged by an
533integer numerical equality test. Preserves the order of unique elements, and
534retains the first value of any duplicate set. Values in the returned list will
535be coerced into integers.
536
537    my $count = uniqint @values
538
539In scalar context, returns the number of elements that would have been
540returned as a list.
541
542Note that `undef` is treated much as other numerical operations treat it; it
543compares equal to zero but additionally produces a warning if such warnings
544are enabled (`use warnings 'uninitialized';`). In addition, an `undef` in
545the returned list is coerced into a numerical zero, so that the entire list of
546values returned by `uniqint` are well-behaved as integers.
547
548## uniqnum
549
550    my @subset = uniqnum @values
551
552_Since version 1.44._
553
554Filters a list of values to remove subsequent duplicates, as judged by a
555numerical equality test. Preserves the order of unique elements, and retains
556the first value of any duplicate set.
557
558    my $count = uniqnum @values
559
560In scalar context, returns the number of elements that would have been
561returned as a list.
562
563Note that `undef` is treated much as other numerical operations treat it; it
564compares equal to zero but additionally produces a warning if such warnings
565are enabled (`use warnings 'uninitialized';`). In addition, an `undef` in
566the returned list is coerced into a numerical zero, so that the entire list of
567values returned by `uniqnum` are well-behaved as numbers.
568
569Note also that multiple IEEE `NaN` values are treated as duplicates of
570each other, regardless of any differences in their payloads, and despite
571the fact that `0+'NaN' == 0+'NaN'` yields false.
572
573## uniqstr
574
575    my @subset = uniqstr @values
576
577_Since version 1.45._
578
579Filters a list of values to remove subsequent duplicates, as judged by a
580string equality test. Preserves the order of unique elements, and retains the
581first value of any duplicate set.
582
583    my $count = uniqstr @values
584
585In scalar context, returns the number of elements that would have been
586returned as a list.
587
588Note that `undef` is treated much as other string operations treat it; it
589compares equal to the empty string but additionally produces a warning if such
590warnings are enabled (`use warnings 'uninitialized';`). In addition, an
591`undef` in the returned list is coerced into an empty string, so that the
592entire list of values returned by `uniqstr` are well-behaved as strings.
593
594## head
595
596    my @values = head $size, @list;
597
598_Since version 1.50._
599
600Returns the first `$size` elements from `@list`. If `$size` is negative, returns
601all but the last `$size` elements from `@list`.
602
603    @result = head 2, qw( foo bar baz );
604    # foo, bar
605
606    @result = head -2, qw( foo bar baz );
607    # foo
608
609## tail
610
611    my @values = tail $size, @list;
612
613_Since version 1.50._
614
615Returns the last `$size` elements from `@list`. If `$size` is negative, returns
616all but the first `$size` elements from `@list`.
617
618    @result = tail 2, qw( foo bar baz );
619    # bar, baz
620
621    @result = tail -2, qw( foo bar baz );
622    # baz
623
624# List::SomeUtils FUNCTIONS
625
626## Junctions
627
628### _Treatment of an empty list_
629
630There are two schools of thought for how to evaluate a junction on an
631empty list:
632
633- Reduction to an identity (boolean)
634- Result is undefined (three-valued)
635
636In the first case, the result of the junction applied to the empty list is
637determined by a mathematical reduction to an identity depending on whether
638the underlying comparison is "or" or "and".  Conceptually:
639
640                    "any are true"      "all are true"
641                    --------------      --------------
642    2 elements:     A || B || 0         A && B && 1
643    1 element:      A || 0              A && 1
644    0 elements:     0                   1
645
646In the second case, three-value logic is desired, in which a junction
647applied to an empty list returns `undef` rather than true or false
648
649Junctions with a `_u` suffix implement three-valued logic.  Those
650without are boolean.
651
652### all BLOCK LIST
653
654### all\_u BLOCK LIST
655
656Returns a true value if all items in LIST meet the criterion given through
657BLOCK. Sets `$_` for each item in LIST in turn:
658
659    print "All values are non-negative"
660      if all { $_ >= 0 } ($x, $y, $z);
661
662For an empty LIST, `all` returns true (i.e. no values failed the condition)
663and `all_u` returns `undef`.
664
665Thus, `all_u(@list)` is equivalent to `@list ? all(@list) : undef`.
666
667**Note**: because Perl treats `undef` as false, you must check the return value
668of `all_u` with `defined` or you will get the opposite result of what you
669expect.
670
671### any BLOCK LIST
672
673### any\_u BLOCK LIST
674
675Returns a true value if any item in LIST meets the criterion given through
676BLOCK. Sets `$_` for each item in LIST in turn:
677
678    print "At least one non-negative value"
679      if any { $_ >= 0 } ($x, $y, $z);
680
681For an empty LIST, `any` returns false and `any_u` returns `undef`.
682
683Thus, `any_u(@list)` is equivalent to `@list ? any(@list) : undef`.
684
685### none BLOCK LIST
686
687### none\_u BLOCK LIST
688
689Logically the negation of `any`. Returns a true value if no item in LIST meets
690the criterion given through BLOCK. Sets `$_` for each item in LIST in turn:
691
692    print "No non-negative values"
693      if none { $_ >= 0 } ($x, $y, $z);
694
695For an empty LIST, `none` returns true (i.e. no values failed the condition)
696and `none_u` returns `undef`.
697
698Thus, `none_u(@list)` is equivalent to `@list ? none(@list) : undef`.
699
700**Note**: because Perl treats `undef` as false, you must check the return value
701of `none_u` with `defined` or you will get the opposite result of what you
702expect.
703
704### notall BLOCK LIST
705
706### notall\_u BLOCK LIST
707
708Logically the negation of `all`. Returns a true value if not all items in LIST
709meet the criterion given through BLOCK. Sets `$_` for each item in LIST in
710turn:
711
712    print "Not all values are non-negative"
713      if notall { $_ >= 0 } ($x, $y, $z);
714
715For an empty LIST, `notall` returns false and `notall_u` returns `undef`.
716
717Thus, `notall_u(@list)` is equivalent to `@list ? notall(@list) : undef`.
718
719### one BLOCK LIST
720
721### one\_u BLOCK LIST
722
723Returns a true value if precisely one item in LIST meets the criterion
724given through BLOCK. Sets `$_` for each item in LIST in turn:
725
726    print "Precisely one value defined"
727        if one { defined($_) } @list;
728
729Returns false otherwise.
730
731For an empty LIST, `one` returns false and `one_u` returns `undef`.
732
733The expression `one BLOCK LIST` is almost equivalent to
734`1 == true BLOCK LIST`, except for short-cutting.
735Evaluation of BLOCK will immediately stop at the second true value.
736
737## Transformation
738
739### apply BLOCK LIST
740
741Makes a copy of the list and then passes each element _from the copy_ to the
742BLOCK. Any changes or assignments to `$_` in the BLOCK will only affect the
743elements of the new list. However, if `$_` is a reference then changes to the
744referenced value will be seen in both the original and new list.
745
746This function is similar to `map` but will not modify the elements of the
747input list:
748
749    my @list = (1 .. 4);
750    my @mult = apply { $_ *= 2 } @list;
751    print "\@list = @list\n";
752    print "\@mult = @mult\n";
753    __END__
754    @list = 1 2 3 4
755    @mult = 2 4 6 8
756
757Think of it as syntactic sugar for
758
759    for (my @mult = @list) { $_ *= 2 }
760
761Note that you must alter `$_` directly inside BLOCK in order for changes to
762make effect. New value returned from the BLOCK are ignored:
763
764    # @new is identical to @list.
765    my @new = apply { $_ * 2 } @list;
766
767    # @new is different from @list
768    my @new = apply { $_ =* 2 } @list;
769
770### insert\_after BLOCK VALUE LIST
771
772Inserts VALUE after the first item in LIST for which the criterion in BLOCK is
773true. Sets `$_` for each item in LIST in turn.
774
775    my @list = qw/This is a list/;
776    insert_after { $_ eq "a" } "longer" => @list;
777    print "@list";
778    __END__
779    This is a longer list
780
781### insert\_after\_string STRING VALUE LIST
782
783Inserts VALUE after the first item in LIST which is equal to STRING.
784
785    my @list = qw/This is a list/;
786    insert_after_string "a", "longer" => @list;
787    print "@list";
788    __END__
789    This is a longer list
790
791### pairwise BLOCK ARRAY1 ARRAY2
792
793Evaluates BLOCK for each pair of elements in ARRAY1 and ARRAY2 and returns a
794new list consisting of BLOCK's return values. The two elements are set to `$a`
795and `$b`.  Note that those two are aliases to the original value so changing
796them will modify the input arrays.
797
798    @a = (1 .. 5);
799    @b = (11 .. 15);
800    @x = pairwise { $a + $b } @a, @b;     # returns 12, 14, 16, 18, 20
801
802    # mesh with pairwise
803    @a = qw/a b c/;
804    @b = qw/1 2 3/;
805    @x = pairwise { ($a, $b) } @a, @b;    # returns a, 1, b, 2, c, 3
806
807### mesh ARRAY1 ARRAY2 \[ ARRAY3 ... \]
808
809### zip ARRAY1 ARRAY2 \[ ARRAY3 ... \]
810
811Returns a list consisting of the first elements of each array, then
812the second, then the third, etc, until all arrays are exhausted.
813
814Examples:
815
816    @x = qw/a b c d/;
817    @y = qw/1 2 3 4/;
818    @z = mesh @x, @y;         # returns a, 1, b, 2, c, 3, d, 4
819
820    @a = ('x');
821    @b = ('1', '2');
822    @c = qw/zip zap zot/;
823    @d = mesh @a, @b, @c;   # x, 1, zip, undef, 2, zap, undef, undef, zot
824
825`zip` is an alias for `mesh`.
826
827### uniq LIST
828
829### distinct LIST
830
831Returns a new list by stripping duplicate values in LIST by comparing
832the values as hash keys, except that undef is considered separate from ''.
833The order of elements in the returned list is the same as in LIST. In
834scalar context, returns the number of unique elements in LIST.
835
836    my @x = uniq 1, 1, 2, 2, 3, 5, 3, 4; # returns 1 2 3 5 4
837    my $x = uniq 1, 1, 2, 2, 3, 5, 3, 4; # returns 5
838    # returns "Mike", "Michael", "Richard", "Rick"
839    my @n = distinct "Mike", "Michael", "Richard", "Rick", "Michael", "Rick"
840    # returns '', undef, 'S1', A5'
841    my @s = distinct '', undef, 'S1', 'A5'
842    # returns '', undef, 'S1', A5'
843    my @w = uniq undef, '', 'S1', 'A5'
844
845`distinct` is an alias for `uniq`.
846
847**RT#49800** can be used to give feedback about this behavior.
848
849### singleton
850
851Returns a new list by stripping values in LIST occurring more than once by
852comparing the values as hash keys, except that undef is considered separate
853from ''.  The order of elements in the returned list is the same as in LIST.
854In scalar context, returns the number of elements occurring only once in LIST.
855
856    my @x = singleton 1,1,2,2,3,4,5 # returns 3 4 5
857
858## Partitioning
859
860### after BLOCK LIST
861
862Returns a list of the values of LIST after (and not including) the point
863where BLOCK returns a true value. Sets `$_` for each element in LIST in turn.
864
865    @x = after { $_ % 5 == 0 } (1..9);    # returns 6, 7, 8, 9
866
867### after\_incl BLOCK LIST
868
869Same as `after` but also includes the element for which BLOCK is true.
870
871### before BLOCK LIST
872
873Returns a list of values of LIST up to (and not including) the point where BLOCK
874returns a true value. Sets `$_` for each element in LIST in turn.
875
876### before\_incl BLOCK LIST
877
878Same as `before` but also includes the element for which BLOCK is true.
879
880### part BLOCK LIST
881
882Partitions LIST based on the return value of BLOCK which denotes into which
883partition the current value is put.
884
885Returns a list of the partitions thusly created. Each partition created is a
886reference to an array.
887
888    my $i = 0;
889    my @part = part { $i++ % 2 } 1 .. 8;   # returns [1, 3, 5, 7], [2, 4, 6, 8]
890
891You can have a sparse list of partitions as well where non-set partitions will
892be undef:
893
894    my @part = part { 2 } 1 .. 10;            # returns undef, undef, [ 1 .. 10 ]
895
896Be careful with negative values, though:
897
898    my @part = part { -1 } 1 .. 10;
899    __END__
900    Modification of non-creatable array value attempted, subscript -1 ...
901
902Negative values are only ok when they refer to a partition previously created:
903
904    my @idx  = ( 0, 1, -1 );
905    my $i    = 0;
906    my @part = part { $idx[$i++ % 3] } 1 .. 8; # [1, 4, 7], [2, 3, 5, 6, 8]
907
908## Iteration
909
910### each\_array ARRAY1 ARRAY2 ...
911
912Creates an array iterator to return the elements of the list of arrays ARRAY1,
913ARRAY2 throughout ARRAYn in turn.  That is, the first time it is called, it
914returns the first element of each array.  The next time, it returns the second
915elements.  And so on, until all elements are exhausted.
916
917This is useful for looping over more than one array at once:
918
919    my $ea = each_array(@a, @b, @c);
920    while ( my ($a, $b, $c) = $ea->() )   { .... }
921
922The iterator returns the empty list when it reached the end of all arrays.
923
924If the iterator is passed an argument of '`index`', then it returns
925the index of the last fetched set of values, as a scalar.
926
927### each\_arrayref LIST
928
929Like each\_array, but the arguments are references to arrays, not the
930plain arrays.
931
932### natatime EXPR, LIST
933
934Creates an array iterator, for looping over an array in chunks of
935`$n` items at a time.  (n at a time, get it?).  An example is
936probably a better explanation than I could give in words.
937
938Example:
939
940    my @x = ('a' .. 'g');
941    my $it = natatime 3, @x;
942    while (my @vals = $it->())
943    {
944      print "@vals\n";
945    }
946
947This prints
948
949    a b c
950    d e f
951    g
952
953## Searching
954
955### bsearch BLOCK LIST
956
957Performs a binary search on LIST which must be a sorted list of values. BLOCK
958must return a negative value if the current element (stored in `$_`) is smaller,
959a positive value if it is bigger and zero if it matches.
960
961Returns a boolean value in scalar context. In list context, it returns the element
962if it was found, otherwise the empty list.
963
964### bsearchidx BLOCK LIST
965
966### bsearch\_index BLOCK LIST
967
968Performs a binary search on LIST which must be a sorted list of values. BLOCK
969must return a negative value if the current element (stored in `$_`) is smaller,
970a positive value if it is bigger and zero if it matches.
971
972Returns the index of found element, otherwise `-1`.
973
974`bsearch_index` is an alias for `bsearchidx`.
975
976### firstval BLOCK LIST
977
978### first\_value BLOCK LIST
979
980Returns the first element in LIST for which BLOCK evaluates to true. Each
981element of LIST is set to `$_` in turn. Returns `undef` if no such element
982has been found.
983
984`first_value` is an alias for `firstval`.
985
986### onlyval BLOCK LIST
987
988### only\_value BLOCK LIST
989
990Returns the only element in LIST for which BLOCK evaluates to true. Sets
991`$_` for each item in LIST in turn. Returns `undef` if no such element
992has been found.
993
994`only_value` is an alias for `onlyval`.
995
996### lastval BLOCK LIST
997
998### last\_value BLOCK LIST
999
1000Returns the last value in LIST for which BLOCK evaluates to true. Each element
1001of LIST is set to `$_` in turn. Returns `undef` if no such element has been
1002found.
1003
1004`last_value` is an alias for `lastval`.
1005
1006### firstres BLOCK LIST
1007
1008### first\_result BLOCK LIST
1009
1010Returns the result of BLOCK for the first element in LIST for which BLOCK
1011evaluates to true. Each element of LIST is set to `$_` in turn. Returns
1012`undef` if no such element has been found.
1013
1014`first_result` is an alias for `firstres`.
1015
1016### onlyres BLOCK LIST
1017
1018### only\_result BLOCK LIST
1019
1020Returns the result of BLOCK for the first element in LIST for which BLOCK
1021evaluates to true. Sets `$_` for each item in LIST in turn. Returns
1022`undef` if no such element has been found.
1023
1024`only_result` is an alias for `onlyres`.
1025
1026### lastres BLOCK LIST
1027
1028### last\_result BLOCK LIST
1029
1030Returns the result of BLOCK for the last element in LIST for which BLOCK
1031evaluates to true. Each element of LIST is set to `$_` in turn. Returns
1032`undef` if no such element has been found.
1033
1034`last_result` is an alias for `lastres`.
1035
1036### indexes BLOCK LIST
1037
1038Evaluates BLOCK for each element in LIST (assigned to `$_`) and returns a list
1039of the indices of those elements for which BLOCK returned a true value. This is
1040just like `grep` only that it returns indices instead of values:
1041
1042    @x = indexes { $_ % 2 == 0 } (1..10);   # returns 1, 3, 5, 7, 9
1043
1044### firstidx BLOCK LIST
1045
1046### first\_index BLOCK LIST
1047
1048Returns the index of the first element in LIST for which the criterion in BLOCK
1049is true. Sets `$_` for each item in LIST in turn:
1050
1051    my @list = (1, 4, 3, 2, 4, 6);
1052    printf "item with index %i in list is 4", firstidx { $_ == 4 } @list;
1053    __END__
1054    item with index 1 in list is 4
1055
1056Returns `-1` if no such item could be found.
1057
1058`first_index` is an alias for `firstidx`.
1059
1060### onlyidx BLOCK LIST
1061
1062### only\_index BLOCK LIST
1063
1064Returns the index of the only element in LIST for which the criterion
1065in BLOCK is true. Sets `$_` for each item in LIST in turn:
1066
1067    my @list = (1, 3, 4, 3, 2, 4);
1068    printf "uniqe index of item 2 in list is %i", onlyidx { $_ == 2 } @list;
1069    __END__
1070    unique index of item 2 in list is 4
1071
1072Returns `-1` if either no such item or more than one of these
1073has been found.
1074
1075`only_index` is an alias for `onlyidx`.
1076
1077### lastidx BLOCK LIST
1078
1079### last\_index BLOCK LIST
1080
1081Returns the index of the last element in LIST for which the criterion in BLOCK
1082is true. Sets `$_` for each item in LIST in turn:
1083
1084    my @list = (1, 4, 3, 2, 4, 6);
1085    printf "item with index %i in list is 4", lastidx { $_ == 4 } @list;
1086    __END__
1087    item with index 4 in list is 4
1088
1089Returns `-1` if no such item could be found.
1090
1091`last_index` is an alias for `lastidx`.
1092
1093## Sorting
1094
1095### sort\_by BLOCK LIST
1096
1097Returns the list of values sorted according to the string values returned by the
1098KEYFUNC block or function. A typical use of this may be to sort objects according
1099to the string value of some accessor, such as
1100
1101    sort_by { $_->name } @people
1102
1103The key function is called in scalar context, being passed each value in turn as
1104both $\_ and the only argument in the parameters, @\_. The values are then sorted
1105according to string comparisons on the values returned.
1106This is equivalent to
1107
1108    sort { $a->name cmp $b->name } @people
1109
1110except that it guarantees the name accessor will be executed only once per value.
1111One interesting use-case is to sort strings which may have numbers embedded in them
1112"naturally", rather than lexically.
1113
1114    sort_by { s/(\d+)/sprintf "%09d", $1/eg; $_ } @strings
1115
1116This sorts strings by generating sort keys which zero-pad the embedded numbers to
1117some level (9 digits in this case), helping to ensure the lexical sort puts them
1118in the correct order.
1119
1120### nsort\_by BLOCK LIST
1121
1122Similar to sort\_by but compares its key values numerically.
1123
1124## Counting and calculation
1125
1126### true BLOCK LIST
1127
1128Counts the number of elements in LIST for which the criterion in BLOCK is true.
1129Sets `$_` for  each item in LIST in turn:
1130
1131    printf "%i item(s) are defined", true { defined($_) } @list;
1132
1133### false BLOCK LIST
1134
1135Counts the number of elements in LIST for which the criterion in BLOCK is false.
1136Sets `$_` for each item in LIST in turn:
1137
1138    printf "%i item(s) are not defined", false { defined($_) } @list;
1139
1140### minmax LIST
1141
1142Calculates the minimum and maximum of LIST and returns a two element list with
1143the first element being the minimum and the second the maximum. Returns the
1144empty list if LIST was empty.
1145
1146The `minmax` algorithm differs from a naive iteration over the list where each
1147element is compared to two values being the so far calculated min and max value
1148in that it only requires 3n/2 - 2 comparisons. Thus it is the most efficient
1149possible algorithm.
1150
1151However, the Perl implementation of it has some overhead simply due to the fact
1152that there are more lines of Perl code involved. Therefore, LIST needs to be
1153fairly big in order for `minmax` to win over a naive implementation. This
1154limitation does not apply to the XS version.
1155
1156### mode LIST
1157
1158Calculates the most common items in the list and returns them as a list. This
1159is effectively done by string comparisons, so references will be
1160stringified. If they implement string overloading, this will be used.
1161
1162If more than one item appears the same number of times in the list, all such
1163items will be returned. For example, the mode of a unique list is the list
1164itself.
1165
1166This function returns a list in list context. In scalar context it returns a
1167count indicating the number of modes in the list.
1168
1169# List::UtilsBy FUNCTIONS
1170
1171All functions added since version 0.04 unless otherwise stated, as the
1172original names for earlier versions were renamed.
1173
1174## sort\_by
1175
1176    @vals = sort_by { KEYFUNC } @vals
1177
1178Returns the list of values sorted according to the string values returned by
1179the `KEYFUNC` block or function. A typical use of this may be to sort objects
1180according to the string value of some accessor, such as
1181
1182    sort_by { $_->name } @people
1183
1184The key function is called in scalar context, being passed each value in turn
1185as both `$_` and the only argument in the parameters, `@_`. The values are
1186then sorted according to string comparisons on the values returned.
1187
1188This is equivalent to
1189
1190    sort { $a->name cmp $b->name } @people
1191
1192except that it guarantees the `name` accessor will be executed only once per
1193value.
1194
1195One interesting use-case is to sort strings which may have numbers embedded in
1196them "naturally", rather than lexically.
1197
1198    sort_by { s/(\d+)/sprintf "%09d", $1/eg; $_ } @strings
1199
1200This sorts strings by generating sort keys which zero-pad the embedded numbers
1201to some level (9 digits in this case), helping to ensure the lexical sort puts
1202them in the correct order.
1203
1204## nsort\_by
1205
1206    @vals = nsort_by { KEYFUNC } @vals
1207
1208Similar to ["sort\_by"](#sort_by) but compares its key values numerically.
1209
1210## rev\_sort\_by
1211
1212## rev\_nsort\_by
1213
1214    @vals = rev_sort_by { KEYFUNC } @vals
1215
1216    @vals = rev_nsort_by { KEYFUNC } @vals
1217
1218_Since version 0.06._
1219
1220Similar to ["sort\_by"](#sort_by) and ["nsort\_by"](#nsort_by) but returns the list in the reverse
1221order. Equivalent to
1222
1223    @vals = reverse sort_by { KEYFUNC } @vals
1224
1225except that these functions are slightly more efficient because they avoid
1226the final `reverse` operation.
1227
1228## max\_by
1229
1230    $optimal = max_by { KEYFUNC } @vals
1231
1232    @optimal = max_by { KEYFUNC } @vals
1233
1234Returns the (first) value from `@vals` that gives the numerically largest
1235result from the key function.
1236
1237    my $tallest = max_by { $_->height } @people
1238
1239    use File::stat qw( stat );
1240    my $newest = max_by { stat($_)->mtime } @files;
1241
1242In scalar context, the first maximal value is returned. In list context, a
1243list of all the maximal values is returned. This may be used to obtain
1244positions other than the first, if order is significant.
1245
1246If called on an empty list, an empty list is returned.
1247
1248For symmetry with the ["nsort\_by"](#nsort_by) function, this is also provided under the
1249name `nmax_by` since it behaves numerically.
1250
1251## min\_by
1252
1253    $optimal = min_by { KEYFUNC } @vals
1254
1255    @optimal = min_by { KEYFUNC } @vals
1256
1257Similar to ["max\_by"](#max_by) but returns values which give the numerically smallest
1258result from the key function. Also provided as `nmin_by`
1259
1260## minmax\_by
1261
1262    ( $minimal, $maximal ) = minmax_by { KEYFUNC } @vals
1263
1264_Since version 0.11._
1265
1266Similar to calling both ["min\_by"](#min_by) and ["max\_by"](#max_by) with the same key function
1267on the same list. This version is more efficient than calling the two other
1268functions individually, as it has less work to perform overall. In the case of
1269ties, only the first optimal element found in each case is returned. Also
1270provided as `nminmax_by`.
1271
1272## uniq\_by
1273
1274    @vals = uniq_by { KEYFUNC } @vals
1275
1276Returns a list of the subset of values for which the key function block
1277returns unique values. The first value yielding a particular key is chosen,
1278subsequent values are rejected.
1279
1280    my @some_fruit = uniq_by { $_->colour } @fruit;
1281
1282To select instead the last value per key, reverse the input list. If the order
1283of the results is significant, don't forget to reverse the result as well:
1284
1285    my @some_fruit = reverse uniq_by { $_->colour } reverse @fruit;
1286
1287Because the values returned by the key function are used as hash keys, they
1288ought to either be strings, or at least well-behaved as strings (such as
1289numbers, or object references which overload stringification in a suitable
1290manner).
1291
1292## partition\_by
1293
1294    %parts = partition_by { KEYFUNC } @vals
1295
1296Returns a key/value list of ARRAY refs containing all the original values
1297distributed according to the result of the key function block. Each value will
1298be an ARRAY ref containing all the values which returned the string from the
1299key function, in their original order.
1300
1301    my %balls_by_colour = partition_by { $_->colour } @balls;
1302
1303Because the values returned by the key function are used as hash keys, they
1304ought to either be strings, or at least well-behaved as strings (such as
1305numbers, or object references which overload stringification in a suitable
1306manner).
1307
1308## count\_by
1309
1310    %counts = count_by { KEYFUNC } @vals
1311
1312_Since version 0.07._
1313
1314Returns a key/value list of integers, giving the number of times the key
1315function block returned the key, for each value in the list.
1316
1317    my %count_of_balls = count_by { $_->colour } @balls;
1318
1319Because the values returned by the key function are used as hash keys, they
1320ought to either be strings, or at least well-behaved as strings (such as
1321numbers, or object references which overload stringification in a suitable
1322manner).
1323
1324## zip\_by
1325
1326    @vals = zip_by { ITEMFUNC } \@arr0, \@arr1, \@arr2,...
1327
1328Returns a list of each of the values returned by the function block, when
1329invoked with values from across each each of the given ARRAY references. Each
1330value in the returned list will be the result of the function having been
1331invoked with arguments at that position, from across each of the arrays given.
1332
1333    my @transposition = zip_by { [ @_ ] } @matrix;
1334
1335    my @names = zip_by { "$_[1], $_[0]" } \@firstnames, \@surnames;
1336
1337    print zip_by { "$_[0] => $_[1]\n" } [ keys %hash ], [ values %hash ];
1338
1339If some of the arrays are shorter than others, the function will behave as if
1340they had `undef` in the trailing positions. The following two lines are
1341equivalent:
1342
1343    zip_by { f(@_) } [ 1, 2, 3 ], [ "a", "b" ]
1344    f( 1, "a" ), f( 2, "b" ), f( 3, undef )
1345
1346The item function is called by `map`, so if it returns a list, the entire
1347list is included in the result. This can be useful for example, for generating
1348a hash from two separate lists of keys and values
1349
1350    my %nums = zip_by { @_ } [qw( one two three )], [ 1, 2, 3 ];
1351    # %nums = ( one => 1, two => 2, three => 3 )
1352
1353(A function having this behaviour is sometimes called `zipWith`, e.g. in
1354Haskell, but that name would not fit the naming scheme used by this module).
1355
1356## unzip\_by
1357
1358    $arr0, $arr1, $arr2, ... = unzip_by { ITEMFUNC } @vals
1359
1360_Since version 0.09._
1361
1362Returns a list of ARRAY references containing the values returned by the
1363function block, when invoked for each of the values given in the input list.
1364Each of the returned ARRAY references will contain the values returned at that
1365corresponding position by the function block. That is, the first returned
1366ARRAY reference will contain all the values returned in the first position by
1367the function block, the second will contain all the values from the second
1368position, and so on.
1369
1370    my ( $firstnames, $lastnames ) = unzip_by { m/^(.*?) (.*)$/ } @names;
1371
1372If the function returns lists of differing lengths, the result will be padded
1373with `undef` in the missing elements.
1374
1375This function is an inverse of ["zip\_by"](#zip_by), if given a corresponding inverse
1376function.
1377
1378## extract\_by
1379
1380    @vals = extract_by { SELECTFUNC } @arr
1381
1382_Since version 0.05._
1383
1384Removes elements from the referenced array on which the selection function
1385returns true, and returns a list containing those elements. This function is
1386similar to `grep`, except that it modifies the referenced array to remove the
1387selected values from it, leaving only the unselected ones.
1388
1389    my @red_balls = extract_by { $_->color eq "red" } @balls;
1390
1391    # Now there are no red balls in the @balls array
1392
1393This function modifies a real array, unlike most of the other functions in this
1394module. Because of this, it requires a real array, not just a list.
1395
1396This function is implemented by invoking `splice` on the array, not by
1397constructing a new list and assigning it. One result of this is that weak
1398references will not be disturbed.
1399
1400    extract_by { !defined $_ } @refs;
1401
1402will leave weak references weakened in the `@refs` array, whereas
1403
1404    @refs = grep { defined $_ } @refs;
1405
1406will strengthen them all again.
1407
1408## extract\_first\_by
1409
1410    $val = extract_first_by { SELECTFUNC } @arr
1411
1412_Since version 0.10._
1413
1414A hybrid between ["extract\_by"](#extract_by) and `List::Util::first`. Removes the first
1415element from the referenced array on which the selection function returns
1416true, returning it.
1417
1418As with ["extract\_by"](#extract_by), this function requires a real array and not just a
1419list, and is also implemented using `splice` so that weak references are
1420not disturbed.
1421
1422If this function fails to find a matching element, it will return an empty
1423list in list context. This allows a caller to distinguish the case between
1424no matching element, and the first matching element being `undef`.
1425
1426## weighted\_shuffle\_by
1427
1428    @vals = weighted_shuffle_by { WEIGHTFUNC } @vals
1429
1430_Since version 0.07._
1431
1432Returns the list of values shuffled into a random order. The randomisation is
1433not uniform, but weighted by the value returned by the `WEIGHTFUNC`. The
1434probabilty of each item being returned first will be distributed with the
1435distribution of the weights, and so on recursively for the remaining items.
1436
1437## bundle\_by
1438
1439    @vals = bundle_by { BLOCKFUNC } $number, @vals
1440
1441_Since version 0.07._
1442
1443Similar to a regular `map` functional, returns a list of the values returned
1444by `BLOCKFUNC`. Values from the input list are given to the block function in
1445bundles of `$number`.
1446
1447If given a list of values whose length does not evenly divide by `$number`,
1448the final call will be passed fewer elements than the others.
1449
1450# EXPORTS
1451
1452This module exports nothing by default. You can import functions by
1453name, or get everything with the `:all` tag.
1454
1455# SEE ALSO
1456
1457[List::Util](https://metacpan.org/pod/List%3A%3AUtil),  [List::SomeUtils](https://metacpan.org/pod/List%3A%3ASomeUtils) and [List::UtilsBy](https://metacpan.org/pod/List%3A%3AUtilsBy), obviously.
1458
1459Also see `Util::Any`, which unifies many more util modules, and also
1460lets you rename functions as part of the import.
1461
1462# BUGS
1463
1464Please report any bugs or feature requests to
1465`bug-list-allutils@rt.cpan.org`, or through the web interface at
1466[http://rt.cpan.org](http://rt.cpan.org).  I will be notified, and then you'll
1467automatically be notified of progress on your bug as I make changes.
1468
1469Bugs may be submitted at [https://github.com/houseabsolute/List-AllUtils/issues](https://github.com/houseabsolute/List-AllUtils/issues).
1470
1471I am also usually active on IRC as 'autarch' on `irc://irc.perl.org`.
1472
1473# SOURCE
1474
1475The source code repository for List-AllUtils can be found at [https://github.com/houseabsolute/List-AllUtils](https://github.com/houseabsolute/List-AllUtils).
1476
1477# DONATIONS
1478
1479If you'd like to thank me for the work I've done on this module, please
1480consider making a "donation" to me via PayPal. I spend a lot of free time
1481creating free software, and would appreciate any support you'd care to offer.
1482
1483Please note that **I am not suggesting that you must do this** in order for me
1484to continue working on this particular software. I will continue to do so,
1485inasmuch as I have in the past, for as long as it interests me.
1486
1487Similarly, a donation made in this way will probably not make me work on this
1488software much more, unless I get so many donations that I can consider working
1489on free software full time (let's all have a chuckle at that together).
1490
1491To donate, log into PayPal and send money to autarch@urth.org, or use the
1492button at [https://www.urth.org/fs-donation.html](https://www.urth.org/fs-donation.html).
1493
1494# AUTHOR
1495
1496Dave Rolsky <autarch@urth.org>
1497
1498# CONTRIBUTORS
1499
1500- Andy Jack <github@veracity.ca>
1501- Dave Jacoby <jacoby.david@gmail.com>
1502- Karen Etheridge <ether@cpan.org>
1503- Olaf Alders <olaf@wundersolutions.com>
1504- Ricardo Signes <rjbs@cpan.org>
1505- Yanick Champoux <yanick@babyl.dyndns.org>
1506
1507# COPYRIGHT AND LICENSE
1508
1509This software is Copyright (c) 2021 by Dave Rolsky.
1510
1511This is free software, licensed under:
1512
1513    The Artistic License 2.0 (GPL Compatible)
1514
1515The full text of the license can be found in the
1516`LICENSE` file included with this distribution.
1517