I would like to use the index of the array in a map routine. For example, this Raku code:
raku -e 'my @a = "First", "Second", "Third", "First"; say @a.map({ "Index of $_ is: ;" })'
prints:
(Index of First is: ; Index of Second is: ; Index of Third is: ; Index of First is: ;)
Would it be possible to get the index of the array, like:
(Index of First is: 0; Index of Second is: 1; Index of Third is: 2; Index of First is: 3;)
Thanks!
There is
.kvto produce "keys" and values. For an array, the keys are the indexes 0, 1, 2...This is a flat sequence of length 2N; we can tell
mapto take 2 things at a time, either via an explicit signature:or via placeholder variables:
Since index comes lexicographically before value, it works out as in the previous explicit signature case, i.e., the same signature is produced for us behind the scenes. We can see this:
Note that what matters is the Unicode order of variable names, not the order of appearance.
In the spirit of TIMTOWDI, some alternatives (IMO not better):
.pairs, cousine of.kvto produce "key => value" pairs instead of a flat listThis last one is the least idiomatic I think.