Methods by type

Indexed list access

Full

Work with positions — kv, antipairs, and the :k adverb on grep/first.

Several methods surface a list's indices alongside (or instead of) its values — for iterating with a counter, or finding where something is rather than what.

kv and antipairs #

.kv flattens to alternating index/value; .antipairs returns value => index pairs.

say <a b c>.kv;
say <a b c>.antipairs;
Output
(0 a 1 b 2 c)
(a => 0 b => 1 c => 2)

.kv is what for @a.kv -> $i, $v { … } iterates.

Indices with :k #

The :k adverb makes grep and first return positions instead of values.

say (10, 20, 30).grep(* > 15, :k);
say (10, 20, 30).first(* > 15, :k);
Output
(1 2)
1

grep(:k) gives every matching index; first(:k) gives the first.

Notes #