Skip to content

Commit c8bb7d3

Browse files
ntwilsonmilesfrain
andauthored
add anchors for chapter 6 (#378)
* add anchors for chapter 6 * Apply suggestions from code review Co-authored-by: milesfrain <[email protected]> Co-authored-by: milesfrain <[email protected]>
1 parent 5e0da3e commit c8bb7d3

File tree

3 files changed

+141
-127
lines changed

3 files changed

+141
-127
lines changed

exercises/chapter6/src/Data/Hashable.purs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,57 @@ import Data.Maybe (Maybe(..))
1717
import Data.String.CodeUnits (toCharArray)
1818
import Data.Tuple (Tuple(..))
1919

20+
-- ANCHOR: Hashable
2021
newtype HashCode = HashCode Int
2122

23+
instance hashCodeEq :: Eq HashCode where
24+
eq (HashCode a) (HashCode b) = a == b
25+
2226
hashCode :: Int -> HashCode
2327
hashCode h = HashCode (h `mod` 65535)
2428

2529
class Eq a <= Hashable a where
2630
hash :: a -> HashCode
31+
-- ANCHOR_END: Hashable
2732

2833
instance showHashCode :: Show HashCode where
2934
show (HashCode h) = "(HashCode " <> show h <> ")"
3035

31-
derive instance eqHashCode :: Eq HashCode
32-
36+
-- ANCHOR: combineHashes
3337
combineHashes :: HashCode -> HashCode -> HashCode
3438
combineHashes (HashCode h1) (HashCode h2) = hashCode (73 * h1 + 51 * h2)
39+
-- ANCHOR_END: combineHashes
3540

41+
-- ANCHOR: hashEqual
3642
hashEqual :: forall a. Hashable a => a -> a -> Boolean
3743
hashEqual = eq `on` hash
44+
-- ANCHOR_END: hashEqual
3845

46+
-- ANCHOR: hashChar
3947
instance hashChar :: Hashable Char where
4048
hash = hash <<< toCharCode
49+
-- ANCHOR_END: hashChar
4150

51+
-- ANCHOR: hashString
4252
instance hashString :: Hashable String where
4353
hash = hash <<< toCharArray
54+
-- ANCHOR_END: hashString
4455

56+
-- ANCHOR: hashInt
4557
instance hashInt :: Hashable Int where
4658
hash = hashCode
59+
-- ANCHOR_END: hashInt
4760

61+
-- ANCHOR: hashBoolean
4862
instance hashBoolean :: Hashable Boolean where
4963
hash false = hashCode 0
5064
hash true = hashCode 1
65+
-- ANCHOR_END: hashBoolean
5166

67+
-- ANCHOR: hashArray
5268
instance hashArray :: Hashable a => Hashable (Array a) where
5369
hash = foldl combineHashes (hashCode 0) <<< map hash
70+
-- ANCHOR_END: hashArray
5471

5572
instance hashMaybe :: Hashable a => Hashable (Maybe a) where
5673
hash Nothing = hashCode 0

exercises/chapter6/test/no-peeking/Solutions.purs

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,25 @@ import Data.Maybe (Maybe(..))
1111
import Data.Monoid (power)
1212
import Data.Newtype (class Newtype, over2, wrap)
1313

14+
-- ANCHOR: Point
1415
newtype Point
15-
= Point
16-
{ x :: Number
17-
, y :: Number
18-
}
16+
= Point
17+
{ x :: Number
18+
, y :: Number
19+
}
20+
-- ANCHOR_END: Point
1921

2022
instance showPoint :: Show Point where
2123
show (Point p) =
2224
"(" <> show p.x <> ", " <> show p.y <> ")"
2325

26+
-- ANCHOR: Complex
2427
newtype Complex
2528
= Complex
2629
{ real :: Number
2730
, imaginary :: Number
2831
}
32+
-- ANCHOR_END: Complex
2933

3034
instance showComplex :: Show Complex where
3135
show (Complex c) =
@@ -83,11 +87,13 @@ instance ringComplex :: Ring Complex where
8387
sub (Complex a) (Complex b) = Complex $ a - b
8488
-}
8589

90+
-- ANCHOR: Shape
8691
data Shape
87-
= Circle Point Number
88-
| Rectangle Point Number Number
89-
| Line Point Point
90-
| Text Point String
92+
= Circle Point Number
93+
| Rectangle Point Number Number
94+
| Line Point Point
95+
| Text Point String
96+
-- ANCHOR_END: Shape
9197

9298
derive instance genericShape :: Generic Shape _
9399

@@ -102,8 +108,9 @@ instance showShape :: Show Shape where
102108
show (Text p s) = "(Text " <> show p <> " " <> show s <> ")"
103109
-}
104110

