版博士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.

FoldableWithIndex.js 2.5 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getFoldableWithIndexComposition = exports.reduceRightWithIndex = exports.foldMapWithIndex = exports.reduceWithIndex = void 0;
  4. /**
  5. * A `Foldable` with an additional index.
  6. * A `FoldableWithIndex` instance must be compatible with its `Foldable` instance
  7. *
  8. * ```ts
  9. * reduce(fa, b, f) = reduceWithIndex(fa, b, (_, b, a) => f(b, a))
  10. * foldMap(M)(fa, f) = foldMapWithIndex(M)(fa, (_, a) => f(a))
  11. * reduceRight(fa, b, f) = reduceRightWithIndex(fa, b, (_, a, b) => f(a, b))
  12. * ```
  13. *
  14. * @since 2.0.0
  15. */
  16. var Foldable_1 = require("./Foldable");
  17. var function_1 = require("./function");
  18. function reduceWithIndex(F, G) {
  19. return function (b, f) { return function (fga) {
  20. return F.reduceWithIndex(fga, b, function (i, b, ga) { return G.reduceWithIndex(ga, b, function (j, b, a) { return f([i, j], b, a); }); });
  21. }; };
  22. }
  23. exports.reduceWithIndex = reduceWithIndex;
  24. function foldMapWithIndex(F, G) {
  25. return function (M) {
  26. var foldMapWithIndexF = F.foldMapWithIndex(M);
  27. var foldMapWithIndexG = G.foldMapWithIndex(M);
  28. return function (f) { return function (fga) { return foldMapWithIndexF(fga, function (i, ga) { return foldMapWithIndexG(ga, function (j, a) { return f([i, j], a); }); }); }; };
  29. };
  30. }
  31. exports.foldMapWithIndex = foldMapWithIndex;
  32. function reduceRightWithIndex(F, G) {
  33. return function (b, f) { return function (fga) {
  34. return F.reduceRightWithIndex(fga, b, function (i, ga, b) { return G.reduceRightWithIndex(ga, b, function (j, a, b) { return f([i, j], a, b); }); });
  35. }; };
  36. }
  37. exports.reduceRightWithIndex = reduceRightWithIndex;
  38. /** @deprecated */
  39. function getFoldableWithIndexComposition(F, G) {
  40. var FC = (0, Foldable_1.getFoldableComposition)(F, G);
  41. var _reduceWithIndex = reduceWithIndex(F, G);
  42. var _foldMapWithIndex = foldMapWithIndex(F, G);
  43. var _reduceRightWithIndex = reduceRightWithIndex(F, G);
  44. return {
  45. reduce: FC.reduce,
  46. foldMap: FC.foldMap,
  47. reduceRight: FC.reduceRight,
  48. reduceWithIndex: function (fga, b, f) { return (0, function_1.pipe)(fga, _reduceWithIndex(b, f)); },
  49. foldMapWithIndex: function (M) {
  50. var foldMapWithIndexM = _foldMapWithIndex(M);
  51. return function (fga, f) { return (0, function_1.pipe)(fga, foldMapWithIndexM(f)); };
  52. },
  53. reduceRightWithIndex: function (fga, b, f) { return (0, function_1.pipe)(fga, _reduceRightWithIndex(b, f)); }
  54. };
  55. }
  56. exports.getFoldableWithIndexComposition = getFoldableWithIndexComposition;