374 lines
9.1 KiB
JavaScript
374 lines
9.1 KiB
JavaScript
/**
|
|
* If a type `A` can form a `Semigroup` it has an **associative** binary operation.
|
|
*
|
|
* ```ts
|
|
* interface Semigroup<A> {
|
|
* readonly concat: (x: A, y: A) => A
|
|
* }
|
|
* ```
|
|
*
|
|
* Associativity means the following equality must hold for any choice of `x`, `y`, and `z`.
|
|
*
|
|
* ```ts
|
|
* concat(x, concat(y, z)) = concat(concat(x, y), z)
|
|
* ```
|
|
*
|
|
* A common example of a semigroup is the type `string` with the operation `+`.
|
|
*
|
|
* ```ts
|
|
* import { Semigroup } from 'fp-ts/Semigroup'
|
|
*
|
|
* const semigroupString: Semigroup<string> = {
|
|
* concat: (x, y) => x + y
|
|
* }
|
|
*
|
|
* const x = 'x'
|
|
* const y = 'y'
|
|
* const z = 'z'
|
|
*
|
|
* semigroupString.concat(x, y) // 'xy'
|
|
*
|
|
* semigroupString.concat(x, semigroupString.concat(y, z)) // 'xyz'
|
|
*
|
|
* semigroupString.concat(semigroupString.concat(x, y), z) // 'xyz'
|
|
* ```
|
|
*
|
|
* *Adapted from https://typelevel.org/cats*
|
|
*
|
|
* @since 2.0.0
|
|
*/
|
|
import { getSemigroup, identity } from './function';
|
|
import * as _ from './internal';
|
|
import * as M from './Magma';
|
|
import * as Or from './Ord';
|
|
// -------------------------------------------------------------------------------------
|
|
// constructors
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* Get a semigroup where `concat` will return the minimum, based on the provided order.
|
|
*
|
|
* @example
|
|
* import * as N from 'fp-ts/number'
|
|
* import * as S from 'fp-ts/Semigroup'
|
|
*
|
|
* const S1 = S.min(N.Ord)
|
|
*
|
|
* assert.deepStrictEqual(S1.concat(1, 2), 1)
|
|
*
|
|
* @category constructors
|
|
* @since 2.10.0
|
|
*/
|
|
export var min = function (O) { return ({
|
|
concat: Or.min(O)
|
|
}); };
|
|
/**
|
|
* Get a semigroup where `concat` will return the maximum, based on the provided order.
|
|
*
|
|
* @example
|
|
* import * as N from 'fp-ts/number'
|
|
* import * as S from 'fp-ts/Semigroup'
|
|
*
|
|
* const S1 = S.max(N.Ord)
|
|
*
|
|
* assert.deepStrictEqual(S1.concat(1, 2), 2)
|
|
*
|
|
* @category constructors
|
|
* @since 2.10.0
|
|
*/
|
|
export var max = function (O) { return ({
|
|
concat: Or.max(O)
|
|
}); };
|
|
/**
|
|
* @category constructors
|
|
* @since 2.10.0
|
|
*/
|
|
export var constant = function (a) { return ({
|
|
concat: function () { return a; }
|
|
}); };
|
|
// -------------------------------------------------------------------------------------
|
|
// combinators
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* The dual of a `Semigroup`, obtained by swapping the arguments of `concat`.
|
|
*
|
|
* @example
|
|
* import { reverse } from 'fp-ts/Semigroup'
|
|
* import * as S from 'fp-ts/string'
|
|
*
|
|
* assert.deepStrictEqual(reverse(S.Semigroup).concat('a', 'b'), 'ba')
|
|
*
|
|
* @since 2.10.0
|
|
*/
|
|
export var reverse = M.reverse;
|
|
/**
|
|
* Given a struct of semigroups returns a semigroup for the struct.
|
|
*
|
|
* @example
|
|
* import { struct } from 'fp-ts/Semigroup'
|
|
* import * as N from 'fp-ts/number'
|
|
*
|
|
* interface Point {
|
|
* readonly x: number
|
|
* readonly y: number
|
|
* }
|
|
*
|
|
* const S = struct<Point>({
|
|
* x: N.SemigroupSum,
|
|
* y: N.SemigroupSum
|
|
* })
|
|
*
|
|
* assert.deepStrictEqual(S.concat({ x: 1, y: 2 }, { x: 3, y: 4 }), { x: 4, y: 6 })
|
|
*
|
|
* @since 2.10.0
|
|
*/
|
|
export var struct = function (semigroups) { return ({
|
|
concat: function (first, second) {
|
|
var r = {};
|
|
for (var k in semigroups) {
|
|
if (_.has.call(semigroups, k)) {
|
|
r[k] = semigroups[k].concat(first[k], second[k]);
|
|
}
|
|
}
|
|
return r;
|
|
}
|
|
}); };
|
|
/**
|
|
* Given a tuple of semigroups returns a semigroup for the tuple.
|
|
*
|
|
* @example
|
|
* import { tuple } from 'fp-ts/Semigroup'
|
|
* import * as B from 'fp-ts/boolean'
|
|
* import * as N from 'fp-ts/number'
|
|
* import * as S from 'fp-ts/string'
|
|
*
|
|
* const S1 = tuple(S.Semigroup, N.SemigroupSum)
|
|
* assert.deepStrictEqual(S1.concat(['a', 1], ['b', 2]), ['ab', 3])
|
|
*
|
|
* const S2 = tuple(S.Semigroup, N.SemigroupSum, B.SemigroupAll)
|
|
* assert.deepStrictEqual(S2.concat(['a', 1, true], ['b', 2, false]), ['ab', 3, false])
|
|
*
|
|
* @since 2.10.0
|
|
*/
|
|
export var tuple = function () {
|
|
var semigroups = [];
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
semigroups[_i] = arguments[_i];
|
|
}
|
|
return ({
|
|
concat: function (first, second) { return semigroups.map(function (s, i) { return s.concat(first[i], second[i]); }); }
|
|
});
|
|
};
|
|
/**
|
|
* Between each pair of elements insert `middle`.
|
|
*
|
|
* @example
|
|
* import { intercalate } from 'fp-ts/Semigroup'
|
|
* import * as S from 'fp-ts/string'
|
|
* import { pipe } from 'fp-ts/function'
|
|
*
|
|
* const S1 = pipe(S.Semigroup, intercalate(' + '))
|
|
*
|
|
* assert.strictEqual(S1.concat('a', 'b'), 'a + b')
|
|
*
|
|
* @since 2.10.0
|
|
*/
|
|
export var intercalate = function (middle) {
|
|
return function (S) { return ({
|
|
concat: function (x, y) { return S.concat(x, S.concat(middle, y)); }
|
|
}); };
|
|
};
|
|
// -------------------------------------------------------------------------------------
|
|
// instances
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* Always return the first argument.
|
|
*
|
|
* @example
|
|
* import * as S from 'fp-ts/Semigroup'
|
|
*
|
|
* assert.deepStrictEqual(S.first<number>().concat(1, 2), 1)
|
|
*
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var first = function () { return ({ concat: identity }); };
|
|
/**
|
|
* Always return the last argument.
|
|
*
|
|
* @example
|
|
* import * as S from 'fp-ts/Semigroup'
|
|
*
|
|
* assert.deepStrictEqual(S.last<number>().concat(1, 2), 2)
|
|
*
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var last = function () { return ({ concat: function (_, y) { return y; } }); };
|
|
// -------------------------------------------------------------------------------------
|
|
// utils
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* Given a sequence of `as`, concat them and return the total.
|
|
*
|
|
* If `as` is empty, return the provided `startWith` value.
|
|
*
|
|
* @example
|
|
* import { concatAll } from 'fp-ts/Semigroup'
|
|
* import * as N from 'fp-ts/number'
|
|
*
|
|
* const sum = concatAll(N.SemigroupSum)(0)
|
|
*
|
|
* assert.deepStrictEqual(sum([1, 2, 3]), 6)
|
|
* assert.deepStrictEqual(sum([]), 0)
|
|
*
|
|
* @since 2.10.0
|
|
*/
|
|
export var concatAll = M.concatAll;
|
|
// -------------------------------------------------------------------------------------
|
|
// deprecated
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* Use `void` module instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var semigroupVoid = constant(undefined);
|
|
/**
|
|
* Use [`getAssignSemigroup`](./struct.ts.html#getAssignSemigroup) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var getObjectSemigroup = function () { return ({
|
|
concat: function (first, second) { return Object.assign({}, first, second); }
|
|
}); };
|
|
/**
|
|
* Use [`last`](#last) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var getLastSemigroup = last;
|
|
/**
|
|
* Use [`first`](#first) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var getFirstSemigroup = first;
|
|
/**
|
|
* Use [`tuple`](#tuple) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var getTupleSemigroup = tuple;
|
|
/**
|
|
* Use [`struct`](#struct) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var getStructSemigroup = struct;
|
|
/**
|
|
* Use [`reverse`](#reverse) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var getDualSemigroup = reverse;
|
|
/**
|
|
* Use [`max`](#max) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var getJoinSemigroup = max;
|
|
/**
|
|
* Use [`min`](#min) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var getMeetSemigroup = min;
|
|
/**
|
|
* Use [`intercalate`](#intercalate) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.5.0
|
|
* @deprecated
|
|
*/
|
|
export var getIntercalateSemigroup = intercalate;
|
|
export function fold(S) {
|
|
var concatAllS = concatAll(S);
|
|
return function (startWith, as) { return (as === undefined ? concatAllS(startWith) : concatAllS(startWith)(as)); };
|
|
}
|
|
/**
|
|
* Use [`SemigroupAll`](./boolean.ts.html#SemigroupAll) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var semigroupAll = {
|
|
concat: function (x, y) { return x && y; }
|
|
};
|
|
/**
|
|
* Use [`SemigroupAny`](./boolean.ts.html#SemigroupAny) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var semigroupAny = {
|
|
concat: function (x, y) { return x || y; }
|
|
};
|
|
/**
|
|
* Use [`getSemigroup`](./function.ts.html#getSemigroup) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var getFunctionSemigroup = getSemigroup;
|
|
/**
|
|
* Use [`Semigroup`](./string.ts.html#Semigroup) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var semigroupString = {
|
|
concat: function (x, y) { return x + y; }
|
|
};
|
|
/**
|
|
* Use [`SemigroupSum`](./number.ts.html#SemigroupSum) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var semigroupSum = {
|
|
concat: function (x, y) { return x + y; }
|
|
};
|
|
/**
|
|
* Use [`SemigroupProduct`](./number.ts.html#SemigroupProduct) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var semigroupProduct = {
|
|
concat: function (x, y) { return x * y; }
|
|
};
|