Skip to content

Instantly share code, notes, and snippets.

@gavinwahl
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gavinwahl/8759556 to your computer and use it in GitHub Desktop.
Save gavinwahl/8759556 to your computer and use it in GitHub Desktop.
{-# LANGUAGE RankNTypes #-}
type Lens t i = Functor f => (i -> f i) -> t -> f t
data Person = Person { _name :: String, _age :: Int } deriving Show
nameLens :: Lens Person String
nameLens f (Person name age) = fmap (\newName -> Person newName age) (f name)
ageLens :: Lens Person Int
ageLens f (Person name age) = fmap (\newAge -> Person name newAge) (f age)
set :: Lens t i -> t -> i -> t
set l x v = l (flip const) x v
get :: Lens t i -> t -> i
get l x = val
where (Left val) = l Left x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment