Open wellcaffeinated opened 2 years ago
Would this be the same as filter
?
And thank you 😊
I don't think it's the same as filter because this would terminate the stream early, allowing for early collection, or ability to exit out of a find algorithm.
For example:
This could exit early once charmander is found.
const pm = await pipeline(
getPokemon(),
takeWhile(pokemon => pokemon.name !== 'charmander', true),
take(-1), // this would also be great to implement btw
collect
)
// => pm[0].name === 'charmander'
This would have to cycle through all 720 pokemon before exiting
const pm = await pipeline(
getPokemon(),
filter(pokemon => pokemon.name === 'charmander'),
collect
)
Generally i think implementing some of the methods that rust's Iterator has would be great: https://doc.rust-lang.org/core/iter/trait.Iterator.html
Or perhaps looking through the list of operators that rxjs provides: https://rxjs.dev/api?type=function
Well stated, thank you. Yeah this makes a lot of sense. I'd happily take a pr if you're interested.
I'll dig into the rust docs, but I'm also happy to take anything useful.
What would take(-1)
do? Pulling from the end of an iterable is tough when it's async, requires buffering some or all of the data depending on the operation. I've purposely not put anything in that would accidentally cause a huge performance issue.
Absolutely, i don't think you'd want to buffer much. I'm imagining that it would effectively release the data it doesn't need. Maybe a take(-n)
could look like:
const takeRight = (count) => async function* (iterable){
const buffer = []
for await (const res of iterable) {
buffer.push(res)
if (buffer.length > count){ buffer.shift() }
}
while (buffer.length){ yield buffer.pop() }
}
const take = (n) => {
if (n < 0) { return takeRight(-n) }
else { return take(n) }
}
I'd be happy to submit some pull requests
hey there. Great library!
A
takeWhile()
operator would be great.Something like this?