版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

84 строки
2.5 KiB

  1. "use strict";
  2. /**
  3. * A `Magma` is a pair `(A, concat)` in which `A` is a non-empty set and `concat` is a binary operation on `A`
  4. *
  5. * See [Semigroup](https://gcanti.github.io/fp-ts/modules/Semigroup.ts.html) for some instances.
  6. *
  7. * @since 2.0.0
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.concatAll = exports.endo = exports.filterSecond = exports.filterFirst = exports.reverse = void 0;
  11. // -------------------------------------------------------------------------------------
  12. // combinators
  13. // -------------------------------------------------------------------------------------
  14. /**
  15. * The dual of a `Magma`, obtained by swapping the arguments of `concat`.
  16. *
  17. * @example
  18. * import { reverse, concatAll } from 'fp-ts/Magma'
  19. * import * as N from 'fp-ts/number'
  20. *
  21. * const subAll = concatAll(reverse(N.MagmaSub))(0)
  22. *
  23. * assert.deepStrictEqual(subAll([1, 2, 3]), 2)
  24. *
  25. * @since 2.11.0
  26. */
  27. var reverse = function (M) { return ({
  28. concat: function (first, second) { return M.concat(second, first); }
  29. }); };
  30. exports.reverse = reverse;
  31. /**
  32. * @since 2.11.0
  33. */
  34. var filterFirst = function (predicate) {
  35. return function (M) { return ({
  36. concat: function (first, second) { return (predicate(first) ? M.concat(first, second) : second); }
  37. }); };
  38. };
  39. exports.filterFirst = filterFirst;
  40. /**
  41. * @since 2.11.0
  42. */
  43. var filterSecond = function (predicate) {
  44. return function (M) { return ({
  45. concat: function (first, second) { return (predicate(second) ? M.concat(first, second) : first); }
  46. }); };
  47. };
  48. exports.filterSecond = filterSecond;
  49. /**
  50. * @since 2.11.0
  51. */
  52. var endo = function (f) {
  53. return function (M) { return ({
  54. concat: function (first, second) { return M.concat(f(first), f(second)); }
  55. }); };
  56. };
  57. exports.endo = endo;
  58. // -------------------------------------------------------------------------------------
  59. // utils
  60. // -------------------------------------------------------------------------------------
  61. /**
  62. * Given a sequence of `as`, concat them and return the total.
  63. *
  64. * If `as` is empty, return the provided `startWith` value.
  65. *
  66. * @example
  67. * import { concatAll } from 'fp-ts/Magma'
  68. * import * as N from 'fp-ts/number'
  69. *
  70. * const subAll = concatAll(N.MagmaSub)(0)
  71. *
  72. * assert.deepStrictEqual(subAll([1, 2, 3]), -6)
  73. *
  74. * @since 2.11.0
  75. */
  76. var concatAll = function (M) {
  77. return function (startWith) {
  78. return function (as) {
  79. return as.reduce(function (a, acc) { return M.concat(a, acc); }, startWith);
  80. };
  81. };
  82. };
  83. exports.concatAll = concatAll;