Skip to content

Commit 286b6db

Browse files
committed
Convert Maybe to Option to match fp-ts
1 parent ee5d053 commit 286b6db

16 files changed

+1059
-881
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Click on the words for information about subjects and things.
66

77
[Intro](https://github.com/danieljharvey/functional-programming-is-boring/blob/master/intro.md)
88

9-
### Lesson 1 - Maybe
9+
### Lesson 1 - Option
1010

11-
[Information](https://github.com/danieljharvey/functional-programming-is-boring/blob/master/slides/lesson1-maybe.md)
11+
[Information](https://github.com/danieljharvey/functional-programming-is-boring/blob/master/slides/lesson1-option.md)
1212

13-
[Exercises](https://github.com/danieljharvey/functional-programming-is-boring/blob/master/src/lesson1-maybe.ts)
13+
[Exercises](https://github.com/danieljharvey/functional-programming-is-boring/blob/master/src/lesson1-option.ts)
1414

1515
### Lesson 2 - Either
1616

slides/complexity.md

+49-42
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Cardinality
1+
# Cardinality
22

33
## Measuring complexity
44

55
- Reducing complexity is good, and so it's helpful to have a way of measuring
6-
it.
6+
it.
77

88
- Look at this simple type:
99

@@ -19,39 +19,41 @@ type ManyOptions = {
1919
- What about now?
2020
2121
```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+
}
2830
```
2931
3032
- A small change reduces the number of options, and thus the number of code
3133
paths needed to deal with it.
3234
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.
3436
3537
```typescript
3638
type ContrivedTariff = {
37-
type: 'electricity' | 'gas',
39+
type: 'electricity' | 'gas'
3840
eco7?: boolean
3941
elecReading?: number
4042
gasReading?: number
41-
}
43+
}
4244
```
4345
44-
## How do we measure it?
46+
## How do we measure it?
4547
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_.
4749
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:
4951
5052
`the number of elements in a set or other grouping, as a property of that grouping.`
5153
5254
- `Boolean` can be `true` or `false`
5355
54-
- so...?
56+
- so...?
5557
5658
- Yes, indeed, `2`.
5759
@@ -61,29 +63,29 @@ This measure of how many possible values exist in a type is called *cardinality*
6163
6264
- I mean `3`. Lolle.
6365
64-
- The cardinality of `string` or `number` is very large indeed
66+
- The cardinality of `string` or `number` is very large indeed
6567
66-
- How does this relate to our new friends `Either` and `Maybe`?
68+
- How does this relate to our new friends `Either` and `Option`?
6769
6870
## Algebraic Data Types
6971
70-
- So `Maybe` and `Either` are both examples of `Algebraic Data Types`
72+
- So `Option` and `Either` are both examples of `Algebraic Data Types`
7173
7274
- More accurately, `sum types`
7375
7476
- (The other kind are `product types`, we'll come to those...)
7577
76-
## Maybe
78+
## Option
7779
78-
- `Maybe A` is a `sum type` because it's *cardinality* is
80+
- `Option A` is a `sum type` because it's _cardinality_ is
7981
8082
- the sum of `whatever the cardinality of A is` and `1`
8183
8284
- So `A` + `1`, kinda.
8385
8486
- (The `1` is to represent `Nothing`)
8587
86-
- The type `Maybe<boolean>` could have values of either
88+
- The type `Option<boolean>` could have values of either
8789
8890
- `Just(true)`
8991
@@ -110,14 +112,15 @@ This measure of how many possible values exist in a type is called *cardinality*
110112
But how does it relate to complexity?
111113
112114
- Bare with me - I swear we're getting to a breakthrough
113-
114-
## Product types
115+
116+
## Product types
115117
116118
- `Product types` are the other kind of `Algebraic Data Type` (`ADT`)
117119
118120
- They are way more boring tbh.
119121
120122
- Most Typescript interfaces are `Product types`
123+
121124
```typescript
122125
interface Person {
123126
name: string
@@ -131,17 +134,17 @@ interface Person {
131134
- (And why wouldn't we? They are broadly supported and it's considered idiomatic
132135
to do so.)
133136

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)
135138

136139
- (yet)
137140

138141
## Anyway
139142

140-
- Whilst `sum types` describe `this` *OR* `that`.
143+
- Whilst `sum types` describe `this` _OR_ `that`.
141144

142-
- `Product types` describe `this` *AND* `that`.
145+
- `Product types` describe `this` _AND_ `that`.
143146

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`
145148

146149
## Tuple
147150

@@ -150,27 +153,28 @@ interface Person {
150153
- A very simple simple product type is `Tuple A B`
151154

152155
- We could represent it like this:
156+
153157
```typescript
154-
type Tuple<A,B> = { type: "Tuple", a: A, b: B }
158+
type Tuple<A, B> = { type: 'Tuple'; a: A; b: B }
155159
```
156160
157-
- It is the *dual* of `Either`...
161+
- It is the _dual_ of `Either`...
158162
159-
- `Either<A,B>` is `A` *+* `B`.
163+
- `Either<A,B>` is `A` _+_ `B`.
160164
161-
- `Tuple<A,B>` is `A` *x* `B`.
165+
- `Tuple<A,B>` is `A` _x_ `B`.
162166
163167
- 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`.
165169
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_
167171
`age`.
168172
169173
- (I'm not sure why you would do this, admittedly)
170174
171-
- So, knowing that the *cardinality* of `Either<TrafficLights, boolean>` is `5`
175+
- So, knowing that the _cardinality_ of `Either<TrafficLights, boolean>` is `5`
172176
173-
- What is *cardinality* of `Tuple<TrafficLights, Boolean>`...?
177+
- What is _cardinality_ of `Tuple<TrafficLights, Boolean>`...?
174178
175179
- .
176180
@@ -194,18 +198,22 @@ type Tuple<A,B> = { type: "Tuple", a: A, b: B }
194198
195199
- So wherever you find yourself adding more rows to a type, think
196200
197-
- Will this __REALLY__ always be there?
201+
- Will this **REALLY** always be there?
198202
199203
- Or can I make it a `sum` instead?
200204
201-
## Extra task time
205+
## Extra task time
206+
207+
- Given a constructor for making `Tuple` types:
202208
203-
- Given a constructor for making `Tuple` types:
204209
```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+
})
207215

208-
tuple("Horse", 100)
216+
tuple('Horse', 100)
209217
// { type: "Tuple", a: "Horse", b: 100 })
210218
```
211219

@@ -220,4 +228,3 @@ tuple("Horse", 100)
220228
- `match :: (A -> B -> C) -> Tuple A B -> C`
221229

222230
- Why is implementing `join` or `bind` difficult?
223-

0 commit comments

Comments
 (0)