Skip to content

Instantly share code, notes, and snippets.

@russmatney
Created May 12, 2018 03:37
Show Gist options
  • Save russmatney/3baef3888e458b20e466d0509b5f6dbc to your computer and use it in GitHub Desktop.
Save russmatney/3baef3888e458b20e466d0509b5f6dbc to your computer and use it in GitHub Desktop.
toListOfExamples :: IO ()
toListOfExamples = do
let tory = HM.fromList [ ("gold", Item 99 10)
, ("silver", Item 10 9)
]
bob = User (UserName "bob") 42 Nothing tory
print "A list of Bob's items"
print $ bob ^.. inventory . folded
-- [Item {_itemValue = 10, _itemWeight = 9},Item {_itemValue = 99, _itemWeight = 10}]
print $ toListOf (inventory . folded) bob
-- [Item {_itemValue = 10, _itemWeight = 9},Item {_itemValue = 99, _itemWeight = 10}]
print "Bob uses ifolded . asIndex to list itemNames."
print $ bob ^.. inventory . ifolded . asIndex
-- ["silver","gold"]
print "Bob's filtering to only his valuable items."
print $
bob ^.. inventory . folded . filtered (\item -> (item ^. value) > 50)
-- [Item {_itemValue = 99, _itemWeight = 10}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment