Built-in routines

map / grep / sort

Full

Transform, filter, and order a list by passing it a block.

The workhorse list routines each take a block and apply it across a list: map transforms every element, grep keeps the ones that match, and sort orders them.

map — transform #

say (1..5).map(* * 2);
Output
(2 4 6 8 10)

* * 2 is a Whatever block — a one-argument function that doubles its input.

grep — filter #

grep keeps elements for which the block is true. Here * %% 2 tests divisibility by two.

say (1..10).grep(* %% 2);
Output
(2 4 6 8 10)

sort — order #

With no argument, sort orders by the elements' natural cmp. Pass a block to sort by a computed key instead.

say <banana apple cherry>.sort;
say <ccc a bb>.sort(*.chars);
Output
(apple banana cherry)
(a bb ccc)

Notes #