public
Last active

  • Download Gist
lens.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
{-# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.