💾*zip()
JS ⟩ objects ⟩ built-in ⟩ Generator ⟩ generator function ⟩ compostion ⟩ *zip()
zip a list of iterables into a single generator function.
replit:zip(...iterables)
// ⭐ zip()
// zip a list of iterables into a single generator function.
function* zip(...iterables) {
// iterables -> iterators
let iterators = iterables.map(it => it[Symbol.iterator]());
// iterators -> iteration results
let results = iterators.map(it => it.next());
let index = 0;
// while some results not done, yield values
while (results.some(result => !result.done)) {
yield results.map(result => result.value);
results = iterators.map(it => it.next());
}
// all results done
return;
}
// export
module.exports = zip;💈範例:
require: Iterator, *integers()
zip() is like the mat.transpose() of a matrix.
*interleave() is similar to but different from zip() .
Last updated
Was this helpful?