Appendix B: Algebraic Structures Support

In this appendix, you’ll find some basic JavaScript implementations of various algebraic
structures described in the book. Keep in mind that these implementations may not be the fastest or the
most efficient implementation out there; they solely serve an educational purpose.

In order to find structures that are more production-ready, have a peak at folktale
or fantasy-land.

Note that some methods also refer to functions defined in the Appendix A

Compose

  1. const createCompose = curry((F, G) => class Compose {
  2. constructor(x) {
  3. this.$value = x;
  4. }
  5. inspect() {
  6. return `Compose(${inspect(this.$value)})`;
  7. }
  8. // ----- Pointed (Compose F G)
  9. static of(x) {
  10. return new Compose(F(G(x)));
  11. }
  12. // ----- Functor (Compose F G)
  13. map(fn) {
  14. return new Compose(this.$value.map(x => x.map(fn)));
  15. }
  16. // ----- Applicative (Compose F G)
  17. ap(f) {
  18. return f.map(this.$value);
  19. }
  20. });

Either

  1. class Either {
  2. constructor(x) {
  3. this.$value = x;
  4. }
  5. // ----- Pointed (Either a)
  6. static of(x) {
  7. return new Right(x);
  8. }
  9. }
  10. class Left extends Either {
  11. get isLeft() {
  12. return true;
  13. }
  14. get isRight() {
  15. return false;
  16. }
  17. static of(x) {
  18. throw new Error('`of` called on class Left (value) instead of Either (type)');
  19. }
  20. inspect() {
  21. return `Left(${inspect(this.$value)})`;
  22. }
  23. // ----- Functor (Either a)
  24. map() {
  25. return this;
  26. }
  27. // ----- Applicative (Either a)
  28. ap() {
  29. return this;
  30. }
  31. // ----- Monad (Either a)
  32. chain() {
  33. return this;
  34. }
  35. join() {
  36. return this;
  37. }
  38. // ----- Traversable (Either a)
  39. sequence(of) {
  40. return of(this);
  41. }
  42. traverse(of, fn) {
  43. return of(this);
  44. }
  45. }
  46. class Right extends Either {
  47. get isLeft() {
  48. return false;
  49. }
  50. get isRight() {
  51. return true;
  52. }
  53. static of(x) {
  54. throw new Error('`of` called on class Right (value) instead of Either (type)');
  55. }
  56. inspect() {
  57. return `Right(${inspect(this.$value)})`;
  58. }
  59. // ----- Functor (Either a)
  60. map(fn) {
  61. return Either.of(fn(this.$value));
  62. }
  63. // ----- Applicative (Either a)
  64. ap(f) {
  65. return f.map(this.$value);
  66. }
  67. // ----- Monad (Either a)
  68. chain(fn) {
  69. return fn(this.$value);
  70. }
  71. join() {
  72. return this.$value;
  73. }
  74. // ----- Traversable (Either a)
  75. sequence(of) {
  76. return this.traverse(of, identity);
  77. }
  78. traverse(of, fn) {
  79. fn(this.$value).map(Either.of);
  80. }
  81. }

Identity

  1. class Identity {
  2. constructor(x) {
  3. this.$value = x;
  4. }
  5. inspect() {
  6. return `Identity(${inspect(this.$value)})`;
  7. }
  8. // ----- Pointed Identity
  9. static of(x) {
  10. return new Identity(x);
  11. }
  12. // ----- Functor Identity
  13. map(fn) {
  14. return Identity.of(fn(this.$value));
  15. }
  16. // ----- Applicative Identity
  17. ap(f) {
  18. return f.map(this.$value);
  19. }
  20. // ----- Monad Identity
  21. chain(fn) {
  22. return this.map(fn).join();
  23. }
  24. join() {
  25. return this.$value;
  26. }
  27. // ----- Traversable Identity
  28. sequence(of) {
  29. return this.traverse(of, identity);
  30. }
  31. traverse(of, fn) {
  32. return fn(this.$value).map(Identity.of);
  33. }
  34. }

IO

  1. class IO {
  2. constructor(fn) {
  3. this.unsafePerformIO = fn;
  4. }
  5. inspect() {
  6. return `IO(?)`;
  7. }
  8. // ----- Pointed IO
  9. static of(x) {
  10. return new IO(() => x);
  11. }
  12. // ----- Functor IO
  13. map(fn) {
  14. return new IO(compose(fn, this.unsafePerformIO));
  15. }
  16. // ----- Applicative IO
  17. ap(f) {
  18. return this.chain(fn => f.map(fn));
  19. }
  20. // ----- Monad IO
  21. chain(fn) {
  22. return this.map(fn).join();
  23. }
  24. join() {
  25. return this.unsafePerformIO();
  26. }
  27. }

List

  1. class List {
  2. constructor(xs) {
  3. this.$value = xs;
  4. }
  5. inspect() {
  6. return `List(${inspect(this.$value)})`;
  7. }
  8. concat(x) {
  9. return new List(this.$value.concat(x));
  10. }
  11. // ----- Pointed List
  12. static of(x) {
  13. return new List([x]);
  14. }
  15. // ----- Functor List
  16. map(fn) {
  17. return new List(this.$value.map(fn));
  18. }
  19. // ----- Traversable List
  20. sequence(of) {
  21. return this.traverse(of, identity);
  22. }
  23. traverse(of, fn) {
  24. return this.$value.reduce(
  25. (f, a) => fn(a).map(b => bs => bs.concat(b)).ap(f),
  26. of(new List([])),
  27. );
  28. }
  29. }

Map

  1. class Map {
  2. constructor(x) {
  3. this.$value = x;
  4. }
  5. inspect() {
  6. return `Map(${inspect(this.$value)})`;
  7. }
  8. insert(k, v) {
  9. const singleton = {};
  10. singleton[k] = v;
  11. return Map.of(Object.assign({}, this.$value, singleton));
  12. }
  13. reduceWithKeys(fn, zero) {
  14. return Object.keys(this.$value)
  15. .reduce((acc, k) => fn(acc, this.$value[k], k), zero);
  16. }
  17. // ----- Functor (Map a)
  18. map(fn) {
  19. return this.reduceWithKeys(
  20. (m, v, k) => m.insert(k, fn(v)),
  21. new Map({}),
  22. );
  23. }
  24. // ----- Traversable (Map a)
  25. sequence(of) {
  26. return this.traverse(of, identity);
  27. }
  28. traverse(of, fn) {
  29. return this.reduceWithKeys(
  30. (f, a, k) => fn(a).map(b => m => m.insert(k, b)).ap(f),
  31. of(new Map({})),
  32. );
  33. }
  34. }

Maybe

Note that Maybe could also be defined in a similar fashion as we did for Either with two
child classes Just and Nothing. This is simply a different flavor.

  1. class Maybe {
  2. get isNothing() {
  3. return this.$value === null || this.$value === undefined;
  4. }
  5. get isJust() {
  6. return !this.isNothing;
  7. }
  8. constructor(x) {
  9. this.$value = x;
  10. }
  11. inspect() {
  12. return `Maybe(${inspect(this.$value)})`;
  13. }
  14. // ----- Pointed Maybe
  15. static of(x) {
  16. return new Maybe(x);
  17. }
  18. // ----- Functor Maybe
  19. map(fn) {
  20. return this.isNothing ? this : Maybe.of(fn(this.$value));
  21. }
  22. // ----- Applicative Maybe
  23. ap(f) {
  24. return this.isNothing ? this : f.map(this.$value);
  25. }
  26. // ----- Monad Maybe
  27. chain(fn) {
  28. return this.map(fn).join();
  29. }
  30. join() {
  31. return this.isNothing ? this : this.$value;
  32. }
  33. // ----- Traversable Maybe
  34. sequence(of) {
  35. this.traverse(of, identity);
  36. }
  37. traverse(of, fn) {
  38. return this.isNothing ? of(this) : fn(this.$value).map(Maybe.of);
  39. }
  40. }

Task

  1. class Task {
  2. constructor(fork) {
  3. this.fork = fork;
  4. }
  5. inspect() {
  6. return 'Task(?)';
  7. }
  8. static rejected(x) {
  9. return new Task((reject, _) => reject(x));
  10. }
  11. // ----- Pointed (Task a)
  12. static of(x) {
  13. return new Task((_, resolve) => resolve(x));
  14. }
  15. // ----- Functor (Task a)
  16. map(fn) {
  17. return new Task((reject, resolve) => this.fork(reject, compose(resolve, fn)));
  18. }
  19. // ----- Applicative (Task a)
  20. ap(f) {
  21. return this.chain(fn => f.map(fn));
  22. }
  23. // ----- Monad (Task a)
  24. chain(fn) {
  25. return new Task((reject, resolve) => this.fork(reject, x => fn(x).fork(reject, resolve)));
  26. }
  27. join() {
  28. return this.chain(identity);
  29. }
  30. }