Published
Edited
Jun 2, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Identity = {
const Identity = x => ({
emit: () => x, // other names: join, value, valueOf
chain: f => f(x),
map: f => Identity(f(x)),
inspect: () => `Identity(${x})`
});
Identity.of = x => Identity(x);
return Identity;
}
Insert cell
one = Identity(1)
Insert cell
Insert cell
Insert cell
one.emit()
Insert cell
Insert cell
one.chain(a => a + 1)
Insert cell
Insert cell
one.chain(add1)
Insert cell
Insert cell
{
const two = one.map(a => a + 1);
return [one.emit(), two.emit()];
}
Insert cell
Insert cell
{
const one = Identity(1)
const two = one.map(add1)
return two.inspect()
}
Insert cell
Insert cell
three = Identity.of(3)
Insert cell
Insert cell
List = {
const List = x => ({
emit: () => x,
chain: f => f(x),
map: f => List.of(f(x)),
inspect: () => `List(${x})`,
concat: a => List.of(x.concat(a)),
head: () => x[0]
});
List.of = arr => (arr instanceof Array ? List(arr) : Identity(arr));
return List;
}
Insert cell
myNumbers = List([1, 2, 3, 4, 5])
Insert cell
Insert cell
{
const b = myNumbers.concat([6, 7, 8, 9, 10]);
return b.inspect();
}
Insert cell
Insert cell
myNumbers.head()
Insert cell
Insert cell
myNumbers.head().inspect()
Insert cell
Insert cell
Maybe = {
const Just = x => ({
emit: () => x,
chain: f => f(x),
map: f => MaybeOf(f(x)),
fork: (_, g) => g(x),
isJust: true,
isNothing: false,
inspect: () => `Just(${x})`
});
const Nothing = x => ({
chain: _ => Nothing(),
emit: () => Nothing(),
map: _ => Nothing(),
fork: (f, _) => f(),
isJust: false,
isNothing: true,
inspect: () => 'Nothing'
});
const MaybeOf = x =>
x === null || x === undefined || x.isNothing ? Nothing() : Just(x);
return {of: MaybeOf}
}
Insert cell
Insert cell
Insert cell
convertReading = x => Maybe.of(x).map(fahrenheitToCelsius)
Insert cell
Insert cell
Insert cell
convertReading(reading1).inspect()
Insert cell
convertReading(reading2).inspect()
Insert cell
Insert cell
Insert cell
Maybe.of(reading1)
.map(fahrenheitToCelsius)
.fork(
_ => display('ERR!'),
t => display(`${t}°C`)
)
Insert cell
Maybe.of(reading2)
.map(fahrenheitToCelsius)
.fork(
_ => display('ERR!'),
t => display(`${t}°C`)
)
Insert cell
temp3C = Maybe.of(reading1)
.map(fahrenheitToCelsius)
.fork(
_ => display('ERR!'),
t => display(`${t}°C`)
)
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more