Skip to content

Instantly share code, notes, and snippets.

@matyasfodor
Created August 9, 2019 16:11
Show Gist options
  • Save matyasfodor/cfccde33d113a5e84c573f537788f476 to your computer and use it in GitHub Desktop.
Save matyasfodor/cfccde33d113a5e84c573f537788f476 to your computer and use it in GitHub Desktop.
const _zip = <T>(func: IArrayMethodSignature<IteratorResult<T>>, args: T[][]) => {
const iterators = args.map(arr => arr[Symbol.iterator]());
let iterateInstances = iterators.map((i) => i.next());
const ret: T[][] = [];
while (func.bind(iterateInstances, ((it: IteratorResult<T>) => !it.done))) {
ret.push(iterateInstances.map(it => it.value));
iterateInstances = iterators.map((i) => i.next());
}
return ret;
}
export const zipShort = <T>(...args: T[][]) => _zip(Array.prototype.every, args);
export const zipLong = <T>(...args: T[][]) => _zip(Array.prototype.some, args);
zipShort([1, 2, 3], [4, 5, 6, 7])
// [
// [ 1, 4 ],
// [ 2, 5 ],
// [ 3, 6 ] ]
zipLong([1, 2, 3], [4, 5, 6, 7])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment