effect-ts-folds
Version:
Recursion schemes for effect-ts.
25 lines • 975 B
JavaScript
import { Array as AR, flow, Number as NU, Option as OP, pipe } from 'effect';
/** Transpose a list of arrays. */
export const transpose = ([head, ...tail]) => head === undefined
? []
: AR.isNonEmptyArray(tail)
? pipe([head, ...tail], padSuffix, transposePadded, AR.map(AR.getSomes))
: pipe(head, AR.map(AR.of));
const padSuffix = (xss) => {
const n = pipe([0, ...AR.map(xss, AR.length)], AR.max(NU.Order)) - 1;
const f = (xs) => n <= 1
? xs
: [
...xs,
...(n === xs.length - 1
? []
: AR.replicate(OP.none(), n - xs.length)),
];
return pipe(xss, AR.map(flow(AR.map(OP.some), f)));
};
const transposePadded = ([first, second, ...rest]) => first === undefined
? []
: second === undefined
? pipe(first, AR.map(AR.of))
: pipe(rest, AR.reduce(AR.zip(first, second), (p, c) => AR.zipWith(p, c, (AR.append))));
//# sourceMappingURL=Array.js.map