for @array Z (0..*) -> $item, $count {
say "$item occured at position $count";
}
An array is (lazily) zipped with a (lazy) infinite array (1, 2, 3 Z 'a', 'b', 'c' evaluates to (1, 'a'), (2, 'b', (3, 'c'), in list context it is flattened (1, 'a', 2, 'b', 3, 'c') and the two loop variables eat up two elements per iteration.
Note that
- It is not much to type
- the counter is scoped to the loop block
- you can easily and intuitivly set the lower boundary of the counter
- you can choose arbitrary for the counter
- You only do that when you really need a counter, so no runtime penalties for loops without counter
- You can omit the ->$item, $counter part and use self declaring parameters with the ^ twigil to save typing
Hooray for Perl 6!
| [reply] [d/l] [select] |
Why dont you implement an iterator similar to
Python's
enumerate() function?
Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality
| [reply] [d/l] |