Help language development. Donate to The Perl Foundation

Raku port of Perl's List::MoreUtils module 0.430

```
# import specific functions
use List::MoreUtils <any uniq>;
if any { /foo/ }, uniq @has_duplicates {
# do stuff
}
# import everything
use List::MoreUtils ':all';
```

This module tries to mimic the behaviour of Perl's `List::MoreUtils`

module as closely as possible in the Raku Programming Language.

List::MoreUtils provides some trivial but commonly needed functionality on lists which is not going to go into `List::Util`

.

Nothing by default. To import all of this module's symbols use the `:all`

tag. Otherwise functions can be imported by name as usual:

```
use List::MoreUtils :all;
use List::MoreUtils <any firstidx>;
```

Raku does not have the concept of `scalar`

and `list`

context. Usually, the effect of a scalar context can be achieved by prefixing `+`

to the result, which would effectively return the number of elements in the result, which usually is the same as the scalar context of Perl of these functions.

Raku does not have a magic `$a`

and `$b`

. But they can be made to exist by specifying the correct signature to blocks, specifically "-> $a, $b". These have been used in all examples that needed them. Just using the signature auto-generating `$^a`

and `$^b`

would be more Raku like. But since we want to keep the documentation as close to the original as possible, it was decided to specifically specify the "-> $a, $b" signatures.

Many functions take a `&code`

parameter of a `Block`

to be called by the function. Many of these assume **$_** will be set. In Raku, this happens automagically if you create a block without a definite or implicit signature:

```
say { $_ == 4 }.signature; # (;; $_? is raw)
```

which indicates the Block takes an optional parameter that will be aliased as `$_`

inside the Block.

Raku also doesn't have a single `undef`

value, but instead has `Type Objects`

, which could be considered undef values, but with a type annotation. In this module, `Nil`

(a special value denoting the absence of a value where there should have been one) is used instead of `undef`

.

Also note there are no special parsing rules with regards to blocks in Raku. So a comma is **always** required after having specified a block.

The following functions are actually built-ins in Raku.

```
any all none minmax uniq zip
```

They mostly provide the same or similar semantics, but there may be subtle differences, so it was decided to not just use the built-ins. If these functions are imported from this library in a scope, they will used instead of the Raku builtins. The easiest way to use both the functions of this library and the Raku builtins in the same scope, is to use the method syntax for the Raku versions.

```
my @a = 42,5,2,98792,88;
{ # Note: imports in Raku are always lexically scoped
use List::Util <minmax>;
say minmax @a; # Ported Perl version
say @a.minmax; # Raku version
}
say minmax @a; # Raku version again
```

Many functions returns either `True`

or `False`

. These are `Bool`

ean objects in Raku, rather than just `0`

or `1`

. However, if you use a Boolean value in a numeric context, they are silently coerced to 0 and 1. So you can still use them in numeric calculations as if they are 0 and 1.

Some functions return something different in scalar context than in list context. Raku doesn't have those concepts. Functions that are supposed to return something different in scalar context also the `Scalar`

type as the first positional parameter to indicate the result like the result of a scalar context, is required. It will be noted with the function in question if that feature is available.

There are two schools of thought for how to evaluate a junction on an empty list:

Reduction to an identity (boolean)

Result is undefined (three-valued)

In the first case, the result of the junction applied to the empty list is determined by a mathematical reduction to an identity depending on whether the underlying comparison is "or" or "and". Conceptually:

```
"any are true" "all are true"
-------------- --------------
2 elements: A || B || 0 A && B && 1
1 element: A || 0 A && 1
0 elements: 0 1
```

In the second case, three-value logic is desired, in which a junction applied to an empty list returns `Nil`

rather than `True`

or `False`

.

Junctions with a `_u`

suffix implement three-valued logic. Those without are boolean.

Returns True if all items in LIST meet the criterion given through BLOCK. Passes each element in LIST to the BLOCK in turn:

```
say "All values are non-negative"
if all { $_ >= 0 }, ($x, $y, $z);
```

For an empty LIST, `all`

returns True (i.e. no values failed the condition) and `all_u`

returns `Nil`

.

Thus, `all_u(@list)`

is equivalent to `@list ?? all(@list) !! Nil`

.

**Note**: because Raku treats `Nil`

as false, you must check the return value of `all_u`

with `defined`

or you will get the opposite result of what you expect.

```
say "All values are non-negative"
if $x & $y & $z >= 0;
say "All values are non-negative"
if all(@list) >= 0;
```

Returns True if any item in LIST meets the criterion given through BLOCK. Passes each element in LIST to the BLOCK in turn:

```
say "At least one non-negative value"
if any { $_ >= 0 }, ($x, $y, $z);
```

For an empty LIST, `any`

returns False and `any_u`

returns `Nil`

.

Thus, `any_u(@list)`

is equivalent to `@list ?? any(@list) !! undef`

.

```
say "At least one non-negative value"
if $x | $y | $z >= 0;
say "At least one non-negative value"
if any(@list) >= 0;
```

Logically the negation of `any`

. Returns True if no item in LIST meets the criterion given through BLOCK. Passes each element in LIST to the BLOCK in turn:

```
say "No non-negative values"
if none { $_ >= 0 }, ($x, $y, $z);
```

For an empty LIST, `none`

returns True (i.e. no values failed the condition) and `none_u`

returns `Nil`

.

Thus, `none_u(@list)`

is equivalent to `@list ?? none(@list) !! Nil`

.

**Note**: because Raku treats `Nil`

as false, you must check the return value of `none_u`

with `defined`

or you will get the opposite result of what you expect.

```
say "No non-negative values"
if none($x,$y,$z) >= 0;
say "No non-negative values"
if none(@list) >= 0;
```

Logically the negation of `all`

. Returns True if not all items in LIST meet the criterion given through BLOCK. Passes each element in LIST to the BLOCK in turn:

```
say "Not all values are non-negative"
if notall { $_ >= 0 }, ($x, $y, $z);
```

For an empty LIST, `notall`

returns False and `notall_u`

returns `Nil`

.

Thus, `notall_u(@list)`

is equivalent to `@list ?? notall(@list) !! Nil`

.

```
say "Not all values are non-negative"
if not all($x,$y,$z) >= 0;
say "Not all values are non-negative"
if not all(@list) >= 0;
```

Returns True if precisely one item in LIST meets the criterion given through BLOCK. Passes each element in LIST to the BLOCK in turn:

```
say "Precisely one value defined"
if one { defined($_) }, @list;
```

Returns False otherwise.

For an empty LIST, `one`

returns False and `one_u`

returns `Nil`

.

The expression `one BLOCK, LIST`

is almost equivalent to `1 == True BLOCK, LIST`

, except for short-cutting. Evaluation of BLOCK will immediately stop at the second true value seen.

```
say "Precisely one value defined"
if ($x ^ $y ^ $z).defined;
say "Precisely one value defined"
if one(@list>>.defined);
```

Applies BLOCK to each item in LIST and returns a list of the values after BLOCK has been applied. Returns the last element if `Scalar`

has been specified. This function is similar to `map`

but will not modify the elements of the input list:

```
my @list = 1 .. 4;
my @mult = apply { $_ *= 2 }, @list;
print "@list = @list[]\n";
print "@mult = @mult[]\n";
=====================================
@list = 1 2 3 4
@mult = 2 4 6 8
```

With the `Scalar`

positional parameter:

```
my @list = 1 .. 4;
my $last = apply Scalar, { $_ *= 2 }, @list;
print "@list = @list[]\n";
print "\$last = $last\n";
=====================================
@list = 1 2 3 4
$last = 8
```

```
my @mult = @list.map: -> $_ is copy { $_ *= 2 };
my $last = @list.map( -> $_ is copy { $_ *= 2 }).tail;
```

Inserts VALUE after the first item in LIST for which the criterion in BLOCK is true. Sets `$_`

