UNPKG

fp-ts-std

Version:

The missing pseudo-standard library for fp-ts.

139 lines (138 loc) 7.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.separateNE = exports.anyM = exports.allM = exports.toReadonly = exports.fromReadonly = exports.fromIterable = exports.extractAt = exports.filterA = exports.zipAll = exports.maximum = exports.minimum = exports.reduceRightWhile = exports.reduceWhile = exports.symmetricDifference = exports.takeRightWhile = exports.transpose = exports.dropAt = exports.dropRightWhile = exports.countBy = exports.moveTo = exports.moveFrom = exports.reject = exports.slice = exports.aperture = exports.median = exports.mean = exports.product = exports.sum = exports.cartesian = exports.without = exports.endsWith = exports.startsWith = exports.dropRepeats = exports.insertMany = exports.upsert = exports.pluckFirst = exports.getDisorderedEq = exports.join = exports.none = exports.elemV = void 0; const A = require("fp-ts/Array"); const Foldable_1 = require("fp-ts/Foldable"); const Monoid_1 = require("fp-ts/Monoid"); const NEA = require("fp-ts/NonEmptyArray"); const O = require("fp-ts/Option"); const Ordering_1 = require("fp-ts/Ordering"); const Predicate_1 = require("fp-ts/Predicate"); const RA = require("fp-ts/ReadonlyArray"); const R = require("fp-ts/Record"); const Semigroup_1 = require("fp-ts/Semigroup"); const T = require("fp-ts/These"); const B = require("fp-ts/boolean"); const function_1 = require("fp-ts/function"); const number_1 = require("fp-ts/number"); const Function_1 = require("./Function"); const elemV = (eq) => (xs) => y => A.elem(eq)(y)(xs); exports.elemV = elemV; exports.none = (0, function_1.flow)(Predicate_1.not, p => A.every(p)); const join = (x) => (0, Function_1.invoke)("join")([x]); exports.join = join; const getDisorderedEq = (ordA) => ({ equals: (xs, ys) => { const sort = A.sort(ordA); return A.getEq(ordA).equals(sort(xs), sort(ys)); }, }); exports.getDisorderedEq = getDisorderedEq; const pluckFirst = (p) => (xs) => (0, function_1.pipe)(A.findIndex(p)(xs), O.fold((0, function_1.constant)([O.none, xs]), i => [ O.some(xs[i]), A.unsafeDeleteAt(i, xs), ])); exports.pluckFirst = pluckFirst; const upsert = (eqA) => (x) => (ys) => (0, function_1.pipe)(A.findIndex(y => eqA.equals(x, y))(ys), O.map(i => A.unsafeUpdateAt(i, x, ys)), O.chain(NEA.fromArray), O.getOrElse(() => A.append(x)(ys))); exports.upsert = upsert; const insertMany = (i) => (xs) => (ys) => (0, function_1.pipe)(xs, A.reverse, (0, Foldable_1.reduceM)(O.Monad, A.Foldable)(ys, (zs, x) => (0, function_1.pipe)(zs, A.insertAt(i, x))), O.chain(NEA.fromArray)); exports.insertMany = insertMany; const dropRepeats = eq => xs => (0, function_1.pipe)(xs, A.filterWithIndex((i, x) => i === 0 || !eq.equals(x, xs[i - 1]))); exports.dropRepeats = dropRepeats; const startsWith = (eq) => (start) => (0, function_1.flow)(A.takeLeft(start.length), xs => A.getEq(eq).equals(xs, start)); exports.startsWith = startsWith; const endsWith = (eq) => (end) => (0, function_1.flow)(A.takeRight(end.length), xs => A.getEq(eq).equals(xs, end)); exports.endsWith = endsWith; const without = (eq) => (xs) => A.filter(y => !A.elem(eq)(y)(xs)); exports.without = without; const cartesian = (xs) => (ys) => (0, function_1.pipe)(xs, A.chain(x => (0, function_1.pipe)(ys, A.map(y => [x, y])))); exports.cartesian = cartesian; exports.sum = (0, Monoid_1.concatAll)(number_1.MonoidSum); exports.product = (0, Monoid_1.concatAll)(number_1.MonoidProduct); const mean = (xs) => (0, exports.sum)(xs) / xs.length; exports.mean = mean; exports.median = (0, function_1.flow)(NEA.sort(number_1.Ord), xs => { const i = xs.length / 2; return i % 1 === 0 ? (xs[i - 1] + xs[i]) / 2 : xs[Math.floor(i)]; }); const aperture = (n) => (xs) => { const go = (i) => (ys) => i + n > xs.length ? ys : go(i + 1)(A.append((0, exports.slice)(i)(n + i)(xs))(ys)); return n < 1 ? [] : go(0)([]); }; exports.aperture = aperture; const slice = (start) => (end) => (0, Function_1.invoke)("slice")([start, end]); exports.slice = slice; const reject = (f) => A.filter((0, Predicate_1.not)(f)); exports.reject = reject; const moveFrom = (from) => (to) => (xs) => from >= xs.length || to >= xs.length ? O.none : from === to ? O.some(xs) : (0, function_1.pipe)(xs, A.lookup(from), O.chain(x => (0, function_1.pipe)(A.deleteAt(from)(xs), O.chain(A.insertAt(to, x))))); exports.moveFrom = moveFrom; exports.moveTo = (0, function_1.flip)(exports.moveFrom); const countBy = (f) => (xs) => R.fromFoldableMap(number_1.MonoidSum, A.Foldable)(xs, x => [f(x), 1]); exports.countBy = countBy; const dropRightWhile = (f) => (0, function_1.flow)(A.reverse, A.dropLeftWhile(f), A.reverse); exports.dropRightWhile = dropRightWhile; const dropAt = (i) => (n) => (xs) => (0, function_1.pipe)(A.isOutOfBound(i, xs), B.fold(() => (0, function_1.pipe)(A.copy(xs), ys => { ys.splice(i, n); return ys; }, O.some), (0, function_1.constant)(O.none))); exports.dropAt = dropAt; const transpose = (xs) => { if (A.isEmpty(xs)) return []; if (A.isEmpty(xs[0])) return (0, exports.transpose)(A.dropLeft(1)(xs)); const [[y, ...ys], ...yss] = xs; const zs = [y, ...A.filterMap(A.head)(yss)]; const zss = [ys, ...A.map(A.dropLeft(1))(yss)]; return [zs, ...(0, exports.transpose)(zss)]; }; exports.transpose = transpose; const takeRightWhile = (f) => (0, function_1.flow)(A.reverse, A.takeLeftWhile(f), A.reverse); exports.takeRightWhile = takeRightWhile; const symmetricDifference = (eq) => (xs) => ys => A.getMonoid().concat(A.difference(eq)(ys)(xs), A.difference(eq)(xs)(ys)); exports.symmetricDifference = symmetricDifference; const reduceWhile = (p) => (f) => { const go = (acc) => (ys) => (0, function_1.pipe)(NEA.fromArray(ys), O.filter((0, function_1.flow)(NEA.head, p)), O.fold((0, function_1.constant)(acc), (0, function_1.flow)(NEA.unprepend, ([z, zs]) => go(f(z)(acc))(zs)))); return go; }; exports.reduceWhile = reduceWhile; const reduceRightWhile = (p) => (f) => (x) => (0, function_1.flow)(A.reverse, (0, exports.reduceWhile)(p)(f)(x)); exports.reduceRightWhile = reduceRightWhile; exports.minimum = (0, function_1.flow)(Semigroup_1.min, NEA.concatAll); exports.maximum = (0, function_1.flow)(Semigroup_1.max, NEA.concatAll); const zipAll = (xs) => (ys) => { const zs = A.zip(ys, xs); const getRem = (0, exports.slice)(A.size(zs))(Number.POSITIVE_INFINITY); const rest = (0, function_1.pipe)(number_1.Ord.compare(A.size(ys), A.size(xs)), (0, Ordering_1.match)(() => (0, function_1.pipe)(xs, getRem, A.map(T.right)), (0, function_1.constant)(A.empty), () => (0, function_1.pipe)(ys, getRem, A.map(T.left)))); return (0, function_1.pipe)(zs, A.map(([za, zb]) => T.both(za, zb)), A.concat(rest)); }; exports.zipAll = zipAll; function filterA(F) { return p => xs => A.Witherable.wither(F)(xs, x => F.map(p(x), y => (y ? O.some(x) : O.none))); } exports.filterA = filterA; const extractAt = (i) => (xs) => (0, function_1.pipe)(xs, A.lookup(i), O.map(x => [x, A.unsafeDeleteAt(i, xs)])); exports.extractAt = extractAt; exports.fromIterable = Array.from; exports.fromReadonly = RA.toArray; exports.toReadonly = RA.fromArray; function allM(M) { return A.reduce(M.of(true), (x, y) => M.chain(x, b => (b ? y : M.of(false)))); } exports.allM = allM; function anyM(M) { return A.reduce(M.of(false), (x, y) => M.chain(x, b => (b ? M.of(true) : y))); } exports.anyM = anyM; const separateNE = (xs) => (0, function_1.pipe)(xs, A.separate, ({ left, right }) => { if (A.isEmpty(left)) return T.right(right); if (A.isEmpty(right)) return T.left(left); return T.both(left, right); }); exports.separateNE = separateNE;