版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

76 wiersze
2.2 KiB

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