版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

279 líneas
6.6 KiB

  1. import { getMonoid as getFM } from './function';
  2. import { getMonoid as getEM } from './Endomorphism';
  3. import * as _ from './internal';
  4. import * as Se from './Semigroup';
  5. // -------------------------------------------------------------------------------------
  6. // constructors
  7. // -------------------------------------------------------------------------------------
  8. /**
  9. * Get a monoid where `concat` will return the minimum, based on the provided bounded order.
  10. *
  11. * The `empty` value is the `top` value.
  12. *
  13. * @example
  14. * import * as N from 'fp-ts/number'
  15. * import * as M from 'fp-ts/Monoid'
  16. *
  17. * const M1 = M.min(N.Bounded)
  18. *
  19. * assert.deepStrictEqual(M1.concat(1, 2), 1)
  20. *
  21. * @category constructors
  22. * @since 2.10.0
  23. */
  24. export var min = function (B) { return ({
  25. concat: Se.min(B).concat,
  26. empty: B.top
  27. }); };
  28. /**
  29. * Get a monoid where `concat` will return the maximum, based on the provided bounded order.
  30. *
  31. * The `empty` value is the `bottom` value.
  32. *
  33. * @example
  34. * import * as N from 'fp-ts/number'
  35. * import * as M from 'fp-ts/Monoid'
  36. *
  37. * const M1 = M.max(N.Bounded)
  38. *
  39. * assert.deepStrictEqual(M1.concat(1, 2), 2)
  40. *
  41. * @category constructors
  42. * @since 2.10.0
  43. */
  44. export var max = function (B) { return ({
  45. concat: Se.max(B).concat,
  46. empty: B.bottom
  47. }); };
  48. // -------------------------------------------------------------------------------------
  49. // combinators
  50. // -------------------------------------------------------------------------------------
  51. /**
  52. * The dual of a `Monoid`, obtained by swapping the arguments of `concat`.
  53. *
  54. * @example
  55. * import { reverse } from 'fp-ts/Monoid'
  56. * import * as S from 'fp-ts/string'
  57. *
  58. * assert.deepStrictEqual(reverse(S.Monoid).concat('a', 'b'), 'ba')
  59. *
  60. * @since 2.10.0
  61. */
  62. export var reverse = function (M) { return ({
  63. concat: Se.reverse(M).concat,
  64. empty: M.empty
  65. }); };
  66. /**
  67. * Given a struct of monoids returns a monoid for the struct.
  68. *
  69. * @example
  70. * import { struct } from 'fp-ts/Monoid'
  71. * import * as N from 'fp-ts/number'
  72. *
  73. * interface Point {
  74. * readonly x: number
  75. * readonly y: number
  76. * }
  77. *
  78. * const M = struct<Point>({
  79. * x: N.MonoidSum,
  80. * y: N.MonoidSum
  81. * })
  82. *
  83. * assert.deepStrictEqual(M.concat({ x: 1, y: 2 }, { x: 3, y: 4 }), { x: 4, y: 6 })
  84. *
  85. * @since 2.10.0
  86. */
  87. export var struct = function (monoids) {
  88. var empty = {};
  89. for (var k in monoids) {
  90. if (_.has.call(monoids, k)) {
  91. empty[k] = monoids[k].empty;
  92. }
  93. }
  94. return {
  95. concat: Se.struct(monoids).concat,
  96. empty: empty
  97. };
  98. };
  99. /**
  100. * Given a tuple of monoids returns a monoid for the tuple.
  101. *
  102. * @example
  103. * import { tuple } from 'fp-ts/Monoid'
  104. * import * as B from 'fp-ts/boolean'
  105. * import * as N from 'fp-ts/number'
  106. * import * as S from 'fp-ts/string'
  107. *
  108. * const M1 = tuple(S.Monoid, N.MonoidSum)
  109. * assert.deepStrictEqual(M1.concat(['a', 1], ['b', 2]), ['ab', 3])
  110. *
  111. * const M2 = tuple(S.Monoid, N.MonoidSum, B.MonoidAll)
  112. * assert.deepStrictEqual(M2.concat(['a', 1, true], ['b', 2, false]), ['ab', 3, false])
  113. *
  114. * @since 2.10.0
  115. */
  116. export var tuple = function () {
  117. var monoids = [];
  118. for (var _i = 0; _i < arguments.length; _i++) {
  119. monoids[_i] = arguments[_i];
  120. }
  121. return ({
  122. concat: Se.tuple.apply(Se, monoids).concat,
  123. empty: monoids.map(function (m) { return m.empty; })
  124. });
  125. };
  126. // -------------------------------------------------------------------------------------
  127. // utils
  128. // -------------------------------------------------------------------------------------
  129. /**
  130. * Given a sequence of `as`, concat them and return the total.
  131. *
  132. * If `as` is empty, return the monoid `empty` value.
  133. *
  134. * @example
  135. * import { concatAll } from 'fp-ts/Monoid'
  136. * import * as N from 'fp-ts/number'
  137. *
  138. * assert.deepStrictEqual(concatAll(N.MonoidSum)([1, 2, 3]), 6)
  139. * assert.deepStrictEqual(concatAll(N.MonoidSum)([]), 0)
  140. *
  141. * @since 2.10.0
  142. */
  143. export var concatAll = function (M) { return Se.concatAll(M)(M.empty); };
  144. // -------------------------------------------------------------------------------------
  145. // deprecated
  146. // -------------------------------------------------------------------------------------
  147. /**
  148. * Use [`Monoid`](./void.ts.html#monoid) instead.
  149. *
  150. * @category zone of death
  151. * @since 2.0.0
  152. * @deprecated
  153. */
  154. export var monoidVoid = {
  155. concat: Se.semigroupVoid.concat,
  156. empty: undefined
  157. };
  158. /**
  159. * Use [`tuple`](#tuple) instead.
  160. *
  161. * @category zone of death
  162. * @since 2.0.0
  163. * @deprecated
  164. */
  165. export var getTupleMonoid = tuple;
  166. /**
  167. * Use [`struct`](#struct) instead.
  168. *
  169. * @category zone of death
  170. * @since 2.0.0
  171. * @deprecated
  172. */
  173. export var getStructMonoid = struct;
  174. /**
  175. * Use [`reverse`](#reverse) instead.
  176. *
  177. * @category zone of death
  178. * @since 2.0.0
  179. * @deprecated
  180. */
  181. export var getDualMonoid = reverse;
  182. /**
  183. * Use [`max`](#max) instead.
  184. *
  185. * @category zone of death
  186. * @since 2.0.0
  187. * @deprecated
  188. */
  189. export var getJoinMonoid = max;
  190. /**
  191. * Use [`min`](#min) instead.
  192. *
  193. * @category zone of death
  194. * @since 2.0.0
  195. * @deprecated
  196. */
  197. export var getMeetMonoid = min;
  198. /**
  199. * Use [`concatAll`](#concatall) instead.
  200. *
  201. * @category zone of death
  202. * @since 2.0.0
  203. * @deprecated
  204. */
  205. export var fold = concatAll;
  206. /**
  207. * Use [`MonoidAll`](./boolean.ts.html#monoidall) instead.
  208. *
  209. * @category zone of death
  210. * @since 2.0.0
  211. * @deprecated
  212. */
  213. export var monoidAll = {
  214. concat: Se.semigroupAll.concat,
  215. empty: true
  216. };
  217. /**
  218. * Use [`MonoidAny`](./boolean.ts.html#monoidany) instead.
  219. *
  220. * @category zone of death
  221. * @since 2.0.0
  222. * @deprecated
  223. */
  224. export var monoidAny = {
  225. concat: Se.semigroupAny.concat,
  226. empty: false
  227. };
  228. /**
  229. * Use [`getMonoid`](./function.ts.html#getmonoid) instead.
  230. *
  231. * @category zone of death
  232. * @since 2.0.0
  233. * @deprecated
  234. */
  235. export var getFunctionMonoid = getFM;
  236. /**
  237. * Use [`getEndomorphismMonoid`](./function.ts.html#getendomorphismmonoid) instead.
  238. *
  239. * **Note**. The execution order in [`getEndomorphismMonoid`](./function.ts.html#getendomorphismmonoid) is reversed.
  240. *
  241. * @category zone of death
  242. * @since 2.0.0
  243. * @deprecated
  244. */
  245. export var getEndomorphismMonoid = function () { return reverse(getEM()); };
  246. /**
  247. * Use [`Monoid`](./string.ts.html#monoid) instead.
  248. *
  249. * @category zone of death
  250. * @since 2.0.0
  251. * @deprecated
  252. */
  253. export var monoidString = {
  254. concat: Se.semigroupString.concat,
  255. empty: ''
  256. };
  257. /**
  258. * Use [`MonoidSum`](./number.ts.html#monoidsum) instead.
  259. *
  260. * @category zone of death
  261. * @since 2.0.0
  262. * @deprecated
  263. */
  264. export var monoidSum = {
  265. concat: Se.semigroupSum.concat,
  266. empty: 0
  267. };
  268. /**
  269. * Use [`MonoidProduct`](./number.ts.html#monoidproduct) instead.
  270. *
  271. * @category zone of death
  272. * @since 2.0.0
  273. * @deprecated
  274. */
  275. export var monoidProduct = {
  276. concat: Se.semigroupProduct.concat,
  277. empty: 1
  278. };