Skip to content

Instantly share code, notes, and snippets.

@Announcement
Forked from slikts/pipe.ts
Last active February 23, 2018 15:19
Show Gist options
  • Save Announcement/df57adc0ad17e4888d6c3450964c671b to your computer and use it in GitHub Desktop.
Save Announcement/df57adc0ad17e4888d6c3450964c671b to your computer and use it in GitHub Desktop.
moar tipezzzz
const pipe = <T, K>(
iterable: IterableIterator<T>,
seed: K,
fn: (a: T) => K
): T => {
const iterator: IterableIterator<T> = iterable[Symbol.iterator]()
if (iterable instanceof GeneratorFunction) {
iterator.next()
}
let result: IteratorResult<T> = iterator.next(seed)
while (result.done === false) {
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