版博士V2.0程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

Alternative.js 1.2 KiB

123456789101112131415161718192021222324252627282930313233
  1. /**
  2. * The `Alternative` type class extends the `Alt` type class with a value that should be the left and right identity for `alt`.
  3. *
  4. * It is similar to `Monoid`, except that it applies to types of kind `* -> *`, like `Array` or `Option`, rather than
  5. * concrete types like `string` or `number`.
  6. *
  7. * `Alternative` instances should satisfy the following laws:
  8. *
  9. * 1. Left identity: `A.alt(zero, fa) <-> fa`
  10. * 2. Right identity: `A.alt(fa, zero) <-> fa`
  11. * 3. Annihilation: `A.map(zero, f) <-> zero`
  12. * 4. Distributivity: `A.ap(A.alt(fab, gab), fa) <-> A.alt(A.ap(fab, fa), A.ap(gab, fa))`
  13. * 5. Annihilation: `A.ap(zero, fa) <-> zero`
  14. *
  15. * @since 2.0.0
  16. */
  17. import { altAll as altAll_ } from './Alt';
  18. import { getApplySemigroup } from './Apply';
  19. export function altAll(F) {
  20. return altAll_(F)(F.zero());
  21. }
  22. export function getAlternativeMonoid(F) {
  23. var f = getApplySemigroup(F);
  24. return function (S) {
  25. var SF = f(S);
  26. return {
  27. concat: function (first, second) {
  28. return F.alt(SF.concat(first, second), function () { return F.alt(first, function () { return second; }); });
  29. },
  30. empty: F.zero()
  31. };
  32. };
  33. }