This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sums, constructing | |
const tag = t => (...args) => ({ tag: t, values: [...args] }); | |
// 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](...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