Skip to content

Instantly share code, notes, and snippets.

@russmatney
Last active May 12, 2018 03:40
Show Gist options
  • Save russmatney/a93c4f5be4aec8ee7fbce14bf219e265 to your computer and use it in GitHub Desktop.
Save russmatney/a93c4f5be4aec8ee7fbce14bf219e265 to your computer and use it in GitHub Desktop.
-- | 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 }
score :: Lens' User Int
score = lens _userScore (\user newScore -> user { _userScore = newScore })
-- | Note that this lens targets a 'Maybe Pet'.
pet :: Lens' User (Maybe Pet)
pet = lens _userPet (\user maybePet -> user {_userPet = maybePet})
-- | Single letter vars, seriously? Yep.
inventory :: Lens' User Inventory
inventory = lens _userInventory (\u i -> u { _userInventory = i })
------------------------------------------------------------------------------
petName :: Lens' Pet PetName
petName = lens _petName (\p n -> p { _petName = n })
------------------------------------------------------------------------------
value :: Lens' Item Int
value = lens _itemValue (\i v -> i { _itemValue = v })
weight :: Lens' Item Int
weight = lens _itemWeight (\i w -> i { _itemWeight = w })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment