loading...

re: Lazy Evaluation in JavaScript with Generators, Map, Filter, and Reduce VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Well done! But wouldn't it make even more sense when the whole statement would be executed one item by one. So next() wouldn't gather all the respo...
 

Thanks for your comment! While this code is lazy, it's still completely synchronous. So once take is called, it has to run to completion - calling next only processes one item, but I think you meant to say take. I wonder if you mean that this code could be implemented asynchronously. I did implement a simple asynchronous reduce for another article:

const asyncReduce = async function* (iterable, reducer, accumulator) {
    for await (const item of iterable) {
        const reductionResult = reducer(item, accumulator)

        accumulator = reductionResult

        yield reductionResult
    }
}
code of conduct - report abuse