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
const createCompose = curry((F, G) => class Compose {
constructor(x) {
this.$value = x;
}
[util.inspect.custom]() {
return `Compose(${inspect(this.$value)})`;
}
// ----- Pointed (Compose F G)
static of(x) {
return new Compose(F(G(x)));
}
// ----- Functor (Compose F G)
map(fn) {
return new Compose(this.$value.map(x => x.map(fn)));
}
// ----- Applicative (Compose F G)
ap(f) {
return f.map(this.$value);
}
});Either
Left
Right
Identity
IO
List
Map
Maybe
Note that
Maybecould also be defined in a similar fashion as we did forEitherwith two child classesJustandNothing. This is simply a different flavor.
Task
Last updated
Was this helpful?