by mightybyte · 3y ago · 0 comments
An example in the wild that demonstrates this `fp-ts`'s explanation of how to do ADTs in TypeScript where you can see the comparison in PureScript is a one-liner (both data and polymorphic data): https://github.com/gcanti/fp-ts/blob/master/docs/guides/pure...
class Bar { tag="bar" as const; constructor(public value:string){} } class Baz { tag="baz" as const; constructor(public value:boolean){} } type Foo = Bar|Baz;
Also while pattern matching isn't an explicit thing in TS you get most benefits from compiler checked accesses that can recognize type-tests in your code and derive types and do property access checks.
Swift enums are really nice[0].
[0] https://littlegreenviper.com/miscellany/swiftwater/enums-wit...
In algebraic data types we have a sum (union) of a labeled cartesian products, and each product may have arbitrary number of values:
data MaybeInt = NoInt | AnInt Int
data MaybeItsPair = NoIntsPair | AnIntPair Int Int
The first constructor in both data types is a labeled (No...) product with empty number of value to apply cartesian product to.
The second constructor in both data types is a cartesian product: in first case an Int and in case a pair of Ints.
Of course you can have more involved data type:
data Expr = Void | Const Int | Var String | Bin BinOp Expr Expr | Un UnOp Expr
A label, two single value constructors and much more involved operations as well.
I haven't studied much theoretical computer science so I'd love to hear some good beginner resources on this stuff.
Here’s an interesting little exploration: https://littlegreenviper.com/miscellany/swiftwater/writing-a...