Skip to content

Instantly share code, notes, and snippets.

@akhileshs
Created April 16, 2016 11:35
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 akhileshs/a6e514fb36e5e521fa19882092b05af8 to your computer and use it in GitHub Desktop.
Save akhileshs/a6e514fb36e5e521fa19882092b05af8 to your computer and use it in GitHub Desktop.
{-# LANGUAGE RankNTypes #-}
module F where
data User = User { name :: String, age :: Int } deriving Show
data Project = Project { owner :: User } deriving Show
type Lens s a = Functor f => (a -> f a) -> s -> f s
newtype Identity a = Identity { runIdentity :: a }
instance Functor Identity where
fmap f (Identity a) = Identity (f a)
over :: Lens s a -> (a -> a) -> s -> s
over ln f s = runIdentity $ ln (Identity . f) s
-- myover ln f = runIdentity . ln (Identity . f)
newtype Const a b = Const { getConst :: a }
instance Functor (Const a) where
fmap _ (Const a) = Const a
view :: Lens s a -> s -> a
view ln s = getConst $ ln Const s
set :: Lens s a -> a -> s -> s
set ln x = over ln (const x)
_1 :: Functor f => (a -> f a) -> (a, b) -> f (a, b)
_1 f (x, y) = fmap (\a -> (a, y)) (f x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment