Created
November 28, 2019 17:55
-
-
Save ChrisPenner/0b963f85feb44b77f091f231247e6a2c to your computer and use it in GitHub Desktop.
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
| import Control.Lens | |
| import Data.Typeable | |
| import Data.Dynamic | |
| dynamicLens :: Typeable a => (Lens' s a) -> Lens' s Dynamic | |
| dynamicLens l = lens getter setter | |
| where | |
| getter s = toDyn $ view l s | |
| setter s b = | |
| case fromDynamic b of | |
| Nothing -> s | |
| Just b' -> set l b' s | |
| dynamicTraversal :: Typeable a => (Lens' s a) -> LensLike' Maybe s Dynamic | |
| dynamicTraversal l f s = do | |
| let a = view l s | |
| altered <- f (toDyn a) | |
| casted <- fromDynamic altered | |
| return $ set l casted s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment