ðŸ“˜ Finding minimum and maximum using Perl 6

# ðŸ“˜ Finding minimum and maximum using Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.

Find the minimum and the maximum numbers in the given list of integers.

Finding the minimum and maximum elements of arrays is extremely easy in PerlÂ 6. ForÂ iterable objects, the two methods,Â `min` andÂ `max`, are defined.

`my @list = 7, 6, 12, 3, 4, 10, 2, 5, 15, 6, 7, 8, 9, 3;say @list.min;say @list.max;`

For the list in the example, this program prints two numbers:

`215`

TheÂ `min` andÂ `max` routines can be used as binary operators, in which case they return the smaller or the bigger number:

`say 7 min 8; # Prints 7say 7 max 8; # Prints 8`

Operators can be chained like this:

`say 7 min 5 min 8; # Prints 5say 7 max 5 max 8; # Prints 8`

This can be expressed with the help of a reduction meta-operatorÂ `[â€¦]`:

`say [min] 7, 9, 5;Â # Prints 5say [max] 7, 9, 4;Â # Prints 9`

Lists are also accepted, for example:Â `say [min] @list`.