Methods by type
Indexed list access
FullWork 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)
1grep(:k) gives every matching index; first(:k) gives the first.
Notes #
- The adverb family is
:k(keys/indices),:v(values, the default),:kv(both), and:p(pairs) — they work ongrep,first, and hash access alike. .pairson a list givesindex => value(the opposite order to.antipairs)..kvon a hash yields key/value pairs instead of index/value — the same method, keyed by the container type.