105-
data NonEmpty a
106-
= NonEmpty a (Array a)
111+
-- ANCHOR: NonEmpty
112+
data NonEmpty a = NonEmpty a (Array a)
113+
-- ANCHOR_END: NonEmpty
107114

108115
instance eqNonEmpty :: Eq a => Eq (NonEmpty a) where
109116
eq (NonEmpty e1 a1) (NonEmpty e2 a2) = e1 == e2 && a1 == a2
@@ -125,9 +132,9 @@ instance functorNonEmpty :: Functor NonEmpty where
125132
map func (NonEmpty e1 a1) = NonEmpty (func e1) (map func a1)
126133
-}
127134

128-
data Extended a
129-
= Infinite
130-
| Finite a
135+
-- ANCHOR: Extended
136+
data Extended a = Infinite | Finite a
137+
-- ANCHOR_END: Extended
131138

132139
derive instance eqExtended :: Eq a => Eq (Extended a)
133140
{-
@@ -156,10 +163,13 @@ instance foldableNonEmpty :: Foldable NonEmpty where
156163
foldl func st (NonEmpty val arr) = foldl func st ([ val ] <> arr)
157164
foldMap func (NonEmpty val arr) = foldMap func ([ val ] <> arr)
158165

159-
data OneMore f a
160-
= OneMore a (f a)
166+
-- ANCHOR: OneMore
167+
data OneMore f a = OneMore a (f a)
168+
-- ANCHOR_END: OneMore
161169

170+
-- ANCHOR: OneMore_Foldable
162171
instance foldableOneMore :: Foldable f => Foldable (OneMore f) where
172+
-- ANCHOR_END: OneMore_Foldable
163173
foldr func st (OneMore val more) = func val lastB
164174
where
165175
lastB = foldr func st more
@@ -184,20 +194,28 @@ unsafeMaximum :: Partial => Array Int -> Int
184194
unsafeMaximum arr = case maximum arr of
185195
Just m -> m
186196

187-
class
188-
Monoid m <= Action m a where
189-
act :: m -> a -> a
197+
-- ANCHOR: Action
198+
class Monoid m <= Action m a where
199+
act :: m -> a -> a
200+
-- ANCHOR_END: Action
190201

191-
newtype Multiply
192-
= Multiply Int
202+
-- ANCHOR: Multiply
203+
newtype Multiply = Multiply Int
204+
-- ANCHOR_END: Multiply
193205

206+
-- ANCHOR: semigroupMultiply
194207
instance semigroupMultiply :: Semigroup Multiply where
195-
append (Multiply n) (Multiply m) = Multiply (n * m)
208+
append (Multiply n) (Multiply m) = Multiply (n * m)
209+
-- ANCHOR_END: semigroupMultiply
196210

211+
-- ANCHOR: monoidMultiply
197212
instance monoidMultiply :: Monoid Multiply where
198-
mempty = Multiply 1
213+
mempty = Multiply 1
214+
-- ANCHOR_END: monoidMultiply
199215

216+
-- ANCHOR: Multiply_Action
200217
instance actionMultiplyInt :: Action Multiply Int where
218+
-- ANCHOR_END: Multiply_Action
201219
act (Multiply n) m = n * m
202220

203221
{-
@@ -227,14 +245,17 @@ instance actionMultiplyInt :: Action Multiply Int where
227245
derive newtype instance showMultiply :: Show Multiply
228246
derive newtype instance eqMultiply :: Eq Multiply
229247

248+
-- ANCHOR: actionMultiplyString
230249
instance actionMultiplyString :: Action Multiply String where
250+
-- ANCHOR_END: actionMultiplyString
231251
act (Multiply n) s = power s n
232252

233253
instance actionArray :: Action m a => Action m (Array a) where
234254
act m arr = map (act m) arr
235255

236-
newtype Self m
237-
= Self m
256+
-- ANCHOR: Self
257+
newtype Self m = Self m
258+
-- ANCHOR_END: Self
238259

239260
instance actionSelf :: Monoid m => Action m (Self m) where
240261
act m1 (Self m2) = Self (m1 <> m2)
@@ -250,11 +271,14 @@ arrayHasDuplicates arr =
250271
in
251272
length arr /= (length $ nubByEq hashAndValEqual arr)
252273

253-
newtype Hour
254-
= Hour Int
274+
-- ANCHOR: Hour
275+
newtype Hour = Hour Int
276+
-- ANCHOR_END: Hour
255277

278+
-- ANCHOR: eqHour
256279
instance eqHour :: Eq Hour where
257-
eq (Hour n) (Hour m) = mod n 12 == mod m 12
280+
eq (Hour n) (Hour m) = mod n 12 == mod m 12
281+
-- ANCHOR_END: eqHour
258282

259283
instance hashHour :: Hashable Hour where
260284
hash (Hour h) = hash $ mod h 12

0 commit comments

Comments
 (0)