@thi.ng/transducers-async
Version:
Async versions of various highly composable transducers, reducers and iterators
17 lines (16 loc) • 457 B
JavaScript
import { ensureReduced, reduced } from "@thi.ng/transducers/reduced";
import { compR } from "./compr.js";
import { iterator } from "./iterator.js";
function take(n, src) {
return src ? iterator(take(n), src) : (rfn) => {
const reduce = rfn[2];
let remaining = n;
return compR(
rfn,
async (acc, x) => --remaining > 0 ? reduce(acc, x) : remaining === 0 ? ensureReduced(reduce(acc, x)) : reduced(acc)
);
};
}
export {
take
};