Skip to content

Commit 38c7cd5

Browse files
committed
Edits
1 parent 8e019f2 commit 38c7cd5

File tree

6 files changed

+134
-411
lines changed

6 files changed

+134
-411
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ Deeper understanding of Haskell performance, and how to improve it.
141141
## Worked examples
142142

143143
* [Length-matched vectors](examples/length-matched-vectors.md)
144-
* [Validation Applicative](examples/validation-applicative.md)
145144
* [Rate limiting using STM](https://github.com/snoyberg/rate-limit)
146145
* [RWST in terms of IORef](https://gist.github.com/snoyberg/7ac111bc873be6a361e452adb5454cb9)
147146

async.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ caught by the `try` and execution continues indefinitely. This is
563563
_very bad code_, don't do this!
564564

565565
__Exercise__ Switch the import from `Control.Exception` to
566-
`Control.Exception.Safe`. What happens? Why?
566+
`UnliftIO.Exception`. What happens? Why?
567567

568568
### Linking
569569

@@ -728,13 +728,6 @@ Using `UnliftIO.Async` as a drop in replacement for
728728
be aware of is that the `Async` data type will take an extra type
729729
paramter for the underlying monad.
730730

731-
FIXME: copy material from
732-
* [async](https://haskell-lang.org/library/async)
733-
734-
* __Exercise__: Write a concurrent program that prints the numbers 1 to 100 in
735-
one thread, pausing one second between number, and printing "Hello World"
736-
seconds in another thread
737-
738731
## Section exercise
739732

740733
Write a helper function which allows you to pass actions to worker

examples/length-matched-vectors.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,61 @@
11
# Length matched vectors
22

3-
FIXME
3+
```haskell
4+
{-# LANGUAGE NoImplicitPrelude #-}
5+
{-# LANGUAGE RankNTypes #-}
6+
{-# LANGUAGE ScopedTypeVariables #-}
7+
{-# LANGUAGE FlexibleContexts #-}
8+
module VectorLenMatched
9+
( VectorLM
10+
, LMKey
11+
, lmKeys
12+
, unVectorLM
13+
, lookupVectorLM
14+
, withVectorLM
15+
, newVectorLM
16+
, unknownKey
17+
) where
18+
19+
import RIO
20+
import qualified RIO.Vector as V
21+
import RIO.Vector.Unsafe (unsafeIndex)
22+
import Data.Proxy
23+
import Data.Reflection
24+
import Data.Void
25+
26+
newtype LMKey k = LMKey Int
27+
28+
newtype VectorLM k a = VectorLM (Vector a)
29+
30+
unknownKey :: Vector a -> VectorLM () a
31+
unknownKey = VectorLM
32+
33+
unVectorLM :: VectorLM k a -> Vector a
34+
unVectorLM (VectorLM v) = v
35+
36+
lookupVectorLM :: LMKey k -> VectorLM k a -> a
37+
lookupVectorLM (LMKey idx) (VectorLM v) = unsafeIndex v idx
38+
39+
lmKeys :: forall k. Reifies k Int => [LMKey k]
40+
lmKeys =
41+
case reflect (Proxy :: Proxy k) of
42+
0 -> []
43+
i -> map LMKey [0..i - 1]
44+
45+
withVectorLM
46+
:: forall a r.
47+
Vector a
48+
-> (forall k. Reifies k Int => VectorLM k a -> r)
49+
-> r
50+
withVectorLM v inner = reify (V.length v) $ \p -> inner $ addType p v
51+
52+
addType :: proxy k -> Vector a -> VectorLM k a
53+
addType _ = VectorLM
54+
55+
newVectorLM :: forall k a. Reifies k Int => Vector a -> Maybe (VectorLM k a)
56+
newVectorLM v
57+
| V.length v == len = Just (VectorLM v)
58+
| otherwise = Nothing
59+
where
60+
len = reflect (Proxy :: Proxy k)
61+
```

examples/validation-applicative.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)