Skip to content

add anchors for chapter 6 #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions exercises/chapter6/src/Data/Hashable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,57 @@ import Data.Maybe (Maybe(..))
import Data.String.CodeUnits (toCharArray)
import Data.Tuple (Tuple(..))

-- ANCHOR: Hashable
newtype HashCode = HashCode Int

instance hashCodeEq :: Eq HashCode where
eq (HashCode a) (HashCode b) = a == b

hashCode :: Int -> HashCode
hashCode h = HashCode (h `mod` 65535)

class Eq a <= Hashable a where
hash :: a -> HashCode
-- ANCHOR_END: Hashable

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

derive instance eqHashCode :: Eq HashCode

-- ANCHOR: combineHashes
combineHashes :: HashCode -> HashCode -> HashCode
combineHashes (HashCode h1) (HashCode h2) = hashCode (73 * h1 + 51 * h2)
-- ANCHOR_END: combineHashes

-- ANCHOR: hashEqual
hashEqual :: forall a. Hashable a => a -> a -> Boolean
hashEqual = eq `on` hash
-- ANCHOR_END: hashEqual

-- ANCHOR: hashChar
instance hashChar :: Hashable Char where
hash = hash <<< toCharCode
-- ANCHOR_END: hashChar

-- ANCHOR: hashString
instance hashString :: Hashable String where
hash = hash <<< toCharArray
-- ANCHOR_END: hashString

-- ANCHOR: hashInt
instance hashInt :: Hashable Int where
hash = hashCode
-- ANCHOR_END: hashInt

-- ANCHOR: hashBoolean
instance hashBoolean :: Hashable Boolean where
hash false = hashCode 0
hash true = hashCode 1
-- ANCHOR_END: hashBoolean

-- ANCHOR: hashArray
instance hashArray :: Hashable a => Hashable (Array a) where
hash = foldl combineHashes (hashCode 0) <<< map hash
-- ANCHOR_END: hashArray

instance hashMaybe :: Hashable a => Hashable (Maybe a) where
hash Nothing = hashCode 0
Expand Down
78 changes: 51 additions & 27 deletions exercises/chapter6/test/no-peeking/Solutions.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@ import Data.Maybe (Maybe(..))
import Data.Monoid (power)
import Data.Newtype (class Newtype, over2, wrap)

-- ANCHOR: Point
newtype Point
= Point
{ x :: Number
, y :: Number
}
= Point
{ x :: Number
, y :: Number
}
-- ANCHOR_END: Point

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

-- ANCHOR: Complex
newtype Complex
= Complex
{ real :: Number
, imaginary :: Number
}
-- ANCHOR_END: Complex

instance showComplex :: Show Complex where
show (Complex c) =
Expand Down Expand Up @@ -83,11 +87,13 @@ instance ringComplex :: Ring Complex where
sub (Complex a) (Complex b) = Complex $ a - b
-}

-- ANCHOR: Shape
data Shape
= Circle Point Number
| Rectangle Point Number Number
| Line Point Point
| Text Point String
= Circle Point Number
| Rectangle Point Number Number
| Line Point Point
| Text Point String
-- ANCHOR_END: Shape

derive instance genericShape :: Generic Shape _

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

data NonEmpty a
= NonEmpty a (Array a)
-- ANCHOR: NonEmpty
data NonEmpty a = NonEmpty a (Array a)
-- ANCHOR_END: NonEmpty

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

data Extended a
= Infinite
| Finite a
-- ANCHOR: Extended
data Extended a = Infinite | Finite a
-- ANCHOR_END: Extended

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

data OneMore f a
= OneMore a (f a)
-- ANCHOR: OneMore
data OneMore f a = OneMore a (f a)
-- ANCHOR_END: OneMore

-- ANCHOR: OneMore_Foldable
instance foldableOneMore :: Foldable f => Foldable (OneMore f) where
-- ANCHOR_END: OneMore_Foldable
foldr func st (OneMore val more) = func val lastB
where
lastB = foldr func st more
Expand All @@ -184,20 +194,28 @@ unsafeMaximum :: Partial => Array Int -> Int
unsafeMaximum arr = case maximum arr of
Just m -> m

class
Monoid m <= Action m a where
act :: m -> a -> a
-- ANCHOR: Action
class Monoid m <= Action m a where
act :: m -> a -> a
-- ANCHOR_END: Action

newtype Multiply
= Multiply Int
-- ANCHOR: Multiply
newtype Multiply = Multiply Int
-- ANCHOR_END: Multiply

-- ANCHOR: semigroupMultiply
instance semigroupMultiply :: Semigroup Multiply where
append (Multiply n) (Multiply m) = Multiply (n * m)
append (Multiply n) (Multiply m) = Multiply (n * m)
-- ANCHOR_END: semigroupMultiply

-- ANCHOR: monoidMultiply
instance monoidMultiply :: Monoid Multiply where
mempty = Multiply 1
mempty = Multiply 1
-- ANCHOR_END: monoidMultiply

-- ANCHOR: Multiply_Action
instance actionMultiplyInt :: Action Multiply Int where
-- ANCHOR_END: Multiply_Action
act (Multiply n) m = n * m

{-
Expand Down Expand Up @@ -227,14 +245,17 @@ instance actionMultiplyInt :: Action Multiply Int where
derive newtype instance showMultiply :: Show Multiply
derive newtype instance eqMultiply :: Eq Multiply

-- ANCHOR: actionMultiplyString
instance actionMultiplyString :: Action Multiply String where
-- ANCHOR_END: actionMultiplyString
act (Multiply n) s = power s n

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

newtype Self m
= Self m
-- ANCHOR: Self
newtype Self m = Self m
-- ANCHOR_END: Self

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

newtype Hour
= Hour Int
-- ANCHOR: Hour
newtype Hour = Hour Int
-- ANCHOR_END: Hour

-- ANCHOR: eqHour
instance eqHour :: Eq Hour where
eq (Hour n) (Hour m) = mod n 12 == mod m 12
eq (Hour n) (Hour m) = mod n 12 == mod m 12
-- ANCHOR_END: eqHour

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