Created
April 25, 2017 23:26
-
-
Save mattpodwysocki/e613b430b0ed6ad833d1b02e74e138f2 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
'use strict'; | |
import { map } from './map'; | |
function* _zip<T>(source: Iterable<T>[]): Iterable<T[]> { | |
for (let i = 0; i < source.length; i++) { | |
let its = source.map(x => x[Symbol.iterator]()); | |
let nexts = its.map(it => it.next()); | |
if (nexts.every(next => !next.done)) { | |
yield nexts.map(next => next.value); | |
} | |
} | |
} | |
export function zip<T>(source: Iterable<T>, ...args: Iterable<T>[]): Iterable<T[]> { | |
return _zip<T>([source].concat(...args)); | |
} | |
export function zipStatic<T>(...args: Iterable<T>[]): Iterable<T[]> { | |
return _zip<T>(args); | |
} | |
export function zipWith<T, TResult>( | |
source: Iterable<T>, | |
fn: (...args: T[]) => TResult, | |
...args: Iterable<T>[]): Iterable<TResult> { | |
return map(_zip([source].concat(...args)), zipArgs => fn(...zipArgs)); | |
} | |
export function zipWithStatic<T, TResult>( | |
fn: (...args: T[]) => TResult, | |
...args: Iterable<T>[]): Iterable<TResult> { | |
return map<T[], TResult>(_zip<T>(args), zipArgs => fn(...zipArgs)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment