@thi.ng/transducers-async
Version:
Async versions of various highly composable transducers, reducers and iterators
20 lines (19 loc) • 503 B
JavaScript
import { asyncIdentity } from "@thi.ng/api/async";
import { isReduced } from "@thi.ng/transducers/reduced";
async function reduce([init, complete, $reduce], ...args) {
let acc = args.length < 2 ? await init() : args.shift();
for await (let x of args[0]) {
const y = await $reduce(acc, x);
if (isReduced(y)) {
acc = y.deref();
break;
}
acc = y;
}
return await complete(acc);
}
const reducer = (init, rfn) => [init, asyncIdentity, rfn];
export {
reduce,
reducer
};