-
Notifications
You must be signed in to change notification settings - Fork 195
Ch6 - Clarify Action exercises #228
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,16 +89,14 @@ instance semigroupMultiply :: Semigroup Multiply where | |
instance monoidMultiply :: Monoid Multiply where | ||
mempty = Multiply 1 | ||
|
||
instance actionMultiply :: Action Multiply Int where | ||
instance actionMultiplyInt :: Action Multiply Int where | ||
act (Multiply n) m = n * m | ||
|
||
instance showMultiply :: Show Multiply where | ||
show (Multiply n) = "Multiply " <> show n | ||
-- These may also be written manualy | ||
derive newtype instance showMultiply :: Show Multiply | ||
derive newtype instance eqMultiply :: Eq Multiply | ||
|
||
instance eqMultiply :: Eq Multiply where | ||
eq (Multiply n) (Multiply m) = n == m | ||
|
||
instance repeatAction :: Action Multiply String where | ||
instance actionMultiplyString :: Action Multiply String where | ||
act (Multiply n) s = power s n | ||
|
||
instance actionArray :: Action m a => Action m (Array a) where | ||
|
@@ -107,23 +105,14 @@ instance actionArray :: Action m a => Action m (Array a) where | |
newtype Self m | ||
= Self m | ||
|
||
-- Why is Monoid constraint required here? | ||
-- Seems like this is already specified by Action class | ||
--instance actionSelf :: Action m (Self m) where | ||
instance actionSelf :: Monoid m => Action m (Self m) where | ||
act m1 (Self m2) = Self (m1 <> m2) | ||
|
||
instance eqSelf :: Eq m => Eq (Self m) where | ||
eq (Self m1) (Self m2) = m1 == m2 | ||
|
||
instance showSelf :: Show m => Show (Self m) where | ||
show (Self m) = "Self " <> show m | ||
|
||
instance semigroupSelf :: Semigroup m => Semigroup (Self m) where | ||
append (Self a) (Self b) = Self (a <> b) | ||
|
||
instance monoidSelf :: Monoid m => Monoid (Self m) where | ||
mempty = Self mempty | ||
-- These may also be written manualy | ||
derive newtype instance showSelf :: Show m => Show (Self m) | ||
derive newtype instance eqSelf :: Eq m => Eq (Self m) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also fine to write without newtype wrapper: derive instance eqSelf :: Eq m => Eq (Self m) |
||
derive newtype instance semigroupSelf :: Semigroup m => Semigroup (Self m) | ||
derive newtype instance monoidSelf :: Monoid m => Monoid (Self m) | ||
|
||
instance repeatActionMultSelf :: Action (Self Multiply) Int where | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might not be the best instance name (doesn't follow the earlier convention of just squishing everything together), although this doesn't really matter. |
||
act (Self (Multiply m)) s = m * s | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the
Monoid
constraint required here? It seems like this superclass relationship is already specified by theAction
class and that it should be possible to write it like this instead:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Answer: