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

50 строки
2.1 KiB

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