functional-abstraction/measure Home Manual Reference Source

src/0-core/0-base/Measure.js

  1. /**
  2. * Abstract Measure class.
  3. */
  4. export class Measure {
  5. /**
  6. * Constructor, stores the monoid.
  7. */
  8. constructor(monoid) {
  9. this.monoid = monoid;
  10. }
  11.  
  12. /**
  13. * Returns the zero measure according to the monoid.
  14. */
  15. zero() {
  16. return this.monoid.zero();
  17. }
  18.  
  19. /**
  20. * Adds measures together using monoid#plus.
  21. */
  22. plus(a, b, c = undefined) {
  23. if (c === undefined) return this.monoid.plus(a, b);
  24.  
  25. return this.monoid.plus(a, this.monoid.plus(b, c));
  26. }
  27.  
  28. /**
  29. * Returns the measure of an element. Must be implemented by classes
  30. * extending Measure
  31. */
  32. measure(_element) {
  33. throw new Error('measure : not implemented');
  34. }
  35. }