版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

410 lines
11 KiB

  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. 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;
  27. /**
  28. * If a type `A` can form a `Semigroup` it has an **associative** binary operation.
  29. *
  30. * ```ts
  31. * interface Semigroup<A> {
  32. * readonly concat: (x: A, y: A) => A
  33. * }
  34. * ```
  35. *
  36. * Associativity means the following equality must hold for any choice of `x`, `y`, and `z`.
  37. *
  38. * ```ts
  39. * concat(x, concat(y, z)) = concat(concat(x, y), z)
  40. * ```
  41. *
  42. * A common example of a semigroup is the type `string` with the operation `+`.
  43. *
  44. * ```ts
  45. * import { Semigroup } from 'fp-ts/Semigroup'
  46. *
  47. * const semigroupString: Semigroup<string> = {
  48. * concat: (x, y) => x + y
  49. * }
  50. *
  51. * const x = 'x'
  52. * const y = 'y'
  53. * const z = 'z'
  54. *
  55. * semigroupString.concat(x, y) // 'xy'
  56. *
  57. * semigroupString.concat(x, semigroupString.concat(y, z)) // 'xyz'
  58. *
  59. * semigroupString.concat(semigroupString.concat(x, y), z) // 'xyz'
  60. * ```
  61. *
  62. * *Adapted from https://typelevel.org/cats*
  63. *
  64. * @since 2.0.0
  65. */
  66. var function_1 = require("./function");
  67. var _ = __importStar(require("./internal"));
  68. var M = __importStar(require("./Magma"));
  69. var Or = __importStar(require("./Ord"));
  70. // -------------------------------------------------------------------------------------
  71. // constructors
  72. // -------------------------------------------------------------------------------------
  73. /**
  74. * Get a semigroup where `concat` will return the minimum, based on the provided order.
  75. *
  76. * @example
  77. * import * as N from 'fp-ts/number'
  78. * import * as S from 'fp-ts/Semigroup'
  79. *
  80. * const S1 = S.min(N.Ord)
  81. *
  82. * assert.deepStrictEqual(S1.concat(1, 2), 1)
  83. *
  84. * @category constructors
  85. * @since 2.10.0
  86. */
  87. var min = function (O) { return ({
  88. concat: Or.min(O)
  89. }); };
  90. exports.min = min;
  91. /**
  92. * Get a semigroup where `concat` will return the maximum, based on the provided order.
  93. *
  94. * @example
  95. * import * as N from 'fp-ts/number'
  96. * import * as S from 'fp-ts/Semigroup'
  97. *
  98. * const S1 = S.max(N.Ord)
  99. *
  100. * assert.deepStrictEqual(S1.concat(1, 2), 2)
  101. *
  102. * @category constructors
  103. * @since 2.10.0
  104. */
  105. var max = function (O) { return ({
  106. concat: Or.max(O)
  107. }); };
  108. exports.max = max;
  109. /**
  110. * @category constructors
  111. * @since 2.10.0
  112. */
  113. var constant = function (a) { return ({
  114. concat: function () { return a; }
  115. }); };
  116. exports.constant = constant;
  117. // -------------------------------------------------------------------------------------
  118. // combinators
  119. // -------------------------------------------------------------------------------------
  120. /**
  121. * The dual of a `Semigroup`, obtained by swapping the arguments of `concat`.
  122. *
  123. * @example
  124. * import { reverse } from 'fp-ts/Semigroup'
  125. * import * as S from 'fp-ts/string'
  126. *
  127. * assert.deepStrictEqual(reverse(S.Semigroup).concat('a', 'b'), 'ba')
  128. *
  129. * @since 2.10.0
  130. */
  131. exports.reverse = M.reverse;
  132. /**
  133. * Given a struct of semigroups returns a semigroup for the struct.
  134. *
  135. * @example
  136. * import { struct } from 'fp-ts/Semigroup'
  137. * import * as N from 'fp-ts/number'
  138. *
  139. * interface Point {
  140. * readonly x: number
  141. * readonly y: number
  142. * }
  143. *
  144. * const S = struct<Point>({
  145. * x: N.SemigroupSum,
  146. * y: N.SemigroupSum
  147. * })
  148. *
  149. * assert.deepStrictEqual(S.concat({ x: 1, y: 2 }, { x: 3, y: 4 }), { x: 4, y: 6 })
  150. *
  151. * @since 2.10.0
  152. */
  153. var struct = function (semigroups) { return ({
  154. concat: function (first, second) {
  155. var r = {};
  156. for (var k in semigroups) {
  157. if (_.has.call(semigroups, k)) {
  158. r[k] = semigroups[k].concat(first[k], second[k]);
  159. }
  160. }
  161. return r;
  162. }
  163. }); };
  164. exports.struct = struct;
  165. /**
  166. * Given a tuple of semigroups returns a semigroup for the tuple.
  167. *
  168. * @example
  169. * import { tuple } from 'fp-ts/Semigroup'
  170. * import * as B from 'fp-ts/boolean'
  171. * import * as N from 'fp-ts/number'
  172. * import * as S from 'fp-ts/string'
  173. *
  174. * const S1 = tuple(S.Semigroup, N.SemigroupSum)
  175. * assert.deepStrictEqual(S1.concat(['a', 1], ['b', 2]), ['ab', 3])
  176. *
  177. * const S2 = tuple(S.Semigroup, N.SemigroupSum, B.SemigroupAll)
  178. * assert.deepStrictEqual(S2.concat(['a', 1, true], ['b', 2, false]), ['ab', 3, false])
  179. *
  180. * @since 2.10.0
  181. */
  182. var tuple = function () {
  183. var semigroups = [];
  184. for (var _i = 0; _i < arguments.length; _i++) {
  185. semigroups[_i] = arguments[_i];
  186. }
  187. return ({
  188. concat: function (first, second) { return semigroups.map(function (s, i) { return s.concat(first[i], second[i]); }); }
  189. });
  190. };
  191. exports.tuple = tuple;
  192. /**
  193. * Between each pair of elements insert `middle`.
  194. *
  195. * @example
  196. * import { intercalate } from 'fp-ts/Semigroup'
  197. * import * as S from 'fp-ts/string'
  198. * import { pipe } from 'fp-ts/function'
  199. *
  200. * const S1 = pipe(S.Semigroup, intercalate(' + '))
  201. *
  202. * assert.strictEqual(S1.concat('a', 'b'), 'a + b')
  203. *
  204. * @since 2.10.0
  205. */
  206. var intercalate = function (middle) {
  207. return function (S) { return ({
  208. concat: function (x, y) { return S.concat(x, S.concat(middle, y)); }
  209. }); };
  210. };
  211. exports.intercalate = intercalate;
  212. // -------------------------------------------------------------------------------------
  213. // instances
  214. // -------------------------------------------------------------------------------------
  215. /**
  216. * Always return the first argument.
  217. *
  218. * @example
  219. * import * as S from 'fp-ts/Semigroup'
  220. *
  221. * assert.deepStrictEqual(S.first<number>().concat(1, 2), 1)
  222. *
  223. * @category instances
  224. * @since 2.10.0
  225. */
  226. var first = function () { return ({ concat: function_1.identity }); };
  227. exports.first = first;
  228. /**
  229. * Always return the last argument.
  230. *
  231. * @example
  232. * import * as S from 'fp-ts/Semigroup'
  233. *
  234. * assert.deepStrictEqual(S.last<number>().concat(1, 2), 2)
  235. *
  236. * @category instances
  237. * @since 2.10.0
  238. */
  239. var last = function () { return ({ concat: function (_, y) { return y; } }); };
  240. exports.last = last;
  241. // -------------------------------------------------------------------------------------
  242. // utils
  243. // -------------------------------------------------------------------------------------
  244. /**
  245. * Given a sequence of `as`, concat them and return the total.
  246. *
  247. * If `as` is empty, return the provided `startWith` value.
  248. *
  249. * @example
  250. * import { concatAll } from 'fp-ts/Semigroup'
  251. * import * as N from 'fp-ts/number'
  252. *
  253. * const sum = concatAll(N.SemigroupSum)(0)
  254. *
  255. * assert.deepStrictEqual(sum([1, 2, 3]), 6)
  256. * assert.deepStrictEqual(sum([]), 0)
  257. *
  258. * @since 2.10.0
  259. */
  260. exports.concatAll = M.concatAll;
  261. // -------------------------------------------------------------------------------------
  262. // deprecated
  263. // -------------------------------------------------------------------------------------
  264. /**
  265. * Use `void` module instead.
  266. *
  267. * @category zone of death
  268. * @since 2.0.0
  269. * @deprecated
  270. */
  271. exports.semigroupVoid = (0, exports.constant)(undefined);
  272. /**
  273. * Use [`getAssignSemigroup`](./struct.ts.html#getAssignSemigroup) instead.
  274. *
  275. * @category zone of death
  276. * @since 2.0.0
  277. * @deprecated
  278. */
  279. var getObjectSemigroup = function () { return ({
  280. concat: function (first, second) { return Object.assign({}, first, second); }
  281. }); };
  282. exports.getObjectSemigroup = getObjectSemigroup;
  283. /**
  284. * Use [`last`](#last) instead.
  285. *
  286. * @category zone of death
  287. * @since 2.0.0
  288. * @deprecated
  289. */
  290. exports.getLastSemigroup = exports.last;
  291. /**
  292. * Use [`first`](#first) instead.
  293. *
  294. * @category zone of death
  295. * @since 2.0.0
  296. * @deprecated
  297. */
  298. exports.getFirstSemigroup = exports.first;
  299. /**
  300. * Use [`tuple`](#tuple) instead.
  301. *
  302. * @category zone of death
  303. * @since 2.0.0
  304. * @deprecated
  305. */
  306. exports.getTupleSemigroup = exports.tuple;
  307. /**
  308. * Use [`struct`](#struct) instead.
  309. *
  310. * @category zone of death
  311. * @since 2.0.0
  312. * @deprecated
  313. */
  314. exports.getStructSemigroup = exports.struct;
  315. /**
  316. * Use [`reverse`](#reverse) instead.
  317. *
  318. * @category zone of death
  319. * @since 2.0.0
  320. * @deprecated
  321. */
  322. exports.getDualSemigroup = exports.reverse;
  323. /**
  324. * Use [`max`](#max) instead.
  325. *
  326. * @category zone of death
  327. * @since 2.0.0
  328. * @deprecated
  329. */
  330. exports.getJoinSemigroup = exports.max;
  331. /**
  332. * Use [`min`](#min) instead.
  333. *
  334. * @category zone of death
  335. * @since 2.0.0
  336. * @deprecated
  337. */
  338. exports.getMeetSemigroup = exports.min;
  339. /**
  340. * Use [`intercalate`](#intercalate) instead.
  341. *
  342. * @category zone of death
  343. * @since 2.5.0
  344. * @deprecated
  345. */
  346. exports.getIntercalateSemigroup = exports.intercalate;
  347. function fold(S) {
  348. var concatAllS = (0, exports.concatAll)(S);
  349. return function (startWith, as) { return (as === undefined ? concatAllS(startWith) : concatAllS(startWith)(as)); };
  350. }
  351. exports.fold = fold;
  352. /**
  353. * Use [`SemigroupAll`](./boolean.ts.html#SemigroupAll) instead.
  354. *
  355. * @category zone of death
  356. * @since 2.0.0
  357. * @deprecated
  358. */
  359. exports.semigroupAll = {
  360. concat: function (x, y) { return x && y; }
  361. };
  362. /**
  363. * Use [`SemigroupAny`](./boolean.ts.html#SemigroupAny) instead.
  364. *
  365. * @category zone of death
  366. * @since 2.0.0
  367. * @deprecated
  368. */
  369. exports.semigroupAny = {
  370. concat: function (x, y) { return x || y; }
  371. };
  372. /**
  373. * Use [`getSemigroup`](./function.ts.html#getSemigroup) instead.
  374. *
  375. * @category zone of death
  376. * @since 2.0.0
  377. * @deprecated
  378. */
  379. exports.getFunctionSemigroup = function_1.getSemigroup;
  380. /**
  381. * Use [`Semigroup`](./string.ts.html#Semigroup) instead.
  382. *
  383. * @category zone of death
  384. * @since 2.0.0
  385. * @deprecated
  386. */
  387. exports.semigroupString = {
  388. concat: function (x, y) { return x + y; }
  389. };
  390. /**
  391. * Use [`SemigroupSum`](./number.ts.html#SemigroupSum) instead.
  392. *
  393. * @category zone of death
  394. * @since 2.0.0
  395. * @deprecated
  396. */
  397. exports.semigroupSum = {
  398. concat: function (x, y) { return x + y; }
  399. };
  400. /**
  401. * Use [`SemigroupProduct`](./number.ts.html#SemigroupProduct) instead.
  402. *
  403. * @category zone of death
  404. * @since 2.0.0
  405. * @deprecated
  406. */
  407. exports.semigroupProduct = {
  408. concat: function (x, y) { return x * y; }
  409. };