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 peek 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
6
[util.inspect.custom]() {
7
return `Compose(${inspect(this.$value)})`;
8
}
9
10
// ----- Pointed (Compose F G)
11
static of(x) {
12
return new Compose(F(G(x)));
13
}
14
15
// ----- Functor (Compose F G)
16
map(fn) {
17
return new Compose(this.$value.map(x => x.map(fn)));
18
}
19
20
// ----- Applicative (Compose F G)
21
ap(f) {
22
return f.map(this.$value);
23
}
24
});
Copied!

Either

1
class Either {
2
constructor(x) {
3
this.$value = x;
4
}
5
6
// ----- Pointed (Either a)
7
static of(x) {
8
return new Right(x);
9
}
10
}
Copied!

Left

1
class Left extends Either {
2
get isLeft() {
3
return true;
4
}
5
6
get isRight() {
7
return false;
8
}
9
10
static of(x) {
11
throw new Error('`of` called on class Left (value) instead of Either (type)');
12
}
13
14
[util.inspect.custom]() {
15
return `Left(${inspect(this.$value)})`;
16
}
17
18
// ----- Functor (Either a)
19
map() {
20
return this;
21
}
22
23
// ----- Applicative (Either a)
24
ap() {
25
return this;
26
}
27
28
// ----- Monad (Either a)
29
chain() {
30
return this;
31
}
32
33
join() {
34
return this;
35
}
36
37
// ----- Traversable (Either a)
38
sequence(of) {
39
return of(this);
40
}
41
42
traverse(of, fn) {
43
return of(this);
44
}
45
}
Copied!
1
class Right extends Either {
2
get isLeft() {
3
return false;
4
}
5
6
get isRight() {
7
return true;
8
}
9
10
static of(x) {
11
throw new Error('`of` called on class Right (value) instead of Either (type)');
12
}
13
14
[util.inspect.custom]() {
15
return `Right(${inspect(this.$value)})`;
16
}
17
18
// ----- Functor (Either a)
19
map(fn) {
20
return Either.of(fn(this.$value));
21
}
22
23
// ----- Applicative (Either a)
24
ap(f) {
25
return f.map(this.$value);
26
}
27
28
// ----- Monad (Either a)
29
chain(fn) {
30
return fn(this.$value);
31
}
32
33
join() {
34
return this.$value;
35
}
36
37
// ----- Traversable (Either a)
38
sequence(of) {
39
return this.traverse(of, identity);
40
}
41
42
traverse(of, fn) {
43
fn(this.$value).map(Either.of);
44
}
45
}
Copied!

Identity

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

IO

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

List

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

Map

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

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
6
get isJust() {
7
return !this.isNothing;
8
}
9
10
constructor(x) {
11
this.$value = x;
12
}
13
14
[util.inspect.custom]() {
15
return this.isNothing ? 'Nothing' : `Just(${inspect(this.$value)})`;
16
}
17
18
// ----- Pointed Maybe
19
static of(x) {
20
return new Maybe(x);
21
}
22
23
// ----- Functor Maybe
24
map(fn) {
25
return this.isNothing ? this : Maybe.of(fn(this.$value));
26
}
27
28
// ----- Applicative Maybe
29
ap(f) {
30
return this.isNothing ? this : f.map(this.$value);
31
}
32
33
// ----- Monad Maybe
34
chain(fn) {
35
return this.map(fn).join();
36
}
37
38
join() {
39
return this.isNothing ? this : this.$value;
40
}
41
42
// ----- Traversable Maybe
43
sequence(of) {
44
return this.traverse(of, identity);
45
}
46
47
traverse(of, fn) {
48
return this.isNothing ? of(this) : fn(this.$value).map(Maybe.of);
49
}
50
}
Copied!

Task

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