Skip to content

Commit

Permalink
Small updates
Browse files Browse the repository at this point in the history
  • Loading branch information
smelc committed Dec 11, 2023
1 parent 596bc32 commit c4c7748
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 49 deletions.
25 changes: 15 additions & 10 deletions slides/Course02.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ instance Collection Maybe where
size = \case Nothing -> 0; Just _ -> 1
toList = \case Nothing -> []; Just x -> [x]

instance Mappable [] where
map f = \case [] -> []; (x : xs) -> f x : Course02.map f xs
instance Mappable Maybe where
map _f = undefined

data Either a b =
-- | The left case, commonly used as the error case
Expand All @@ -76,14 +76,6 @@ readEither :: Read a => String -> Either String a

readEither _ = undefined

instance Collection (Either a) where
size _ = 1
isEmpty _ = False
toList = \case Left _ -> []; Right b -> [b]

instance Mappable (Either a) where
map f = \case Left a -> Left a; Right b -> Right (f b)

data Account = Account {
balance :: Int,
email :: String,
Expand All @@ -105,6 +97,19 @@ mkAccount2 email = Account { .. } -- Take fields from enclosing scope
setBalance :: Int -> Account -> Account
setBalance n account = account { balance = n } -- functional update

data Interval a = Interval {
start :: a,
end :: a
}

instance Collection Interval where
size _ = undefined
isEmpty _ = undefined
toList _ = undefined

instance Mappable Interval where
map _f _ = undefined

class Semigroup a where
-- | Associative binary operation
(<>) :: a -> a -> a
Expand Down
6 changes: 3 additions & 3 deletions slides/Course03.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ data Account = Account {
dubious :: [Account] -> [String]
dubious accounts =
accounts
& filter (\Account{balance} -> balance > 1000000)
& filter (\Account{email} -> "ponzi" `isInfixOf` email)
& filter (\Account{name} -> case name of Nothing -> True; Just _ -> False)
& filter (\account -> account.balance > 1000000)
& filter (\account -> "ponzi" `isInfixOf` account.email)
& filter (\account -> case account.name of Nothing -> True; Just _ -> False)
& map email


Expand Down
2 changes: 1 addition & 1 deletion slides/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Continously build the *.hs files extract from
# snippets of the *.md files

ls *.md *.cabal | grep course | entr "./build_hs.sh" &
ls *.md | grep course | entr "./extract_hs.sh && cabal build" &

declare -r HS_CHECKER_PID="$!"

Expand Down
52 changes: 21 additions & 31 deletions slides/course-02.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ instance Collection Maybe where
size = \case Nothing -> 0; Just _ -> 1
toList = \case Nothing -> []; Just x -> [x]

instance Mappable [] where
map f = \case [] -> []; (x : xs) -> f x : Course02.map f xs
instance Mappable Maybe where
map _f = undefined
```

---
Expand Down Expand Up @@ -437,35 +437,6 @@ Right 0
Left "Prelude.read: no parse" # Not the best error message
```

--

**Cette fin de slide naze, parceque la syntaxe `Collection (Either a)` apparaît trop bizarre**

* Abstracting over `Either`

<!-- exdown-skip -->
```hs
instance Collection (Either a) where
size _ = undefined
isEmpty _ = undefined
toList _ = undefined

instance Mappable (Either a) where
map f _ = undefined
```

???

```hs
instance Collection (Either a) where
size _ = 1
isEmpty _ = False
toList = \case Left _ -> []; Right b -> [b]

instance Mappable (Either a) where
map f = \case Left a -> Left a; Right b -> Right (f b)
```

---

# Types: records
Expand Down Expand Up @@ -509,6 +480,25 @@ setBalance n account = account { balance = n } -- functional update

---

# Abstracting over a record: `Interval`

```hs
data Interval a = Interval {
start :: a,
end :: a
}

instance Collection Interval where
size _ = undefined
isEmpty _ = undefined
toList _ = undefined

instance Mappable Interval where
map _f _ = undefined
```

---

# Common abstractions

```hs
Expand Down
6 changes: 3 additions & 3 deletions slides/course-03.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ data Account = Account {
dubious :: [Account] -> [String]
dubious accounts =
accounts
& filter (\Account{balance} -> balance > 1000000)
& filter (\Account{email} -> "ponzi" `isInfixOf` email)
& filter (\Account{name} -> case name of Nothing -> True; Just _ -> False)
& filter (\account -> account.balance > 1000000)
& filter (\account -> "ponzi" `isInfixOf` account.email)
& filter (\account -> case account.name of Nothing -> True; Just _ -> False)
& map email
```

Expand Down
3 changes: 2 additions & 1 deletion slides/course-04.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ class Functor f where

# Be lazy, rely on the compiler

From course 2:

```hs
data Interval a = Interval {
start :: a,
Expand All @@ -373,7 +375,6 @@ data Interval a = Interval {
deriving Functor
```

- What can this type represent?
- Write `Functor Interval`

---
Expand Down

0 comments on commit c4c7748

Please sign in to comment.