for each item in LIST in turn.

```
my @list = <This is a list>;
insert_after { $_ eq "a" }, "longer" => @list;
say "@list[]";
===================================
This is a longer list
```

Inserts VALUE after the first item in LIST which is equal to STRING.

```
my @list = <This is a list>;
insert_after_string "a", "longer" => @list;
say "@list[]";
===================================
This is a longer list
```

Evaluates BLOCK for each pair of elements in ARRAY1 and ARRAY2 and returns a new list consisting of BLOCK's return values. The two elements are passed as parameters to BLOCK.

```
my @a = 1 .. 5;
my @b = 11 .. 15;
my @x = pairwise -> $a, $b { $a + $b }, @a, @b; # returns 12, 14, 16, 18, 20
# mesh with pairwise
my @a = <a b c>;
my @b = <1 2 3>;
my @x = pairwise -> $a, $b { $a, $b }, @a, @b; # returns a, 1, b, 2, c, 3
```

```
my @x = zip(@a,@b).map: -> ($a,$b) { $a + $b };
my @x = zip(@a,@b).flat;
```

Returns a list consisting of the first elements of each array, then the second, then the third, etc, until all arrays are exhausted.

Examples:

```
my @x = <a b c d>;
my @y = <1 2 3 4>;
my @z = mesh @x, @y; # returns a, 1, b, 2, c, 3, d, 4
my Str @a = 'x';
my Int @b = 1, 2;
my @c = <zip zap zot>;
my @d = mesh @a, @b, @c; # x, 1, zip, Str, 2, zap, Str, Int, zot
```

`zip`

is an alias for `mesh`

.

```
my @x = zip(@a,@b).flat;
my @x = zip(@a,@b,@c).flat;
```

Returns a list of arrays consisting of the first elements of each array, then the second, then the third, etc, until all arrays are exhausted.

```
my @x = <a b c d>;
my @y = <1 2 3 4>;
my @z = zip6 @x, @y; # returns [a, 1], [b, 2], [c, 3], [d, 4]
my Str @a = 'x';
my Int @b = 1, 2;
my @c = <zip zap zot>;
my @d = zip6 @a, @b, @c; # [x, 1, zip], [Str, 2, zap], [Str, Int, zot]
```

`zip_unflatten`

is an alias for `zip6`

.

```
my @x = zip(@a,@b);
my @x = zip(@a,@b,@c);
```

Returns an associative list of elements and every *id* of the list it was found in. Allows easy implementation of @a & @b, @a | @b, @a ^ @b and so on. Undefined entries in any given array are skipped.

```
my @a = <one two four five six seven eight nine ten>;
my @b = <two five seven eleven thirteen seventeen>;
my @c = <one one two five eight thirteen twentyone>;
my %cmp := listcmp @a, @b, @c;
# (one => [0, 2], two => [0, 1, 2], four => [0], ...)
my @seq = 1, 2, 3;
my @prim = Int, 2, 3, 5;
my @fib = 1, 1, 2;
my $cmp = listcmp @seq, @prim, @fib;
# { 1 => [0, 2], 2 => [0, 1, 2], 3 => [0, 1], 5 => [1] }
```

```
my @x = zip(@a,@b);
my @x = zip(@a,@b,@c);
```

Returns a list costisting of each element of the given arrays. Recursive arrays are flattened, too.

```
my @a = 1, [[2], 3], 4, [5], 6, [7], 8, 9;
my @l = arrayify @a; # returns 1, 2, 3, 4, 5, 6, 7, 8, 9
```

Returns a new list by stripping duplicate values in LIST by comparing the values as hash keys, except that type objects are considered separate from ''. The order of elements in the returned list is the same as in LIST. Returns the number of unique elements in LIST if the `Scalar`

positional parameter has been specified.

```
my @x = uniq (1, 1, 2, 2, 3, 5, 3, 4); # returns (1,2,3,5,4)
my $x = uniq Scalar, (1, 1, 2, 2, 3, 5, 3, 4); # returns 5
my @n = distinct "Mike", "Michael", "Richard", "Rick", "Michael", "Rick"
# ("Mike", "Michael", "Richard", "Rick")
my @s = distinct "A8", "", Str, "A5", "S1", "A5", "A8"
# ("A8", "", Str, "A5", "S1")
my @w = uniq "Giulia", "Giulietta", Str, "", 156, "Giulietta", "Giulia";
# ("Giulia", "Giulietta", Str, "", 156)
```

`distinct`

is an alias for `uniq`

.

```
my @x = (1, 1, 2, 2, 3, 5, 3, 4).unique;
my $x = (1, 1, 2, 2, 3, 5, 3, 4).unique.elems;
```

Returns a new list by stripping values in LIST occurring only once by comparing the values as hash keys, except that type objects are considered separate from ''. The order of elements in the returned list is the same as in LIST. Returns the number of elements occurring only once in LIST if the `Scalar`

positional parameter has been specified.

```
my @x = singleton (1,1,4,2,2,3,3,5); # returns (4,5)
my $n = singleton Scalar, (1,1,4,2,2,3,3,5); # returns 2
```

Returns a new list by stripping values in LIST occuring more than once by comparing the values as hash keys, except that type objects are considered separate from ''. The order of elements in the returned list is the same as in LIST. Returns the number of elements occurring more than once in LIST.

```
my @y = duplicates (1,1,2,4,7,2,3,4,6,9); # returns (1,2,4)
my $n = duplicates Scalar, (1,1,2,4,7,2,3,4,6,9); # returns 3
```

```
my @y = (1,1,2,4,7,2,3,4,6,9).repeated;
my $n = (1,1,2,4,7,2,3,4,6,9).repeated.elems;
```

Returns a hash of distinct values and the corresponding frequency.

```
my %f := frequency values %radio_nrw; # returns (
# 'Deutschlandfunk (DLF)' => 9, 'WDR 3' => 10,
# 'WDR 4' => 11, 'WDR 5' => 14, 'WDR Eins Live' => 14,
# 'Deutschlandradio Kultur' => 8,...)
```

```
my %f := %radio_nrw.values.Bag;
```

Returns a new list of frequencies and the corresponding values from LIST.

```
my @o = occurrences (1 xx 3, 2 xx 4, 3 xx 2, 4 xx 7, 5 xx 2, 6 xx 4);
# (Any, Any, [3, 5], [1], [2, 6], Any, Any, [4])
```

Returns the modal value of LIST. Returns the modal value only if the `Scalar`

positional parameter is specified. Otherwise all probes occuring *modal* times are returned as well.

```
my @m = mode (1 xx 3, 2 xx 4, 3 xx 2, 4 xx 7, 5 xx 2, 6 xx 7);
# (7, 4, 6)
my $mode = mode Scalar, (1 xx 3, 2 xx 4, 3 xx 2, 4 xx 7, 5 xx 2, 6 xx 7);
# 7
```

Returns a list of the values of LIST after (and not including) the point where BLOCK returns a true value. Passes the value as a parameter to BLOCK for each element in LIST in turn.

```
my @x = after { $_ %% 5 }, (1..9); # returns (6, 7, 8, 9)
```

Same as `after`

but also includes the element for which BLOCK is true.

```
my @x = after_incl { $_ %% 5 }, (1..9); # returns (5, 6, 7, 8, 9)
```

```
my @x = (1..9).toggle: * %% 5, :off;
```

Returns a list of values of LIST up to (and not including) the point where BLOCK returns a true value. Passes the value as a parameter to BLOCK for each element in LIST in turn.

```
my @x = before { $_ %% 5 }, (1..9); # returns (1, 2, 3, 4)
```

```
my @x = (1..9).toggle: * %% 5;
```

Same as `before`

but also includes the element for which BLOCK is true.

```
my @x = before_incl { $_ %% 5 }, (1..9); # returns (1, 2, 3, 4, 5)
```

Partitions LIST based on the return value of BLOCK which denotes into which partition the current value is put.

Returns a list of the partitions thusly created. Each partition created is an Array.

```
my $i = 0;
my @part = part { $i++ % 2 } (1..8); # returns ([1, 3, 5, 7], [2, 4, 6, 8])
```

You can have a sparse list of partitions as well where non-set partitions will be an `Array`

type object:

```
my @part = part { 2 } (1..5); # returns (Array, Array, [1,2,3,4,5])
```

Be careful with negative values, though:

```
my @part = part { -1 } (1..10);
===============================
Unsupported use of a negative -1 subscript to index from the end
```

Negative values are only ok when they refer to a partition previously created:

```
my @idx = 0, 1, -1;
my $i = 0;
my @part = part { $idx[$i++ % 3] }, (1..8); # ([1, 4, 7], [2, 3, 5, 6, 8])
```

Returns a new list containing COUNT random samples from LIST. Is similar to List::Util/shuffle, but stops after COUNT.

```
my @r = samples 10, (1..10); # same as (1..10).pick(*)
my @r2 = samples 5, (1..10); # same as (1..10).pick(5)
```

```
my @r = (1..10).pick(*);
my @r2 = (1..10).pick(5);
```

Creates an array iterator to return the elements of the list of arrays ARRAY1, ARRAY2 throughout ARRAYn in turn. That is, the first time it is called, it returns the first element of each array. The next time, it returns the second elements. And so on, until all elements are exhausted.

This is useful for looping over more than one array at once:

```
my &ea = each_array(@a, @b, @c);
while ea() -> ($a,$b,$c) { .... }
```

The iterator returns the empty list when it reached the end of all arrays.

If the iterator is passed an argument of '`index`

', then it returns the index of the last fetched set of values, as a scalar.

```
while zip(@a,@b,@c) -> ($a,$b,$c) { .... }
```

Like each_array, but the arguments is a single list with arrays.

Creates an array iterator, for looping over an array in chunks of `$n`

items at a time. (n at a time, get it?). An example is probably a better explanation than I could give in words.

Example:

```
my @x = 'a'..'g';
my &it = natatime 3, @x;
while it() -> @vals {
print "@vals[]\n";
}
```

This prints

```
a b c
d e f
g
```

```
for @x.rotor(3,:partial) -> @vals {
print "@vals[]\n";
}
```

Returns the first element in LIST for which BLOCK evaluates to true. Each element of LIST is passed to the BLOCK in turn. Returns `Nil`

if no such element has been found.

```
my @list = <alpha beta cicero bearing effortless>;
say firstval { .starts-with('c') }, @list; # cicero
say firstval { .starts-with('b') }, @list; # beta
say firstval { .starts-with('g') }, @list; # Nil, because never
```

`first_value`

is an alias for `firstval`

.

```
say @list.first: *.starts-with('c');
say @list.first: *.starts-with('b');
say @list.first: *.starts-with('g');
```

Returns the only element in LIST for which BLOCK evaluates to true. Each element in LIST is passed to BLOCK in turn. Returns `Nil`

if no such element has been found.

```
my @list = <alpha beta cicero bearing effortless>;
say onlyval { .starts-with('c') }, @list; # cicero
say onlyval { .starts-with('b') }, @list; # Nil, because twice
say onlyval { .starts-with('g') }, @list; # Nil, because never
```

`only_value`

is an alias for `onlyval`

.

Returns the last value in LIST for which BLOCK evaluates to true. Each element in LIST is passed to BLOCK in turn. Returns `Nil`

if no such element has been found.

```
my @list = <alpha beta cicero bearing effortless>;
say lastval { .starts-with('c') }, @list; # cicero
say lastval { .starts-with('b') }, @list; # bearing
say lastval { .starts-with('g') }, @list; # Nil, because never
```

`last_value`

is an alias for `lastval`

.

```
say @list.first: *.starts-with('c'), :end;
say @list.first: *.starts-with('b'), :end;
say @list.first: *.starts-with('g'), :end;
```

