Practical introduction to algebraic datatypes (ADTs) in TypeScript
1–10 of 23 posts
Re: Practical introduction to algebraic datatypes (ADTs) in TypeScript
#2Re: Practical introduction to algebraic datatypes (ADTs) in TypeScript
#3 enum Bool {
True,
False
}
enum Status {
Waiting,
Successful,
Failed
}
We can combine them into a product type, like a tuple, or a sum type, like a union. type Product = (Bool, Status)
type Sum = Bool | Status
Now, we ask ourselves, what are the valid values of type Product? (True, Waiting)
(True, Successful)
(True, Failed)
(False, Waiting)
(False, Successful)
(False, Failed)
There are two Bool values, and three Status values, so there are 2×3=6 values for the product of Bool and Status. Now, what about the Sum type? True
False
Waiting
Successful
Failed
We have 2+3=5 total values for the sum of Bool and Product.Now, obviously, this math doesn't quite work out for types with infinite values, like strings or bigints or arrays, but that's where the analogy comes from.
If you extend this further, you can even figure out what an exponential type would be: the exponential type Bool^Status is a function that takes a Status as its argument and returns a Bool.
Re: Practical introduction to algebraic datatypes (ADTs) in TypeScript
#4Definitely take a look at fp-ts [1]! It's mentioned at the end of the article, but I want to emphasize just how cool it is. [1] https://github.com/gcanti/fp-ts
Re: Practical introduction to algebraic datatypes (ADTs) in TypeScript
#5Definitely take a look at fp-ts [1]! It's mentioned at the end of the article, but I want to emphasize just how cool it is. [1] https://github.com/gcanti/fp-ts
Re: Practical introduction to algebraic datatypes (ADTs) in TypeScript
#6I think this article makes the "sum" and "product" terms out to be very complicated, when in fact it's quite simple. If we have two enums enum Bool { True, False } enum Status { Waiting, Successful, Failed } We can combine them into a product type, like a tuple, or a sum type, like a union. type Product = (Bool, Status) type Sum = Bool | Status Now, we ask ourselves, what are the valid values of type Product? (True,…
In the switch statement it becomes a Sum value (as shown in the article).
In ReScript (shown as well, which uses OCaml under the hood) the syntax is that of a Variant, in which each element is a different nominal type.
Re: Practical introduction to algebraic datatypes (ADTs) in TypeScript
#7Definitely take a look at fp-ts [1]! It's mentioned at the end of the article, but I want to emphasize just how cool it is. [1] https://github.com/gcanti/fp-ts
I know there are a bunch of small guides / articles written by the author of fp-ts, but is there a good book, guide, etc. for diving in for someone who is kind of familiar with functional concepts but not quite ready for going full haskell? A book on haskell?
That said, I wrote this[1] a year and a half ago for some colleagues just as I was getting into it myself. I hope it helps, and if not let me know if there's anything more specific I could help with.
Re: Practical introduction to algebraic datatypes (ADTs) in TypeScript
#8I think this article makes the "sum" and "product" terms out to be very complicated, when in fact it's quite simple. If we have two enums enum Bool { True, False } enum Status { Waiting, Successful, Failed } We can combine them into a product type, like a tuple, or a sum type, like a union. type Product = (Bool, Status) type Sum = Bool | Status Now, we ask ourselves, what are the valid values of type Product? (True,…
The Sum is called Union in the TypeScript docs, as it narrows the valid values to the overlapping elements. In the switch statement it becomes a Sum value (as shown in the article). In ReScript (shown as well, which uses OCaml under the hood) the syntax is that of a Variant, in which each element is a different nominal type.
type NotAProduct = Bool | Bool
only has 2 values, not 4. For a real product type, we need type Product = {tag: "Left", value: Bool} | {tag: "Right", value: Bool}
which has the requisite 4 values.Re: Practical introduction to algebraic datatypes (ADTs) in TypeScript
#9Earlier quoted context omitted.
I know there are a bunch of small guides / articles written by the author of fp-ts, but is there a good book, guide, etc. for diving in for someone who is kind of familiar with functional concepts but not quite ready for going full haskell? A book on haskell?
I've just gone through this process over the last couple of years. To be honest, the most effective learning tool for me was dedicating my free time for a little while to learning Haskell. It's a higher upfront cost but a huge pay-off, not just in terms of understanding fp-ts but more broadly in how you'll be able to view programming through a new lens. That said, I wrote this[1] a year and a half ago for some collea…