Skip to content

Instantly share code, notes, and snippets.

@jaawerth
Forked from slikts/pipe.ts
Created February 23, 2018 15:12
Show Gist options
  • Save jaawerth/a6b30d5fcd95e44fbfa65e1942417747 to your computer and use it in GitHub Desktop.
Save jaawerth/a6b30d5fcd95e44fbfa65e1942417747 to your computer and use it in GitHub Desktop.
const pipe = <T, K>(
iterable: IterableIterator<T>,
seed: K,
fn: (a: T) => K
): T => {
const iterator = iterable[Symbol.iterator]()
if (iterable instanceof GeneratorFunction) {
iterator.next()
}
let result = iterator.next(seed)
while (!result.done) {
result = iterator.next(fn(result.value))
}
return result.value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment