1
- # Cardinality
1
+ # Cardinality
2
2
3
3
## Measuring complexity
4
4
5
5
- Reducing complexity is good, and so it's helpful to have a way of measuring
6
- it.
6
+ it.
7
7
8
8
- Look at this simple type:
9
9
@@ -19,39 +19,41 @@ type ManyOptions = {
19
19
- What about now?
20
20
21
21
` ` ` typescript
22
- type SlightlyLessOptions = {
23
- type: ' electricity' ,
24
- eco7: boolean
25
- } | {
26
- type: ' gas'
27
- }
22
+ type SlightlyLessOptions =
23
+ | {
24
+ type: ' electricity'
25
+ eco7: boolean
26
+ }
27
+ | {
28
+ type: ' gas'
29
+ }
28
30
` ` `
29
31
30
32
- A small change reduces the number of options, and thus the number of code
31
33
paths needed to deal with it.
32
34
33
- - Although it's trivial now, as a data type grows it becomes more significant.
35
+ - Although it's trivial now, as a data type grows it becomes more significant.
34
36
35
37
` ` ` typescript
36
38
type ContrivedTariff = {
37
- type: ' electricity' | ' gas' ,
39
+ type: ' electricity' | ' gas'
38
40
eco7? : boolean
39
41
elecReading? : number
40
42
gasReading? : number
41
- }
43
+ }
42
44
` ` `
43
45
44
- ## How do we measure it?
46
+ ## How do we measure it?
45
47
46
- This measure of how many possible values exist in a type is called *cardinality* .
48
+ This measure of how many possible values exist in a type is called _cardinality_ .
47
49
48
- - It is defined by the first result in a Google search I just did as:
50
+ - It is defined by the first result in a Google search I just did as:
49
51
50
52
` the number of elements in a set or other grouping , as a property of that grouping .`
51
53
52
54
- ` Boolean ` can be ` true ` or ` false `
53
55
54
- - so...?
56
+ - so...?
55
57
56
58
- Yes, indeed, ` 2 ` .
57
59
@@ -61,29 +63,29 @@ This measure of how many possible values exist in a type is called *cardinality*
61
63
62
64
- I mean ` 3 ` . Lolle.
63
65
64
- - The cardinality of ` string ` or ` number ` is very large indeed
66
+ - The cardinality of ` string ` or ` number ` is very large indeed
65
67
66
- - How does this relate to our new friends ` Either ` and ` Maybe ` ?
68
+ - How does this relate to our new friends ` Either ` and ` Option ` ?
67
69
68
70
## Algebraic Data Types
69
71
70
- - So ` Maybe ` and ` Either ` are both examples of ` Algebraic Data Types `
72
+ - So ` Option ` and ` Either ` are both examples of ` Algebraic Data Types `
71
73
72
74
- More accurately, ` sum types `
73
75
74
76
- (The other kind are ` product types ` , we'll come to those...)
75
77
76
- ## Maybe
78
+ ## Option
77
79
78
- - ` Maybe A ` is a ` sum type ` because it's *cardinality* is
80
+ - ` Option A ` is a ` sum type ` because it's _cardinality_ is
79
81
80
82
- the sum of ` whatever the cardinality of A is ` and ` 1 `
81
83
82
84
- So ` A ` + ` 1 ` , kinda.
83
85
84
86
- (The ` 1 ` is to represent ` Nothing ` )
85
87
86
- - The type ` Maybe <boolean >` could have values of either
88
+ - The type ` Option <boolean >` could have values of either
87
89
88
90
- ` Just (true )`
89
91
@@ -110,14 +112,15 @@ This measure of how many possible values exist in a type is called *cardinality*
110
112
But how does it relate to complexity?
111
113
112
114
- Bare with me - I swear we're getting to a breakthrough
113
-
114
- ## Product types
115
+
116
+ ## Product types
115
117
116
118
- ` Product types ` are the other kind of ` Algebraic Data Type ` ( ` ADT ` )
117
119
118
120
- They are way more boring tbh.
119
121
120
122
- Most Typescript interfaces are ` Product types `
123
+
121
124
` ` ` typescript
122
125
interface Person {
123
126
name: string
@@ -131,17 +134,17 @@ interface Person {
131
134
- (And why wouldn't we? They are broadly supported and it's considered idiomatic
132
135
to do so.)
133
136
134
- - (And I'm not writing a thinkpiece named __ Javascript Objects Considered Harmful __ or anything)
137
+ - (And I'm not writing a thinkpiece named ** Javascript Objects Considered Harmful ** or anything)
135
138
136
139
- (yet)
137
140
138
141
## Anyway
139
142
140
- - Whilst ` sum types ` describe ` this ` * OR * ` that ` .
143
+ - Whilst ` sum types ` describe ` this ` _ OR _ ` that ` .
141
144
142
- - ` Product types ` describe ` this ` * AND * ` that ` .
145
+ - ` Product types ` describe ` this ` _ AND _ ` that ` .
143
146
144
- - So, a ` Person ` interface is just a nice way of carrying around a ` string ` * AND * a ` number `
147
+ - So, a ` Person ` interface is just a nice way of carrying around a ` string ` _ AND _ a ` number `
145
148
146
149
## Tuple
147
150
@@ -150,27 +153,28 @@ interface Person {
150
153
- A very simple simple product type is ` Tuple A B `
151
154
152
155
- We could represent it like this:
156
+
153
157
``` typescript
154
- type Tuple <A ,B > = { type: " Tuple" , a: A , b: B }
158
+ type Tuple <A , B > = { type: ' Tuple' ; a: A ; b: B }
155
159
` ` `
156
160
157
- - It is the *dual* of ` Either ` ...
161
+ - It is the _dual_ of ` Either ` ...
158
162
159
- - ` Either <A ,B >` is ` A ` *+* ` B ` .
163
+ - ` Either <A ,B >` is ` A ` _+_ ` B ` .
160
164
161
- - ` Tuple <A ,B >` is ` A ` *x* ` B ` .
165
+ - ` Tuple <A ,B >` is ` A ` _x_ ` B ` .
162
166
163
167
- A ` Tuple <string ,number >` could represent ` Person ` interface from before as
164
- it's their ` name ` *AND* ` age ` .
168
+ it's their ` name ` _AND_ ` age ` .
165
169
166
- - Whilst ` Either <string ,number >` could be used to describe a person's ` name ` *OR*
170
+ - Whilst ` Either <string ,number >` could be used to describe a person's ` name ` _OR_
167
171
` age ` .
168
172
169
173
- (I'm not sure why you would do this, admittedly)
170
174
171
- - So, knowing that the *cardinality* of ` Either <TrafficLights , boolean >` is ` 5 `
175
+ - So, knowing that the _cardinality_ of ` Either <TrafficLights , boolean >` is ` 5 `
172
176
173
- - What is *cardinality* of ` Tuple <TrafficLights , Boolean >` ...?
177
+ - What is _cardinality_ of ` Tuple <TrafficLights , Boolean >` ...?
174
178
175
179
- .
176
180
@@ -194,18 +198,22 @@ type Tuple<A,B> = { type: "Tuple", a: A, b: B }
194
198
195
199
- So wherever you find yourself adding more rows to a type, think
196
200
197
- - Will this __REALLY__ always be there?
201
+ - Will this **REALLY** always be there?
198
202
199
203
- Or can I make it a ` sum ` instead?
200
204
201
- ## Extra task time
205
+ ## Extra task time
206
+
207
+ - Given a constructor for making ` Tuple ` types:
202
208
203
- - Given a constructor for making ` Tuple ` types:
204
209
` ` ` typescript
205
- const tuple = <A ,B >(a : A , b : B ): Tuple <A ,B > =>
206
- ({ type: " Tuple" , a , b })
210
+ const tuple = <A , B >(a : A , b : B ): Tuple <A , B > => ({
211
+ type: ' Tuple' ,
212
+ a ,
213
+ b ,
214
+ })
207
215
208
- tuple (" Horse" , 100 )
216
+ tuple (' Horse' , 100 )
209
217
// { type: "Tuple", a: "Horse", b: 100 })
210
218
```
211
219
@@ -220,4 +228,3 @@ tuple("Horse", 100)
220
228
- ` match :: (A -> B -> C) -> Tuple A B -> C `
221
229
222
230
- Why is implementing ` join ` or ` bind ` difficult?
223
-
0 commit comments