Skip to content

Instantly share code, notes, and snippets.

@slikts
Last active February 23, 2018 10:17
Show Gist options
  • Save slikts/8909ddaab1714e1490cd427690f4177b to your computer and use it in GitHub Desktop.
Save slikts/8909ddaab1714e1490cd427690f4177b to your computer and use it in GitHub Desktop.
const mapUntil = function*<T, K>(
fn: (a: T) => K,
p: (a: K) => boolean,
xs: T[]
): IterableIterator<K> {
for (const x of xs) {
const result = fn(x)
if (!p(result)) {
break
}
yield result
}
}
const asyncMapUntil = async function*<T, K>(
fn: (a: T) => K,
p: (a: K) => boolean,
xs: T[]
): AsyncIterableIterator<K> {
for (const x of xs) {
const result = await fn(x)
if (!p(result)) {
break
}
yield result
}
}
const asyncConsume = async <T>(xs: AsyncIterableIterator<T>) => {
const results = []
for await (const x of xs) {
results.push(x)
}
return results
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment