Esta página ainda não foi traduzida, por isso é exibida em inglês. Ajude a traduzir

transpose

Turns a collection of rows into a collection of columns.

Iterable<List<A>> transpose<A>(Iterable<Iterable<A>> rows) FxAsyncIterable<List<A>> transposeAsync<A>(Iterable<FxAsyncIterable<A>> rows)

Lecture

transpose is zip generalized to any number of iterables: the n-th output list holds the n-th element of every input row that still has one. Where zip/zip3 take each row as a separate argument (because Dart can't express variadic generics), transpose takes all the rows as a single iterable of iterables — transpose([row1, row2, row3]) — so it works for any row count decided at runtime.

Unlike zip, which stops as soon as the shortest input runs dry, transpose keeps going as long as any row still has values left — a shorter row just stops contributing to later output lists, which may end up shorter than the row count. It only stops entirely once every row is exhausted.

Demo 1 · Basics & ragged rows

Demo 2 · Async

Try it yourself

Exercise: transpose the matrix (rows become columns).

Related: zip — the two-row special case · chunk — batch a flat source into rows first · reverse