326 lines
8.1 KiB
JavaScript
326 lines
8.1 KiB
JavaScript
|
// -------------------------------------------------------------------------------------
|
||
|
// instances
|
||
|
// -------------------------------------------------------------------------------------
|
||
|
/**
|
||
|
* @category instances
|
||
|
* @since 2.10.0
|
||
|
*/
|
||
|
export 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)); }; }
|
||
|
}); };
|
||
|
};
|
||
|
/**
|
||
|
* 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
|
||
|
*/
|
||
|
export var getSemigroup = function (S) {
|
||
|
return function () { return ({
|
||
|
concat: function (f, g) { return function (a) { return S.concat(f(a), g(a)); }; }
|
||
|
}); };
|
||
|
};
|
||
|
/**
|
||
|
* 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
|
||
|
*/
|
||
|
export var getMonoid = function (M) {
|
||
|
var getSemigroupM = getSemigroup(M);
|
||
|
return function () { return ({
|
||
|
concat: getSemigroupM().concat,
|
||
|
empty: function () { return M.empty; }
|
||
|
}); };
|
||
|
};
|
||
|
/**
|
||
|
* @category instances
|
||
|
* @since 2.10.0
|
||
|
*/
|
||
|
export 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; }
|
||
|
}); };
|
||
|
/**
|
||
|
* @category instances
|
||
|
* @since 2.10.0
|
||
|
*/
|
||
|
export var getRing = function (R) {
|
||
|
var S = 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)); }; }
|
||
|
};
|
||
|
};
|
||
|
// -------------------------------------------------------------------------------------
|
||
|
// utils
|
||
|
// -------------------------------------------------------------------------------------
|
||
|
/**
|
||
|
* @since 2.11.0
|
||
|
*/
|
||
|
export var apply = function (a) {
|
||
|
return function (f) {
|
||
|
return f(a);
|
||
|
};
|
||
|
};
|
||
|
/**
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
export function identity(a) {
|
||
|
return a;
|
||
|
}
|
||
|
/**
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
export var unsafeCoerce = identity;
|
||
|
/**
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
export function constant(a) {
|
||
|
return function () { return a; };
|
||
|
}
|
||
|
/**
|
||
|
* A thunk that returns always `true`.
|
||
|
*
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
export var constTrue = /*#__PURE__*/ constant(true);
|
||
|
/**
|
||
|
* A thunk that returns always `false`.
|
||
|
*
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
export var constFalse = /*#__PURE__*/ constant(false);
|
||
|
/**
|
||
|
* A thunk that returns always `null`.
|
||
|
*
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
export var constNull = /*#__PURE__*/ constant(null);
|
||
|
/**
|
||
|
* A thunk that returns always `undefined`.
|
||
|
*
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
export var constUndefined = /*#__PURE__*/ constant(undefined);
|
||
|
/**
|
||
|
* A thunk that returns always `void`.
|
||
|
*
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
export var constVoid = constUndefined;
|
||
|
export 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]); };
|
||
|
};
|
||
|
}
|
||
|
export 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;
|
||
|
}
|
||
|
/**
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
export function tuple() {
|
||
|
var t = [];
|
||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||
|
t[_i] = arguments[_i];
|
||
|
}
|
||
|
return t;
|
||
|
}
|
||
|
/**
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
export function increment(n) {
|
||
|
return n + 1;
|
||
|
}
|
||
|
/**
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
export function decrement(n) {
|
||
|
return n - 1;
|
||
|
}
|
||
|
/**
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
export function absurd(_) {
|
||
|
throw new Error('Called `absurd` function which should be uncallable');
|
||
|
}
|
||
|
/**
|
||
|
* 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
|
||
|
*/
|
||
|
export function tupled(f) {
|
||
|
return function (a) { return f.apply(void 0, a); };
|
||
|
}
|
||
|
/**
|
||
|
* Inverse function of `tupled`
|
||
|
*
|
||
|
* @since 2.4.0
|
||
|
*/
|
||
|
export function untupled(f) {
|
||
|
return function () {
|
||
|
var a = [];
|
||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||
|
a[_i] = arguments[_i];
|
||
|
}
|
||
|
return f(a);
|
||
|
};
|
||
|
}
|
||
|
export 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;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/**
|
||
|
* Type hole simulation
|
||
|
*
|
||
|
* @since 2.7.0
|
||
|
*/
|
||
|
export var hole = absurd;
|
||
|
/**
|
||
|
* @since 2.11.0
|
||
|
*/
|
||
|
export var SK = function (_, b) { return b; };
|
||
|
/**
|
||
|
* Use `Predicate` module instead.
|
||
|
*
|
||
|
* @category zone of death
|
||
|
* @since 2.0.0
|
||
|
* @deprecated
|
||
|
*/
|
||
|
export function not(predicate) {
|
||
|
return function (a) { return !predicate(a); };
|
||
|
}
|
||
|
/**
|
||
|
* Use `Endomorphism` module instead.
|
||
|
*
|
||
|
* @category zone of death
|
||
|
* @since 2.10.0
|
||
|
* @deprecated
|
||
|
*/
|
||
|
export var getEndomorphismMonoid = function () { return ({
|
||
|
concat: function (first, second) { return flow(first, second); },
|
||
|
empty: identity
|
||
|
}); };
|