Last active
February 23, 2018 10:17
-
-
Save slikts/8909ddaab1714e1490cd427690f4177b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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