Created
August 9, 2019 16:11
-
-
Save matyasfodor/cfccde33d113a5e84c573f537788f476 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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