indexed

Pairs each element with its (0-based) index: (index, value).

Iterable<(int, A)> indexed<A>(Iterable<A> iterable) FxAsyncIterable<(int, A)> indexedAsync<A>(FxAsyncIterable<A> iterable) Iterable<(int, T)> Fx.indexed // sync chain: inherited Iterable getter, no parens FxAsync<(int, T)> FxAsync.indexed() // async chain Iterable<(int, A)> zipWithIndex<A>(Iterable<A> iterable) // FxTS alias

Lecture

indexed is zip against an implicit counter: each element comes out paired as (index, value), counting up from 0. It's the lazy-pipeline answer to a manual for (var i = 0; i < list.length; i++) loop — no counter variable to manage, and it composes with the rest of the chain. indexed is the Dart-idiomatic name (it mirrors Iterable.indexed); fxdart also accepts the FxTS spelling zipWithIndex — they're the same operator.

Because it only needs to track a running counter, indexed stays lazy and works over an infinite source just as well as a finite one; the async form counts the same way as elements resolve.

Demo 1 · Basics

Demo 2 · Async

Try it yourself

Exercise: pair each runner with their (0-based) finishing position.

Related: zip — pair two iterables together · zipWith — zip and combine in one step · entries — pair Map keys with values