Last active
April 15, 2018 13:52
-
-
Save danidiaz/ff76231a4d666415c10e7504784ff128 to your computer and use it in GitHub Desktop.
Teasing out fields
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
| {-# LANGUAGE RankNTypes #-} | |
| data Person = Person { _name :: String , _age :: Int } deriving Show | |
| makeLenses 'Person | |
| example :: Person | |
| example = Person "Bob" 55 | |
| pry :: Lens' r x -> Iso' r (x -> r,x) | |
| pry l = iso (\r -> (\x -> set l x r, view l r)) (uncurry ($)) | |
| consName :: Prism' Person (Char,Person) | |
| consName = | |
| pry name . aside _Cons . iso (\(a,(b,c)) -> (b,(a,c))) (\(b,(a,c)) -> (a,(b,c))) . seconding (Control.Lens.from (pry name)) | |
| -- Λ review consName ('z',example) | |
| -- Person {_name = "zBob", _age = 55} | |
| -- | |
| -- Λ preview consName example | |
| -- Just ('B',Person {_name = "ob", _age = 55}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment