UNPKG

@thi.ng/transducers

Version:

Collection of ~170 lightweight, composable transducers, reducers, generators, iterators for functional data transformations

25 lines (24 loc) 596 B
import { isIterable } from "@thi.ng/checks/is-iterable"; import { iterator } from "./iterator.js"; import { isReduced } from "./reduced.js"; function padLast(n, fill, src) { return isIterable(src) ? iterator(padLast(n, fill), src) : ([init, complete, reduce]) => { let m = 0; return [ init, (acc) => { let rem = m % n; if (rem > 0) { while (++rem <= n && !isReduced(acc)) { acc = reduce(acc, fill); } } return complete(acc); }, (acc, x) => (m++, reduce(acc, x)) ]; }; } export { padLast };