Returns the result of BLOCK for the first element in LIST for which BLOCK evaluates to true. Each element of LIST is passed to BLOCK in turn. Returns `Nil`

if no such element has been found.

```
my @list = <alpha beta cicero bearing effortless>;
say firstres { .uc if .starts-with('c') }, @list; # CICERO
say firstres { .uc if .starts-with('b') }, @list; # BETA
say firstres { .uc if .starts-with('g') }, @list; # Nil, because never
```

`first_result`

is an alias for `firstres`

.

Returns the result of BLOCK for the first element in LIST for which BLOCK evaluates to true. Each element of LIST is passed to BLOCK in turn. Returns `Nil`

if no such element has been found.

```
my @list = <alpha beta cicero bearing effortless>;
say onlyres { .uc if .starts-with('c') }, @list; # CICERO
say onlyres { .uc if .starts-with('b') }, @list; # Nil, because twice
say onlyres { .uc if .starts-with('g') }, @list; # Nil, because never
```

`only_result`

is an alias for `onlyres`

.

Returns the result of BLOCK for the last element in LIST for which BLOCK evaluates to true. Each element of LIST is passed to BLOCK in turn. Returns `Nil`

if no such element has been found.

```
my @list = <alpha beta cicero bearing effortless>;
say lastval { .uc if .starts-with('c') }, @list; # CICERO
say lastval { .uc if .starts-with('b') }, @list; # BEARING
say lastval { .uc if .starts-with('g') }, @list; # Nil, because never
```

`last_result`

is an alias for `lastres`

.

Evaluates BLOCK for each element in LIST (passed to BLOCK as the parameter) and returns a list of the indices of those elements for which BLOCK returned a true value. This is just like `grep`

only that it returns indices instead of values:

```
my @x = indexes { $_ %% 2 } (1..10); # returns (1, 3, 5, 7, 9)
```

```
my @x = (1..10).grep: * %% 2, :k;
```

Returns the index of the first element in LIST for which the criterion in BLOCK is true. Passes each element in LIST to BLOCK in turn:

```
my @list = 1, 4, 3, 2, 4, 6;
printf "item with index %i in list is 4", firstidx { $_ == 4 }, @list;
===============================
item with index 1 in list is 4
```

Returns `-1`

if no such item could be found.

```
my @list = 1, 3, 4, 3, 2, 4;
print firstidx { $_ == 3 }, @list; # 1
print firstidx { $_ == 5 }, @list; # -1, because not found
```

`first_index`

is an alias for `firstidx`

.

```
printf "item with index %i in list is 4", @list.first: * == 4, :k;
print @list.first: * == 3, :k;
print @list.first( * == 5, :k) // -1; # not found == Nil
```

Returns the index of the only element in LIST for which the criterion in BLOCK is true. Passes each element in LIST to BLOCK in turn:

```
my @list = 1, 3, 4, 3, 2, 4;
printf "uniqe index of item 2 in list is %i", onlyidx { $_ == 2 }, @list;
===============================
unique index of item 2 in list is 4
```

Returns `-1`

if either no such item or more than one of these has been found.

```
my @list = 1, 3, 4, 3, 2, 4;
print onlyidx { $_ == 3 }, @list; # -1, because more than once
print onlyidx { $_ == 5 }, @list; # -1, because not found
```

`only_index`

is an alias for `onlyidx`

.

Returns the index of the last element in LIST for which the criterion in BLOCK is true. Passes each element in LIST to BLOCK in turn:

```
my @list = 1, 4, 3, 2, 4, 6;
printf "item with index %i in list is 4", lastidx { $_ == 4 } @list;
==================================
item with index 4 in list is 4
```

Returns `-1`

if no such item could be found.

```
my @list = 1, 3, 4, 3, 2, 4;
print lastidx { $_ == 3 }, @list; # 3
print lastidx { $_ == 5 }, @list; # -1, because not found
```

`last_index`

is an alias for `lastidx`

.

