Skip to content

Instantly share code, notes, and snippets.

@slikts
Last active August 8, 2018 13:19
Show Gist options
  • Save slikts/f1d0874b4ba11e6207401a2bcfdf56f3 to your computer and use it in GitHub Desktop.
Save slikts/f1d0874b4ba11e6207401a2bcfdf56f3 to your computer and use it in GitHub Desktop.
const pipe = <A, B>(
iterable: IterableIterator<A>,
seed: B,
fn: (a: A) => B
): A => {
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
}
@slikts
Copy link
Author

slikts commented Feb 23, 2018

GeneratorFunction = (function*(){}).constructor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment