Lampiran B: Dukungan Struktur Aljabar
Dalam lampiran ini, Anda akan menemukan beberapa implementasi JavaScript dasar dari berbagai struktur aljabar yang dijelaskan dalam buku ini.
Ingatlah bahwa implementasi ini mungkin bukan implementasi tercepat atau paling efisien di luar sana; mereka dibuat hanya untuk tujuan pendidikan.
Untuk menemukan struktur yang lebih siap produksi, intip folktale atau fantasy-land.
Perhatikan bahwa beberapa metode juga merujuk ke fungsi yang didefinisikan dalam Lampiran 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?