Marius Jurgelenas
FrontCon 2019
ADTs - composed types
Algebraic Data Types
Product types
Sum types
langauge-agnostic
Fruit Salad - a Product type
type Fruit Salad = Apple AND Banana AND Cherry
How many different fruit salads can we make?
Fruit Salad - a Product type
3 × 2 × 2 = 12
Product
Fruit Salad - a Product type
{
name: 'John', // string
age: 42, // number
ownsDog: true // boolean
}
Person - a Product type
type Fruit Snack = Apple OR Banana OR Cherry
Fruit Snack - a Sum type
How many different snacks can we have?
Fruit Snack - a Sum type
3 + 2 + 2 = 7
Sum
Fruit Snack - a Sum type
Are Sum types ok?
type snack = | |
type strangeThing = number | boolean
Uncought TypeError: Cannot read property 'toUpperCase' of undefined
name.toUpperCase()
Sum types are ok
if (name) {
return name.toUpperCase()
} else {
return "No name!"
}
Matching Types
Why is it called Algebraic?
Algebraic Data Types
Product types
Sum types
Look around - Algebra everywhere!
type OS = Linux | Windows | MacOS
type WorkPlace = Computer & Desk & Chair
type Computer = Hardware & OS