This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| atIxNonExamples :: IO () | |
| atIxNonExamples = do | |
| let bob = User (UserName "bob") 42 Nothing HM.empty | |
| -- if you were doing this for-real, you would impl and use Data.Default | |
| defaultGoldItem = Item 0 0 | |
| print "Return the value of Bob's gold, whether he has it or not." | |
| print $ bob ^. inventory . at "gold" . non defaultGoldItem . value | |
| -- 0 | |
| print $ bob ^? inventory . at "gold" . _Just . value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| atIxExamples :: IO () | |
| atIxExamples = do | |
| -- Yep, you can use apostrophes in var names. Not that you should... | |
| let bob'sInventory = HM.fromList [ ("gold", Item 99 10) | |
| , ("silver", Item 10 9) | |
| ] | |
| bob = User (UserName "bob") 42 Nothing bob'sInventory | |
| print "Printing Bob's gold value" | |
| print $ bob ^? inventory . at "gold" . _Just . value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- StorageError in a module somewhere | |
| newtype StorageError = StorageError Text deriving (Eq, Show) | |
| -- WebError wraps storage Error | |
| data WebError | |
| = WebTextError Text | |
| | WebStorageError StorageError | |
| deriving (Eq, Show) | |
| -- Let's convert an 'Either StorageError a' to an 'Either WebError a' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| overExamples :: IO () | |
| overExamples = do | |
| let fitz = Pet (PetName "Fitz") | |
| let bob = User (UserName "bob") 0 (Just fitz) HM.empty | |
| print "Bob scores a point. Way to go, Bob." | |
| -- These all print bob with a score incremented by 1. | |
| print $ bob & score %~ (\sc -> sc + 1) | |
| print $ bob & score %~ (+1) | |
| print $ over score (+1) bob |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fancySetExamples :: IO () | |
| fancySetExamples = do | |
| let bob = User (UserName "bob") 0 Nothing HM.empty | |
| -- check out this multi-line string, why don't ya? | |
| print "Bob changes his name to 'Bill'\ | |
| \, updates his score, and now owns Jeff's pet fish,\ | |
| \who is named Fitzgerald." | |
| print $ | |
| bob |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| setExamples :: IO () | |
| setExamples = do | |
| let bob = User (UserName "bob") 0 Nothing HM.empty | |
| print "Bob, with an updated score" | |
| print $ set score 42 bob | |
| -- User {_userName = UserName "bob", _userScore = 42, _userPet = Nothing, _userInventory = fromList []} | |
| print $ (score .~ 42) bob | |
| -- User {_userName = UserName "bob", _userScore = 42, _userPet = Nothing, _userInventory = fromList []} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| previewExamples :: IO () | |
| previewExamples = do | |
| let maybeIntA = Just 1 | |
| -- Have to tell the compiler this was a 'Maybe Int' for it to be printable | |
| maybeIntB = Nothing :: Maybe Int | |
| print "maybeIntA" | |
| print $ maybeIntA ^? _Just | |
| -- Just 1 | |
| print "maybeIntB" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| composedViewExamples :: IO () | |
| composedViewExamples = do | |
| let | |
| bob = User (UserName "bob") 42 Nothing HM.empty | |
| fitzgerald = Pet (PetName "Fitzgerald") | |
| jeff = User (UserName "jeff") 42 (Just fitzgerald) HM.empty | |
| print "Bob's pet's name is: " | |
| print $ preview (pet . _Just . petName) bob | |
| -- Nothing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| viewExamples :: IO () | |
| viewExamples = do | |
| let bob = User (UserName "Bob") 42 Nothing HM.empty | |
| print "Bob's name is: " | |
| print $ view userName bob | |
| -- UserName "bob" | |
| print $ bob ^. userName | |
| -- UserName "bob" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- | A lens from a User to Text. | |
| -- | |
| -- Written quite explicitly with getter and setter helper functions to expose | |
| -- Lens's nature. | |
| userName :: Lens' User UserName | |
| userName = lens getter setter | |
| where | |
| getter user = _userName user | |
| setter user newName = user { _userName = newName } |