macoolka-collection
Version:
`macoolka-collection` Define Data Collection Interface.
528 lines • 22.4 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.initTypeclass = void 0;
var pipeable_1 = require("fp-ts/pipeable");
var Apply_1 = require("fp-ts/Apply");
var Foldable_1 = require("fp-ts/Foldable");
var function_1 = require("fp-ts/function");
var E = require("fp-ts/Either");
var O = require("fp-ts/Option");
var Ord_1 = require("fp-ts/Ord");
var _Array = require("fp-ts/Array");
var R = require("fp-ts/Record");
var macoolka_object_1 = require("macoolka-object");
var indexAToA = function (fai) {
return function (_, a) { return fai(a); };
};
var swip = function (f) {
return function (b, a) { return f(a, b); };
};
function initTypeclass(option) {
var from = option.from, forEachIndex = option.forEachIndex, slice = option.slice, URI = option.URI, toArray = option.toArray, size = option.size, getAt = option.getAt, first = option.first, last = option.last, notEmpty = option.notEmpty, empty = option.empty, snoc = option.snoc, everyIndex = option.everyIndex, findFirstIndex = option.findFirstIndex;
function mapWithIndex(mapper) {
return function (fa) {
var container = [];
(0, pipeable_1.pipe)(fa, forEachIndex(function (index, a) {
container.push(mapper(index, a));
return true;
}));
return from(container);
};
}
function map(mapper) {
return (0, pipeable_1.pipe)(mapper, indexAToA, mapWithIndex);
}
var ap = function (fa) { return function (fab) {
return (0, pipeable_1.pipe)(fab, map(function (ab) {
return (0, pipeable_1.pipe)(fa, map(ab));
}), flatten);
}; };
var chain = function (f) { return function (ma) {
return (0, pipeable_1.pipe)(ma, map(f), flatten);
}; };
var of = function (a) { return from([a]); };
var apFirst = function (fb) { return function (fa) {
return collection.ap(collection.map(fa, function (a) { return function (_) { return a; }; }), fb);
}; };
// collection.ap(collection.map(fa,a=>()=>a),fb)
// = fb => fa => I.ap(I.map(fa, a => () => a), fb)
var apSecond = function (fb) { return function (fa) {
return collection.ap(collection.map(fa, function (_) { return function (value) { return value; }; }), fb);
}; };
var chainFirst = function (_) { return function (fa) { return fa; }; };
var flatten = function (mma) {
var result = [];
(0, pipeable_1.pipe)(mma, map((0, pipeable_1.pipe)(map(function (a) {
result.push(a);
}))));
return from(result);
};
var sequenceT = function () {
var e_1, _a;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var fst = args[0];
var others = args.slice(1);
var fas = collection.map(fst, function (a) { return [a]; });
try {
for (var others_1 = __values(others), others_1_1 = others_1.next(); !others_1_1.done; others_1_1 = others_1.next()) {
var fa = others_1_1.value;
fas = collection.ap(collection.map(fas, function (as) { return function (a) {
as.push(a);
return as;
}; }), fa);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (others_1_1 && !others_1_1.done && (_a = others_1.return)) _a.call(others_1);
}
finally { if (e_1) throw e_1.error; }
}
return flatten(fas);
};
var reduceWithIndex = function (b, f) { return function (as) {
var result = b;
(0, pipeable_1.pipe)(as, mapWithIndex(function (i, a) {
result = f(i, result, a);
}));
return result;
}; };
var reduceRightWithIndex = function (b, f) { return function (as) {
var result = b;
(0, pipeable_1.pipe)(as, mapWithIndex(function (i, a) {
result = f(i, a, result);
}));
return result;
}; };
var foldMapWithIndex = function (m) { return function (f) { return function (as) {
return (0, pipeable_1.pipe)(as, reduceWithIndex(m.empty, function (i, b, a) {
return m.concat(b, f(i, a));
}));
}; }; };
var reduce = function (b, f) { return function (as) {
var result = b;
(0, pipeable_1.pipe)(as, map(function (a) {
result = f(result, a);
}));
return result;
}; };
var reduceRight = function (b, f) { return function (as) {
return reduce(b, swip(f))(as);
}; };
var foldMap = function (m) { return function (f) { return function (as) {
return (0, pipeable_1.pipe)(as, reduce(m.empty, function (b, a) {
return m.concat(b, f(a));
}));
}; }; };
function traverse_(M) {
var applyFirst = function (mu, mb) { return M.ap(M.map(mu, function_1.constant), mb); };
var mu = M.of(undefined);
return function (fa, f) { return collection.reduce(fa, mu, function (mu, a) { return applyFirst(mu, f(a)); }); };
}
var unfold = function (b, f) {
var ret = [];
var bb = b;
while (true) {
var mt = f(bb);
if (O.isSome(mt)) {
var _a = __read(mt.value, 2), a = _a[0], b_1 = _a[1];
ret.push(a);
bb = b_1;
}
else {
break;
}
}
return from(ret);
};
var partitionWithIndex = function (predicateWithIndex) { return function (fa) {
var left = [];
var right = [];
(0, pipeable_1.pipe)(fa, mapWithIndex(function (i, a) {
(0, pipeable_1.pipe)(predicateWithIndex(i, a), function (result) { return result ? right.push(a) : left.push(a); });
}));
return {
left: from(left),
right: from(right)
};
}; };
var filterWithIndex = function (predicateWithIndex) { return function (fa) {
var result = [];
(0, pipeable_1.pipe)(fa, mapWithIndex(function (i, a) {
(0, pipeable_1.pipe)(predicateWithIndex(i, a), function (value) {
if (value) {
result.push(a);
}
});
}));
return from(result);
}; };
var partitionMapWithIndex = function (f) { return function (fa) {
var left = [];
var right = [];
(0, pipeable_1.pipe)(fa, mapWithIndex(function (i, a) {
(0, pipeable_1.pipe)(f(i, a), E.fold(function (l) { return left.push(l); }, function (r) { return right.push(r); }));
}));
return { left: from(left), right: from(right) };
}; };
var filterMapWithIndex = function (f) { return function (fa) {
var result = [];
(0, pipeable_1.pipe)(fa, mapWithIndex(function (i, a) {
(0, pipeable_1.pipe)(f(i, a), function (value) {
if (O.isSome(value)) {
result.push(value.value);
}
});
}));
return from(result);
}; };
var filter = function (predicate) { return function (fa) {
var result = [];
(0, pipeable_1.pipe)(fa, map(function (a) {
(0, pipeable_1.pipe)(predicate(a), function (value) {
if (value) {
result.push(a);
}
});
}));
return from(result);
}; };
var filterMap = function (f) { return function (as) {
return (0, pipeable_1.pipe)(as, filterMapWithIndex(function (_, a) { return f(a); }));
}; };
var partition = function (predicate) { return function (fa) {
var left = [];
var right = [];
(0, pipeable_1.pipe)(fa, map(function (a) {
(0, pipeable_1.pipe)(predicate(a), function (result) { return result ? right.push(a) : left.push(a); });
}));
return {
left: from(left),
right: from(right)
};
}; };
var partitionMap = function (f) { return function (as) {
return (0, pipeable_1.pipe)(as, partitionMapWithIndex(function (_, a) { return f(a); }));
}; };
var compact = function (fa) { return (0, pipeable_1.pipe)(fa, filterMap(function (a) { return a; })); };
var separate = function (fa) { return (0, pipeable_1.pipe)(fa, partitionMap(function (a) { return a; })); };
var traverse = function (App) {
return function (ta, f) {
var traverseWithIndexF = traverseWithIndex(App);
return traverseWithIndexF(ta, function (_, a) { return f(a); });
};
};
var traverseWithIndex = function (App) {
return function (ta, f) {
return (0, pipeable_1.pipe)(ta, reduceWithIndex(App.of(zero()), function (i, fbs, a) {
return App.ap(App.map(fbs, function (bs) { return function (b) {
return snoc(b)(bs);
}; }), f(i, a));
// const result= f(i,a)
}));
};
};
var sequence = function (App) {
return function (ta) {
return (0, pipeable_1.pipe)(ta, reduce(App.of(empty()), function (fas, fa) {
return App.ap(App.map(fas, function (as) { return function (a) { return snoc(a)(as); }; }), fa);
}));
};
};
var wilt = function (App) {
return function (ta, f) {
var traverseF = traverse(App);
return App.map(traverseF(ta, f), separate);
};
};
var wither = function (App) {
return function (ta, f) {
var traverseF = traverse(App);
return App.map(traverseF(ta, f), compact);
};
};
var extend = function (f) { return function (fa) {
return (0, pipeable_1.pipe)(fa, mapWithIndex(function (i) { return f(slice(i)(fa)); }));
}; };
var duplicate = function (fa) {
return (0, pipeable_1.pipe)(fa, extend(function (a) { return a; }));
};
var sort = function (Ord) { return function (as) {
return (0, pipeable_1.pipe)(as, toArray, function (ar) { return ar.sort(Ord.compare); }, from);
}; };
var sortBy = function (E) { return function (as) {
return from(_Array.sortBy(E)(toArray(as)));
}; };
var min = function (Ord) { return function (as) {
return (0, pipeable_1.pipe)(as, sort(Ord), first);
}; };
var minBy = function (Ord) { return function (as) {
return (0, pipeable_1.pipe)(as, sortBy(Ord), first);
}; };
var max = function (Ord) { return function (as) {
return (0, pipeable_1.pipe)(as, sort(Ord), last);
}; };
var maxBy = function (Ord) { return function (as) {
return (0, pipeable_1.pipe)(as, sortBy(Ord), first);
}; };
var zipWith = function (fa, fb, f) {
var fc = [];
var len = Math.min(size(fa), size(fb));
for (var i = 0; i < len; i++) {
var va = getAt(i)(fa);
var vb = getAt(i)(fb);
if (O.isNone(va) || O.isNone(vb)) {
throw new Error("The given index ".concat(i, " not exist "));
}
fc[i] = f(va.value, vb.value);
}
return from(fc);
};
var rotate = function (i) { return function (as) {
return from(_Array.rotate(i)(toArray(as)));
}; };
var zip = function (fa, fb) {
return zipWith(fa, fb, function (a, b) { return [a, b]; });
};
var unzip = function (as) {
var fa = [];
var fb = [];
var ar = toArray(as);
for (var i = 0; i < ar.length; i++) {
fa[i] = ar[i][0];
fb[i] = ar[i][1];
}
return [from(fa), from(fb)];
};
var includes = function (E) { return function (a, as) {
var predicate = function (element) { return E.equals(element, a); };
return O.isSome(findFirstIndex(predicate)(as));
}; };
var uniq = function (E) { return function (as) {
return from(_Array.uniq(E)(toArray(as)));
}; };
var union = function (E) { return function (xs, ys) {
return from(_Array.union(E)(toArray(xs), toArray(ys)));
}; };
var intersection = function (E) { return function (xs, ys) {
return from(_Array.intersection(E)(toArray(xs), toArray(ys)));
}; };
var difference = function (E) { return function (xs, ys) {
return from(_Array.difference(E)(toArray(xs), toArray(ys)));
}; };
var scanLeft = function (b, f) { return function (as) {
return from(_Array.scanLeft(b, f)(toArray(as)));
}; };
var scanRight = function (b, f) { return function (as) {
return from(_Array.scanRight(b, f)(toArray(as)));
}; };
var concat = function (x, y) { return from(toArray(x).concat(toArray(y))); };
var getMonoid = function () {
return {
concat: function (x, y) { return concat(x, y); },
empty: empty(),
};
};
var getEq = function (eq) {
return {
equals: function (xs, ys) { return size(xs) === size(ys) && (0, pipeable_1.pipe)(xs, everyIndex(function (i, a) {
return (0, pipeable_1.pipe)(ys, getAt(i), O.map(function (value) { return eq.equals(value, a); }), O.getOrElse(function () { return false; }));
})); }
};
};
var getOrd = function (Ord) {
return (0, Ord_1.fromCompare)(function (a, b) {
var aLen = size(a);
var bLen = size(b);
var len = Math.min(aLen, bLen);
for (var i = 0; i < len; i++) {
var va = getAt(i)(a);
if (O.isNone(va)) {
return -1;
}
var vb = getAt(i)(b);
if (O.isNone(vb)) {
return 1;
}
var ordering = Ord.compare(va.value, vb.value);
if (ordering !== 0) {
return ordering;
}
}
return Ord_1.ordNumber.compare(aLen, bLen);
});
};
var getShow = function (S) {
return {
show: function (as) { return "[".concat((0, pipeable_1.pipe)(as, map(S.show), toArray)
.join(', '), "]"); }
};
};
var rights = function (as) {
var result = [];
(0, pipeable_1.pipe)(as, map(function (a) {
if (E.isRight(a)) {
result.push(a.right);
}
}));
return from(result);
};
var lefts = function (as) {
var result = [];
(0, pipeable_1.pipe)(as, map(function (a) {
if (E.isLeft(a)) {
result.push(a.left);
}
}));
return from(result);
};
var foldLeft = function (onNil, onCons) { return function (as) {
return (0, pipeable_1.pipe)(as, first, O.map(function (head) {
return onCons(head, slice(1)(as));
}), O.getOrElse(function () {
return onNil();
}));
}; };
var foldRight = function (onNil, onCons) { return function (as) {
return (0, pipeable_1.pipe)(as, last, O.map(function (value) {
return onCons(slice(0, size(as) - 1)(as), value);
}), O.getOrElse(function () {
return onNil();
}));
}; };
var chop = function (f) { return function (as) {
var result = zero();
var cs = as;
while (notEmpty(cs)) {
var _a = __read(f(cs), 2), b = _a[0], c = _a[1];
result = snoc(b)(result);
cs = c;
}
return result;
}; };
var groupByOption = function (_a) {
var getValue = _a.getValue, show = _a.show.show;
return function (as) {
var result = {};
var add = function (a) {
var b = getValue(a);
var key = show(b);
(0, pipeable_1.pipe)(O.fromNullable(result[key]), O.fold(function () {
result[key] = from([a]);
}, function (bs) {
result[key] = snoc(a)(bs);
}));
};
(0, pipeable_1.pipe)(as, map(add));
return result;
};
};
var toRecord = function (key) { return function (as) {
return (0, pipeable_1.pipe)(as, reduce({}, function (b, a) {
var _a;
return (__assign(__assign({}, b), (_a = {}, _a[a[key]] = (0, macoolka_object_1.omit)(a, [key]), _a)));
}));
}; };
var fromRecord = function (key) { return function (as) {
return (0, pipeable_1.pipe)(as, R.reduceWithIndex(empty(), function (k, b, a) {
var _a;
return (__spreadArray(__spreadArray([], __read(b), false), [__assign(__assign({}, a), (_a = {}, _a[key] = k, _a))], false));
}));
}; };
var comprehension = function (input, f, g) {
if (g === void 0) { g = function () { return true; }; }
var go = function (scope, input) {
if (input.length === 0) {
return g.apply(void 0, __spreadArray([], __read(scope), false)) ? of(f.apply(void 0, __spreadArray([], __read(scope), false))) : empty();
}
else {
return collection.chain(input[0], function (x) { return go(snoc(x)(scope), input.slice(1)); });
}
};
return go(empty(), input);
};
var zero = function () { return empty(); };
var alt = function (fy) { return function (fx) { return concat(fx, fy()); }; };
var collection = {
URI: URI,
of: function (a) { return of(a); },
map: function (as, a) { return map(a)(as); },
mapWithIndex: function (as, f) { return mapWithIndex(f)(as); },
ap: function (fab, fa) { return ap(fa)(fab); },
chain: function (fa, f) { return chain(f)(fa); },
reduce: function (as, b, f) { return reduce(b, f)(as); },
reduceRight: function (as, b, f) { return reduceRight(b, f)(as); },
foldMap: function (M) { return function (as, f) { return foldMap(M)(f)(as); }; },
reduceWithIndex: function (fa, b, f) { return reduceWithIndex(b, f)(fa); },
reduceRightWithIndex: function (fa, b, f) { return reduceRightWithIndex(b, f)(fa); },
foldMapWithIndex: function (M) { return function (fa, f) { return foldMapWithIndex(M)(f)(fa); }; },
compact: compact,
separate: separate,
unfold: unfold,
alt: function (fx, fy) { return alt(fy)(fx); },
zero: zero,
wilt: wilt,
wither: wither,
traverse: traverse,
sequence: sequence,
partition: function (fa, predicate) { return partition(predicate)(fa); },
partitionMap: function (fa, f) { return partitionMap(f)(fa); },
filter: function (fa, predicate) { return filter(predicate)(fa); },
filterMap: function (fa, f) { return filterMap(f)(fa); },
traverseWithIndex: traverseWithIndex,
};
return __assign({ zero: zero, alt: alt, sequenceS: (0, Apply_1.sequenceS)(collection), sequenceT: sequenceT, collection: collection, foldM: function (M) { return (0, Foldable_1.foldM)(M, collection); }, map: map, mapWithIndex: mapWithIndex, ap: ap, apFirst: apFirst, apSecond: apSecond, chain: chain, chainFirst: chainFirst, flatten: flatten, of: of, reduce: reduce, reduceRight: reduceRight, traverse_: traverse_, foldMap: foldMap, reduceWithIndex: reduceWithIndex, reduceRightWithIndex: reduceRightWithIndex, foldMapWithIndex: foldMapWithIndex, unfold: unfold, filter: filter, filterMap: filterMap, filterWithIndex: filterWithIndex, filterMapWithIndex: filterMapWithIndex, partition: partition, partitionMap: partitionMap, partitionMapWithIndex: partitionMapWithIndex, partitionWithIndex: partitionWithIndex, compact: compact, separate: separate, sort: sort, sortBy: sortBy, min: min, minBy: minBy, max: max, maxBy: maxBy, intersection: intersection, union: union, difference: difference, zip: zip, zipWith: zipWith, unzip: unzip, uniq: uniq, includes: includes, rotate: rotate, scanLeft: scanLeft, scanRight: scanRight, getEq: getEq, getMonoid: getMonoid, getShow: getShow, getOrd: getOrd, lefts: lefts, rights: rights, foldLeft: foldLeft, foldRight: foldRight, traverse: traverse, traverseWithIndex: traverseWithIndex, sequence: sequence, wilt: wilt, wither: wither, extend: extend, duplicate: duplicate, chop: chop, groupBy: groupByOption, comprehension: comprehension, toRecord: toRecord, fromRecord: fromRecord }, option);
}
exports.initTypeclass = initTypeclass;
//# sourceMappingURL=typeclass.js.map