"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.semigroupProduct = exports.semigroupSum = exports.semigroupString = exports.getFunctionSemigroup = exports.semigroupAny = exports.semigroupAll = exports.fold = exports.getIntercalateSemigroup = exports.getMeetSemigroup = exports.getJoinSemigroup = exports.getDualSemigroup = exports.getStructSemigroup = exports.getTupleSemigroup = exports.getFirstSemigroup = exports.getLastSemigroup = exports.getObjectSemigroup = exports.semigroupVoid = exports.concatAll = exports.last = exports.first = exports.intercalate = exports.tuple = exports.struct = exports.reverse = exports.constant = exports.max = exports.min = void 0; /** * If a type `A` can form a `Semigroup` it has an **associative** binary operation. * * ```ts * interface Semigroup { * 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 = { * 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 */ var function_1 = require("./function"); var _ = __importStar(require("./internal")); var M = __importStar(require("./Magma")); var Or = __importStar(require("./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 */ var min = function (O) { return ({ concat: Or.min(O) }); }; exports.min = min; /** * 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 */ var max = function (O) { return ({ concat: Or.max(O) }); }; exports.max = max; /** * @category constructors * @since 2.10.0 */ var constant = function (a) { return ({ concat: function () { return a; } }); }; exports.constant = constant; // ------------------------------------------------------------------------------------- // 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 */ exports.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({ * 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 */ 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; } }); }; exports.struct = struct; /** * 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 */ 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]); }); } }); }; exports.tuple = tuple; /** * 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 */ var intercalate = function (middle) { return function (S) { return ({ concat: function (x, y) { return S.concat(x, S.concat(middle, y)); } }); }; }; exports.intercalate = intercalate; // ------------------------------------------------------------------------------------- // instances // ------------------------------------------------------------------------------------- /** * Always return the first argument. * * @example * import * as S from 'fp-ts/Semigroup' * * assert.deepStrictEqual(S.first().concat(1, 2), 1) * * @category instances * @since 2.10.0 */ var first = function () { return ({ concat: function_1.identity }); }; exports.first = first; /** * Always return the last argument. * * @example * import * as S from 'fp-ts/Semigroup' * * assert.deepStrictEqual(S.last().concat(1, 2), 2) * * @category instances * @since 2.10.0 */ var last = function () { return ({ concat: function (_, y) { return y; } }); }; exports.last = last; // ------------------------------------------------------------------------------------- // 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 */ exports.concatAll = M.concatAll; // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- /** * Use `void` module instead. * * @category zone of death * @since 2.0.0 * @deprecated */ exports.semigroupVoid = (0, exports.constant)(undefined); /** * Use [`getAssignSemigroup`](./struct.ts.html#getAssignSemigroup) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ var getObjectSemigroup = function () { return ({ concat: function (first, second) { return Object.assign({}, first, second); } }); }; exports.getObjectSemigroup = getObjectSemigroup; /** * Use [`last`](#last) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ exports.getLastSemigroup = exports.last; /** * Use [`first`](#first) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ exports.getFirstSemigroup = exports.first; /** * Use [`tuple`](#tuple) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ exports.getTupleSemigroup = exports.tuple; /** * Use [`struct`](#struct) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ exports.getStructSemigroup = exports.struct; /** * Use [`reverse`](#reverse) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ exports.getDualSemigroup = exports.reverse; /** * Use [`max`](#max) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ exports.getJoinSemigroup = exports.max; /** * Use [`min`](#min) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ exports.getMeetSemigroup = exports.min; /** * Use [`intercalate`](#intercalate) instead. * * @category zone of death * @since 2.5.0 * @deprecated */ exports.getIntercalateSemigroup = exports.intercalate; function fold(S) { var concatAllS = (0, exports.concatAll)(S); return function (startWith, as) { return (as === undefined ? concatAllS(startWith) : concatAllS(startWith)(as)); }; } exports.fold = fold; /** * Use [`SemigroupAll`](./boolean.ts.html#SemigroupAll) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ exports.semigroupAll = { concat: function (x, y) { return x && y; } }; /** * Use [`SemigroupAny`](./boolean.ts.html#SemigroupAny) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ exports.semigroupAny = { concat: function (x, y) { return x || y; } }; /** * Use [`getSemigroup`](./function.ts.html#getSemigroup) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ exports.getFunctionSemigroup = function_1.getSemigroup; /** * Use [`Semigroup`](./string.ts.html#Semigroup) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ exports.semigroupString = { concat: function (x, y) { return x + y; } }; /** * Use [`SemigroupSum`](./number.ts.html#SemigroupSum) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ exports.semigroupSum = { concat: function (x, y) { return x + y; } }; /** * Use [`SemigroupProduct`](./number.ts.html#SemigroupProduct) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ exports.semigroupProduct = { concat: function (x, y) { return x * y; } };