Methods by type
min / max / minmax
FullThe least and greatest element — by natural order or a computed key.
.min and .max return the extreme elements; with a block they compare by a computed key. .minmax returns both ends at once as a range.
By a computed key #
Pass a key extractor to compare by something other than the value — here, string length.
say <bb a ccc>.min(*.chars); say <bb a ccc>.max(*.chars);
Output
a
ccca is shortest, ccc longest — the comparison is on .chars, not alphabetical order.
minmax — both ends #
.minmax returns a Range from the least to the greatest element.
say (3, 1, 2).minmax;
Output
1..3Notes #
- With no argument,
.min/.maxuse the elements' naturalcmporder (numeric for numbers, alphabetic for strings). .minmaxis one pass over the data — cheaper than calling.minand.maxseparately when you need both.- These pair with
.sort(which takes the same key-extractor block) — see map / grep / sort.