Instantly share code, notes, and snippets.

Embed
What would you like to do?
// sums, constructing
const tag = t => {
function halp() {
return { tag: t, values: [... arguments] }
}
return halp;
};
// testing
tag("label")(1, "horse", [2,3]);
// fruit
const Peach = tag("Peach")();
const Apple = tag("Apple")();
const Pear = tag("Pear")();
const Lemon = tag("Lemon")();
const Fig = tag("Fig")();
// tree
const Bud = tag("Bud")();
const Flat = tag("Flat");
const Split = tag("Split");
const smol_tree = Split(Bud, Flat(Peach, Bud));
const larger_tree = Split(Flat(Apple, Flat(Lemon, Bud)), Flat(Peach, Bud));
// sums, destructing
const match = cases => x => cases[x.tag].apply(null, x.values);
const is_apple =
match(
{
Peach: () => false,
Apple: () => true,
Pear: () => false,
Lemon: () => false,
Fig: () => false
}
);
// testing
is_apple(Apple);
is_apple(Fig);
const height =
match(
{
Bud: () => 0,
Flat: (_, t) => 1 + height(t),
Split: (a, b) => 1 + Math.max(height(a), height(b))
}
);
// testing
height(smol_tree);
height(larger_tree);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment