Skip to content

Commit 9c3292a

Browse files
authored
Ch7 Ch8 Ch10 generic showPhoneType (#249)
* Ch10 - adjust generic alignment * Ch7 - remove derive explanation * Ch7 - Improve showPhoneType alignment * Ch7 - generic showPhoneType * Ch8 - generic showPhoneType
1 parent 53ac94d commit 9c3292a

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

exercises/chapter10/src/Data/AddressBook.purs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ data PhoneType
2626
| OtherPhone
2727

2828
-- ANCHOR: PhoneType_generic
29-
derive instance genericPhoneType :: Generic PhoneType _
30-
instance encodeJsonPhoneType :: EncodeJson PhoneType where encodeJson = genericEncodeJson
31-
instance decodeJsonPhoneType :: DecodeJson PhoneType where decodeJson = genericDecodeJson
29+
derive instance genericPhoneType :: Generic PhoneType _
30+
31+
instance encodeJsonPhoneType :: EncodeJson PhoneType where encodeJson = genericEncodeJson
32+
instance decodeJsonPhoneType :: DecodeJson PhoneType where decodeJson = genericDecodeJson
3233
-- ANCHOR_END: PhoneType_generic
33-
instance showPhoneType :: Show PhoneType where show = genericShow
34+
instance showPhoneType :: Show PhoneType where show = genericShow
3435

3536
type PhoneNumber
3637
= { "type" :: PhoneType

exercises/chapter7/src/Data/AddressBook.purs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module Data.AddressBook where
22

33
import Prelude
4+
import Data.Generic.Rep (class Generic)
5+
import Data.Generic.Rep.Show (genericShow)
46

57
type Address
68
= { street :: String
@@ -21,18 +23,26 @@ data PhoneType
2123
| OtherPhone
2224
-- ANCHOR_END: PhoneType
2325

24-
{-| derive has not been discussed yet but will be
25-
covered in Ch 10. Here it is needed by the unit
26-
tests to define how to compare the PhoneType values
26+
{-
27+
Eq and Show instances are needed by unit tests to
28+
compare and report differences between PhoneType values
2729
(HomePhone, WorkPhone, etc).
2830
-}
2931
derive instance eqPhoneType :: Eq PhoneType
3032

33+
-- Generic Show instance
34+
derive instance genericPhoneType :: Generic PhoneType _
35+
36+
instance showPhoneType :: Show PhoneType where
37+
show = genericShow
38+
{-
39+
-- Manually-written Show instance
3140
instance showPhoneType :: Show PhoneType where
32-
show HomePhone = "HomePhone"
33-
show WorkPhone = "WorkPhone"
34-
show CellPhone = "CellPhone"
41+
show HomePhone = "HomePhone"
42+
show WorkPhone = "WorkPhone"
43+
show CellPhone = "CellPhone"
3544
show OtherPhone = "OtherPhone"
45+
-}
3646

3747
type PhoneNumber
3848
= { "type" :: PhoneType

exercises/chapter8/src/Data/AddressBook.purs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module Data.AddressBook where
22

33
import Prelude
4+
import Data.Generic.Rep (class Generic)
5+
import Data.Generic.Rep.Show (genericShow)
46

57
type Address
68
= { street :: String
@@ -17,11 +19,10 @@ data PhoneType
1719
| CellPhone
1820
| OtherPhone
1921

22+
derive instance genericPhoneType :: Generic PhoneType _
23+
2024
instance showPhoneType :: Show PhoneType where
21-
show HomePhone = "HomePhone"
22-
show WorkPhone = "WorkPhone"
23-
show CellPhone = "CellPhone"
24-
show OtherPhone = "OtherPhone"
25+
show = genericShow
2526

2627
type PhoneNumber
2728
= { "type" :: PhoneType

0 commit comments

Comments
 (0)