```
printf "item with index %i in list is 4", @list.first: * == 4, :k, :end;
print @list.first: * == 3, :k, :end;
print @list.first( * == 5, :k, :end) // -1; # not found == Nil
```

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

```
my @sorted = sort_by { .name }, @people;
```

The key function is being passed each value in turn, The values are then sorted according to string comparisons on the values returned. This is equivalent to:

```
my @sorted = sort -> $a, $b { $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:

```
my @sorted = sort_by { S:g/ (\d+) / { sprintf "%09d", $0 } / }, @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.

```
my @sorted = @people.sort: *.name;
```

Similar to `sort_by`

but compares its key values numerically.

```
my @sorted = <10 1 20 42>.sort: +*;
```

This sorts the given array **in place** using the given compare code. The Raku version uses the basic sort functionality as provided by the `sort`

built-in function.

```
@people .= sort;
```

Performs a binary search on LIST which must be a sorted list of values. BLOCK receives each element in turn and must return a negative value if the element is smaller, a positive value if it is bigger and zero if it matches.

Returns a boolean value if the `Scalar`

named parameter is specified. Otherwise it returns a single element list if it was found, or the empty list if none of the calls to BLOCK returned `0`

.

```
my @list = <alpha beta cicero delta>;
my @found = bsearch { $_ cmp "cicero" }, @list; # ("cicero",)
my @found = bsearch { $_ cmp "effort" }, @list; # ()
my @list = <alpha beta cicero delta>;
my $found = bsearch Scalar, { $_ cmp "cicero" }, @list; # True
my $found = bsearch Scalar, { $_ cmp "effort" }, @list; # False
```

Performs a binary search on LIST which must be a sorted list of values. BLOCK receives each element in turn and must return a negative value if the element is smaller, a positive value if it is bigger and zero if it matches.

Returns the index of found element, otherwise `-1`

.

```
my @list = <alpha beta cicero delta>;
my $found = bsearchidx { $_ cmp "cicero" }, @list; # 2
my $found = bsearchidx { $_ cmp "effort" }, @list; # -1
```

`bsearch_index`

is an alias for `bsearchidx`

.

Returns the index of the first element in LIST which does not compare *less than val*. Technically it's the first element in LIST which does not return a value below zero when passed to BLOCK.

```
my @ids = 1, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6;
my $lb = lower_bound { $_ <=> 2 }, @ids; # 1
my $lb = lower_bound { $_ <=> 4 }, @ids; # 9
```

Returns the index of the first element in LIST which does not compare *greater than val*. Technically it's the first element in LIST which does not return a value below or equal to zero when passed to BLOCK.

```
my @ids = 1, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6;
my $ub = upper_bound { $_ <=> 2 }, @ids; # 3
my $ub = upper_bound { $_ <=> 4 }, @ids; # 13
```

Returns a list of indices containing the `lower_bound`

and the `upper_bound`

of given BLOCK and LIST.

```
my @ids = 1, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6;
my $er = equal_range { $_ <=> 2 }, @ids; # (1,3)
my $er = equal_range { $_ <=> 4 }, @ids; # (9,13)
```

Performs a binary search on LIST which must be a sorted list of values. BLOCK must return a negative value if the current element (passed as a parameter to the BLOCK) is smaller, a positive value if it is bigger and zero if it matches.

ITEM is inserted at the index where the ITEM should be placed (based on above search). That means, it's inserted before the next bigger element.

```
my @l = 2,3,5,7;
binsert { $_ <=> 4 }, 4, @l; # @l = (2,3,4,5,7)
binsert { $_ <=> 6 }, 42, @l; # @l = (2,3,4,5,42,7)
```

You take care that the inserted element matches the compare result.

`bsearch_insert`

is an alias for `binsert`

.

Performs a binary search on LIST which must be a sorted list of values. BLOCK must return a negative value if the current element (passed as a parameter to the BLOCK) is smaller, a positive value if it is bigger and zero if it matches.

The item at the found position is removed and returned.

