Skip to content

Instantly share code, notes, and snippets.

View russmatney's full-sized avatar

Russell Matney russmatney

View GitHub Profile
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
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
-- 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'
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
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
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 []}
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"
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
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"
-- | 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 }