|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- "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<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
- */
- 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<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
- */
- 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<number>().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<number>().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; }
- };
|