```
my @l = 2,3,4,5,7;
bremove { $_ <=> 4 }, @l; # @l = (2,3,5,7);
```

`bsearch_remove`

is an alias for `bremove`

.

Counts the number of elements in LIST for which the criterion in BLOCK is true. Passes each item in LIST to BLOCK in turn:

```
printf "%i item(s) are defined", true { defined($_) }, @list;
```

```
print "@list.grep(*.defined).elems() item(s) are defined";
```

Counts the number of elements in LIST for which the criterion in BLOCK is false. Passes each item in LIST to BLOCK in turn:

```
printf "%i item(s) are not defined", false { defined($_) }, @list;
```

```
print "@list.grep(!*.defined).elems() item(s) are not defined";
```

Reduce LIST by calling BLOCK in scalar context for each element of LIST. The first parameter contains the progressional result and is initialized with **0**. The second parameter contains the currently being processed element of LIST.

```
my $reduced = reduce_0 -> $a, $b { $a + $b }, @list;
```

In the Perl version, `$_`

is also set to the index of the element being processed. This is not the case in the Raku version for various reasons. Should you need the index value in your calculation, you can post-increment the anonymous state variable instead: `$++`

:

```
my $reduced = reduce_0 -> $a, $b { dd $++ }, @list; # 0 1 2 3 4 5 ...
```

The idea behind reduce_0 is **summation** (addition of a sequence of numbers).

Reduce LIST by calling BLOCK in scalar context for each element of LIST. The first parameter contains the progressional result and is initialized with **1**. The second parameter contains the currently being processed element of LIST.

```
my $reduced = reduce_1 -> $a, $b { $a * $b }, @list;
```

In the Perl version, `$_`

is also set to the index of the element being processed. This is not the case in the Raku version for various reasons. Should you need the index value in your calculation, you can post-increment the anonymous state variable instead: `$++`

:

```
my $reduced = reduce_1 -> $a, $b { dd $++ }, @list; # 0 1 2 3 4 5 ...
```

The idea behind reduce_1 is **product** of a sequence of numbers.

Reduce LIST by calling BLOCK in scalar context for each element of LIST. The first parameter contains the progressional result and is initialized with **Any**. The second parameter contains the currently being processed element of LIST.

```
my $reduced = reduce_u -> $a, $b { $a.push($b) }, @list;
```

In the Perl version, `$_`

is also set to the index of the element being processed. This is not the case in the Raku version for various reasons. Should you need the index value in your calculation, you can post-increment the anonymous state variable instead: `$++`

:

```
my $reduced = reduce_u -> $a, $b { dd $++ }, @list; # 0 1 2 3 4 5 ...
```

The idea behind reduce_u is to produce a list of numbers.

Calculates the minimum and maximum of LIST and returns a two element list with the first element being the minimum and the second the maximum. Returns the empty list if LIST was empty.

```
my ($min,$max) = minmax (43,66,77,23,780); # (23,780)
```

```
my $range = <43,66,77,23,780>.minmax( +* );
my $range = (43,66,77,23,780).minmax; # auto-numerically compares
```

Computes the minimum and maximum of LIST using string compare and returns a two element list with the first element being the minimum and the second the maximum. Returns the empty list if LIST was empty.

```
my ($min,$max) = minmaxstr <foo bar baz zippo>; # <bar zippo>
```

```
my $range = (43,66,77,23,780).minmax( ~* );
my $range = <foo bar baz zippo>.minmax; # auto-string compares
```

List::Util, List::AllUtils, List::UtilsBy

Thanks to all of the individuals who have contributed to the Perl version of this module.

Elizabeth Mattijsen [email protected]

Source can be located at: https://github.com/lizmat/List-MoreUtils . Comments and Pull Requests are welcome.

Copyright 2018-2020 Elizabeth Mattijsen

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.

Re-imagined from the Perl version as part of the CPAN Butterfly Plan. Perl version originally developed by Tassilo von Parseval, subsequently maintained by Adam Kennedy and Jens Rehsack.