Public
Edited
Feb 12, 2023
Insert cell
Insert cell
class Identity extends Inspectable {
constructor(x) {
super();
this.$value = x;
}

static of(x) {
return new Identity(x);
}

map(fn) {
return Identity.of(fn(this.$value));
}
}
Insert cell
// map :: Functor f => (a -> b) -> f a -> f b
map = R.curry((fn, functor) => functor.map(fn))
Insert cell
// append :: String -> String
append = R.curry((a,b) => b + a)
Insert cell
Identity.of("flamethrowers")
.map(append(" away"))
.map(R.prop("length"))
.inspect()
Insert cell
R.compose(map(append(" away")), Identity.of)("flamethrowers").inspect()
Insert cell
class Maybe extends Inspectable {
$value;

constructor(x) {
super();
this.$value = x;
}

static of(x) {
return new Maybe(x);
}

isNothing() {
return this.$value === null || this.$value === undefined;
}

map(fn) {
return this.isNothing() ? this : Maybe.of(fn(this.$value));
}

valueToString() {
return this.isNothing() ? 'Nothing' : `Just ${this.$value}`
}
}
Insert cell
Maybe.of("0123")
.map(R.prop(3))
.map((x) => "abc"[x])
.map((x) => x + x)
.inspect()
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more