175 lines
6.1 KiB
JavaScript
175 lines
6.1 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.sequenceS = exports.sequenceT = exports.getApplySemigroup = exports.apS = exports.apSecond = exports.apFirst = exports.ap = void 0;
|
|
var function_1 = require("./function");
|
|
var _ = __importStar(require("./internal"));
|
|
function ap(F, G) {
|
|
return function (fa) {
|
|
return function (fab) {
|
|
return F.ap(F.map(fab, function (gab) { return function (ga) { return G.ap(gab, ga); }; }), fa);
|
|
};
|
|
};
|
|
}
|
|
exports.ap = ap;
|
|
function apFirst(A) {
|
|
return function (second) { return function (first) {
|
|
return A.ap(A.map(first, function (a) { return function () { return a; }; }), second);
|
|
}; };
|
|
}
|
|
exports.apFirst = apFirst;
|
|
function apSecond(A) {
|
|
return function (second) {
|
|
return function (first) {
|
|
return A.ap(A.map(first, function () { return function (b) { return b; }; }), second);
|
|
};
|
|
};
|
|
}
|
|
exports.apSecond = apSecond;
|
|
function apS(F) {
|
|
return function (name, fb) {
|
|
return function (fa) {
|
|
return F.ap(F.map(fa, function (a) { return function (b) {
|
|
var _a;
|
|
return Object.assign({}, a, (_a = {}, _a[name] = b, _a));
|
|
}; }), fb);
|
|
};
|
|
};
|
|
}
|
|
exports.apS = apS;
|
|
function getApplySemigroup(F) {
|
|
return function (S) { return ({
|
|
concat: function (first, second) {
|
|
return F.ap(F.map(first, function (x) { return function (y) { return S.concat(x, y); }; }), second);
|
|
}
|
|
}); };
|
|
}
|
|
exports.getApplySemigroup = getApplySemigroup;
|
|
function curried(f, n, acc) {
|
|
return function (x) {
|
|
var combined = Array(acc.length + 1);
|
|
for (var i = 0; i < acc.length; i++) {
|
|
combined[i] = acc[i];
|
|
}
|
|
combined[acc.length] = x;
|
|
return n === 0 ? f.apply(null, combined) : curried(f, n - 1, combined);
|
|
};
|
|
}
|
|
var tupleConstructors = {
|
|
1: function (a) { return [a]; },
|
|
2: function (a) { return function (b) { return [a, b]; }; },
|
|
3: function (a) { return function (b) { return function (c) { return [a, b, c]; }; }; },
|
|
4: function (a) { return function (b) { return function (c) { return function (d) { return [a, b, c, d]; }; }; }; },
|
|
5: function (a) { return function (b) { return function (c) { return function (d) { return function (e) { return [a, b, c, d, e]; }; }; }; }; }
|
|
};
|
|
function getTupleConstructor(len) {
|
|
if (!_.has.call(tupleConstructors, len)) {
|
|
tupleConstructors[len] = curried(function_1.tuple, len - 1, []);
|
|
}
|
|
return tupleConstructors[len];
|
|
}
|
|
function sequenceT(F) {
|
|
return function () {
|
|
var args = [];
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
args[_i] = arguments[_i];
|
|
}
|
|
var len = args.length;
|
|
var f = getTupleConstructor(len);
|
|
var fas = F.map(args[0], f);
|
|
for (var i = 1; i < len; i++) {
|
|
fas = F.ap(fas, args[i]);
|
|
}
|
|
return fas;
|
|
};
|
|
}
|
|
exports.sequenceT = sequenceT;
|
|
function getRecordConstructor(keys) {
|
|
var len = keys.length;
|
|
switch (len) {
|
|
case 1:
|
|
return function (a) {
|
|
var _a;
|
|
return (_a = {}, _a[keys[0]] = a, _a);
|
|
};
|
|
case 2:
|
|
return function (a) { return function (b) {
|
|
var _a;
|
|
return (_a = {}, _a[keys[0]] = a, _a[keys[1]] = b, _a);
|
|
}; };
|
|
case 3:
|
|
return function (a) { return function (b) { return function (c) {
|
|
var _a;
|
|
return (_a = {}, _a[keys[0]] = a, _a[keys[1]] = b, _a[keys[2]] = c, _a);
|
|
}; }; };
|
|
case 4:
|
|
return function (a) { return function (b) { return function (c) { return function (d) {
|
|
var _a;
|
|
return (_a = {},
|
|
_a[keys[0]] = a,
|
|
_a[keys[1]] = b,
|
|
_a[keys[2]] = c,
|
|
_a[keys[3]] = d,
|
|
_a);
|
|
}; }; }; };
|
|
case 5:
|
|
return function (a) { return function (b) { return function (c) { return function (d) { return function (e) {
|
|
var _a;
|
|
return (_a = {},
|
|
_a[keys[0]] = a,
|
|
_a[keys[1]] = b,
|
|
_a[keys[2]] = c,
|
|
_a[keys[3]] = d,
|
|
_a[keys[4]] = e,
|
|
_a);
|
|
}; }; }; }; };
|
|
default:
|
|
return curried(function () {
|
|
var args = [];
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
args[_i] = arguments[_i];
|
|
}
|
|
var r = {};
|
|
for (var i = 0; i < len; i++) {
|
|
r[keys[i]] = args[i];
|
|
}
|
|
return r;
|
|
}, len - 1, []);
|
|
}
|
|
}
|
|
function sequenceS(F) {
|
|
return function (r) {
|
|
var keys = Object.keys(r);
|
|
var len = keys.length;
|
|
var f = getRecordConstructor(keys);
|
|
var fr = F.map(r[keys[0]], f);
|
|
for (var i = 1; i < len; i++) {
|
|
fr = F.ap(fr, r[keys[i]]);
|
|
}
|
|
return fr;
|
|
};
|
|
}
|
|
exports.sequenceS = sequenceS;
|