Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created April 25, 2017 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/e613b430b0ed6ad833d1b02e74e138f2 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/e613b430b0ed6ad833d1b02e74e138f2 to your computer and use it in GitHub Desktop.
'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