349 lines
9.1 KiB
JavaScript
349 lines
9.1 KiB
JavaScript
|
"use strict";
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.getEndomorphismMonoid = exports.not = exports.SK = exports.hole = exports.pipe = exports.untupled = exports.tupled = exports.absurd = exports.decrement = exports.increment = exports.tuple = exports.flow = exports.flip = exports.constVoid = exports.constUndefined = exports.constNull = exports.constFalse = exports.constTrue = exports.constant = exports.unsafeCoerce = exports.identity = exports.apply = exports.getRing = exports.getSemiring = exports.getMonoid = exports.getSemigroup = exports.getBooleanAlgebra = void 0;
|
||
|
// -------------------------------------------------------------------------------------
|
||
|
// instances
|
||
|
// -------------------------------------------------------------------------------------
|
||
|
/**
|
||
|
* @category instances
|
||
|
* @since 2.10.0
|
||
|
*/
|
||
|
var getBooleanAlgebra = function (B) {
|
||
|
return function () { return ({
|
||
|
meet: function (x, y) { return function (a) { return B.meet(x(a), y(a)); }; },
|
||
|
join: function (x, y) { return function (a) { return B.join(x(a), y(a)); }; },
|
||
|
zero: function () { return B.zero; },
|
||
|
one: function () { return B.one; },
|
||
|
implies: function (x, y) { return function (a) { return B.implies(x(a), y(a)); }; },
|
||
|
not: function (x) { return function (a) { return B.not(x(a)); }; }
|
||
|
}); };
|
||
|
};
|
||
|
exports.getBooleanAlgebra = getBooleanAlgebra;
|
||
|
/**
|
||
|
* Unary functions form a semigroup as long as you can provide a semigroup for the codomain.
|
||
|
*
|
||
|
* @example
|
||
|
* import { Predicate, getSemigroup } from 'fp-ts/function'
|
||
|
* import * as B from 'fp-ts/boolean'
|
||
|
*
|
||
|
* const f: Predicate<number> = (n) => n <= 2
|
||
|
* const g: Predicate<number> = (n) => n >= 0
|
||
|
*
|
||
|
* const S1 = getSemigroup(B.SemigroupAll)<number>()
|
||
|
*
|
||
|
* assert.deepStrictEqual(S1.concat(f, g)(1), true)
|
||
|
* assert.deepStrictEqual(S1.concat(f, g)(3), false)
|
||
|
*
|
||
|
* const S2 = getSemigroup(B.SemigroupAny)<number>()
|
||
|
*
|
||
|
* assert.deepStrictEqual(S2.concat(f, g)(1), true)
|
||
|
* assert.deepStrictEqual(S2.concat(f, g)(3), true)
|
||
|
*
|
||
|
* @category instances
|
||
|
* @since 2.10.0
|
||
|
*/
|
||
|
var getSemigroup = function (S) {
|
||
|
return function () { return ({
|
||
|
concat: function (f, g) { return function (a) { return S.concat(f(a), g(a)); }; }
|
||
|
}); };
|
||
|
};
|
||
|
exports.getSemigroup = getSemigroup;
|
||
|
/**
|
||
|
* Unary functions form a monoid as long as you can provide a monoid for the codomain.
|
||
|
*
|
||
|
* @example
|
||
|
* import { Predicate } from 'fp-ts/Predicate'
|
||
|
* import { getMonoid } from 'fp-ts/function'
|
||
|
* import * as B from 'fp-ts/boolean'
|
||
|
*
|
||
|
* const f: Predicate<number> = (n) => n <= 2
|
||
|
* const g: Predicate<number> = (n) => n >= 0
|
||
|
*
|
||
|
* const M1 = getMonoid(B.MonoidAll)<number>()
|
||
|
*
|
||
|
* assert.deepStrictEqual(M1.concat(f, g)(1), true)
|
||
|
* assert.deepStrictEqual(M1.concat(f, g)(3), false)
|
||
|
*
|
||
|
* const M2 = getMonoid(B.MonoidAny)<number>()
|
||
|
*
|
||
|
* assert.deepStrictEqual(M2.concat(f, g)(1), true)
|
||
|
* assert.deepStrictEqual(M2.concat(f, g)(3), true)
|
||
|
*
|
||
|
* @category instances
|
||
|
* @since 2.10.0
|
||
|
*/
|
||
|
var getMonoid = function (M) {
|
||
|
var getSemigroupM = (0, exports.getSemigroup)(M);
|
||
|
return function () { return ({
|
||
|
concat: getSemigroupM().concat,
|
||
|
empty: function () { return M.empty; }
|
||
|
}); };
|
||
|
};
|
||
|
exports.getMonoid = getMonoid;
|
||
|
/**
|
||
|
* @category instances
|
||
|
* @since 2.10.0
|
||
|
*/
|
||
|
var getSemiring = function (S) { return ({
|
||
|
add: function (f, g) { return function (x) { return S.add(f(x), g(x)); }; },
|
||
|
zero: function () { return S.zero; },
|
||
|
mul: function (f, g) { return function (x) { return S.mul(f(x), g(x)); }; },
|
||
|
one: function () { return S.one; }
|
||
|
}); };
|
||
|
exports.getSemiring = getSemiring;
|
||
|
/**
|
||
|
* @category instances
|
||
|
* @since 2.10.0
|
||
|
*/
|
||
|
var getRing = function (R) {
|
||
|
var S = (0, exports.getSemiring)(R);
|
||
|
return {
|
||
|
add: S.add,
|
||
|
mul: S.mul,
|
||
|
one: S.one,
|
||
|
zero: S.zero,
|
||
|
sub: function (f, g) { return function (x) { return R.sub(f(x), g(x)); }; }
|
||
|
};
|
||
|
};
|
||
|
exports.getRing = getRing;
|
||
|
// -------------------------------------------------------------------------------------
|
||
|
// utils
|
||
|
// -------------------------------------------------------------------------------------
|
||
|
/**
|
||
|
* @since 2.11.0
|
||
|
*/
|
||
|
var apply = function (a) {
|
||
|
return function (f) {
|
||
|
return f(a);
|
||
|
};
|
||
|
};
|
||
|
exports.apply = apply;
|
||
|
/**
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
function identity(a) {
|
||
|
return a;
|
||
|
}
|
||
|
exports.identity = identity;
|
||
|
/**
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
exports.unsafeCoerce = identity;
|
||
|
/**
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
function constant(a) {
|
||
|
return function () { return a; };
|
||
|
}
|
||
|
exports.constant = constant;
|
||
|
/**
|
||
|
* A thunk that returns always `true`.
|
||
|
*
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
exports.constTrue = constant(true);
|
||
|
/**
|
||
|
* A thunk that returns always `false`.
|
||
|
*
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
exports.constFalse = constant(false);
|
||
|
/**
|
||
|
* A thunk that returns always `null`.
|
||
|
*
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
exports.constNull = constant(null);
|
||
|
/**
|
||
|
* A thunk that returns always `undefined`.
|
||
|
*
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
exports.constUndefined = constant(undefined);
|
||
|
/**
|
||
|
* A thunk that returns always `void`.
|
||
|
*
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
exports.constVoid = exports.constUndefined;
|
||
|
function flip(f) {
|
||
|
return function () {
|
||
|
var args = [];
|
||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||
|
args[_i] = arguments[_i];
|
||
|
}
|
||
|
if (args.length > 1) {
|
||
|
return f(args[1], args[0]);
|
||
|
}
|
||
|
return function (a) { return f(a)(args[0]); };
|
||
|
};
|
||
|
}
|
||
|
exports.flip = flip;
|
||
|
function flow(ab, bc, cd, de, ef, fg, gh, hi, ij) {
|
||
|
switch (arguments.length) {
|
||
|
case 1:
|
||
|
return ab;
|
||
|
case 2:
|
||
|
return function () {
|
||
|
return bc(ab.apply(this, arguments));
|
||
|
};
|
||
|
case 3:
|
||
|
return function () {
|
||
|
return cd(bc(ab.apply(this, arguments)));
|
||
|
};
|
||
|
case 4:
|
||
|
return function () {
|
||
|
return de(cd(bc(ab.apply(this, arguments))));
|
||
|
};
|
||
|
case 5:
|
||
|
return function () {
|
||
|
return ef(de(cd(bc(ab.apply(this, arguments)))));
|
||
|
};
|
||
|
case 6:
|
||
|
return function () {
|
||
|
return fg(ef(de(cd(bc(ab.apply(this, arguments))))));
|
||
|
};
|
||
|
case 7:
|
||
|
return function () {
|
||
|
return gh(fg(ef(de(cd(bc(ab.apply(this, arguments)))))));
|
||
|
};
|
||
|
case 8:
|
||
|
return function () {
|
||
|
return hi(gh(fg(ef(de(cd(bc(ab.apply(this, arguments))))))));
|
||
|
};
|
||
|
case 9:
|
||
|
return function () {
|
||
|
return ij(hi(gh(fg(ef(de(cd(bc(ab.apply(this, arguments)))))))));
|
||
|
};
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
exports.flow = flow;
|
||
|
/**
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
function tuple() {
|
||
|
var t = [];
|
||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||
|
t[_i] = arguments[_i];
|
||
|
}
|
||
|
return t;
|
||
|
}
|
||
|
exports.tuple = tuple;
|
||
|
/**
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
function increment(n) {
|
||
|
return n + 1;
|
||
|
}
|
||
|
exports.increment = increment;
|
||
|
/**
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
function decrement(n) {
|
||
|
return n - 1;
|
||
|
}
|
||
|
exports.decrement = decrement;
|
||
|
/**
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
function absurd(_) {
|
||
|
throw new Error('Called `absurd` function which should be uncallable');
|
||
|
}
|
||
|
exports.absurd = absurd;
|
||
|
/**
|
||
|
* Creates a tupled version of this function: instead of `n` arguments, it accepts a single tuple argument.
|
||
|
*
|
||
|
* @example
|
||
|
* import { tupled } from 'fp-ts/function'
|
||
|
*
|
||
|
* const add = tupled((x: number, y: number): number => x + y)
|
||
|
*
|
||
|
* assert.strictEqual(add([1, 2]), 3)
|
||
|
*
|
||
|
* @since 2.4.0
|
||
|
*/
|
||
|
function tupled(f) {
|
||
|
return function (a) { return f.apply(void 0, a); };
|
||
|
}
|
||
|
exports.tupled = tupled;
|
||
|
/**
|
||
|
* Inverse function of `tupled`
|
||
|
*
|
||
|
* @since 2.4.0
|
||
|
*/
|
||
|
function untupled(f) {
|
||
|
return function () {
|
||
|
var a = [];
|
||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||
|
a[_i] = arguments[_i];
|
||
|
}
|
||
|
return f(a);
|
||
|
};
|
||
|
}
|
||
|
exports.untupled = untupled;
|
||
|
function pipe(a, ab, bc, cd, de, ef, fg, gh, hi) {
|
||
|
switch (arguments.length) {
|
||
|
case 1:
|
||
|
return a;
|
||
|
case 2:
|
||
|
return ab(a);
|
||
|
case 3:
|
||
|
return bc(ab(a));
|
||
|
case 4:
|
||
|
return cd(bc(ab(a)));
|
||
|
case 5:
|
||
|
return de(cd(bc(ab(a))));
|
||
|
case 6:
|
||
|
return ef(de(cd(bc(ab(a)))));
|
||
|
case 7:
|
||
|
return fg(ef(de(cd(bc(ab(a))))));
|
||
|
case 8:
|
||
|
return gh(fg(ef(de(cd(bc(ab(a)))))));
|
||
|
case 9:
|
||
|
return hi(gh(fg(ef(de(cd(bc(ab(a))))))));
|
||
|
default: {
|
||
|
var ret = arguments[0];
|
||
|
for (var i = 1; i < arguments.length; i++) {
|
||
|
ret = arguments[i](ret);
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
exports.pipe = pipe;
|
||
|
/**
|
||
|
* Type hole simulation
|
||
|
*
|
||
|
* @since 2.7.0
|
||
|
*/
|
||
|
exports.hole = absurd;
|
||
|
/**
|
||
|
* @since 2.11.0
|
||
|
*/
|
||
|
var SK = function (_, b) { return b; };
|
||
|
exports.SK = SK;
|
||
|
/**
|
||
|
* Use `Predicate` module instead.
|
||
|
*
|
||
|
* @category zone of death
|
||
|
* @since 2.0.0
|
||
|
* @deprecated
|
||
|
*/
|
||
|
function not(predicate) {
|
||
|
return function (a) { return !predicate(a); };
|
||
|
}
|
||
|
exports.not = not;
|
||
|
/**
|
||
|
* Use `Endomorphism` module instead.
|
||
|
*
|
||
|
* @category zone of death
|
||
|
* @since 2.10.0
|
||
|
* @deprecated
|
||
|
*/
|
||
|
var getEndomorphismMonoid = function () { return ({
|
||
|
concat: function (first, second) { return flow(first, second); },
|
||
|
empty: identity
|
||
|
}); };
|
||
|
exports.getEndomorphismMonoid = getEndomorphismMonoid;
|