Created
April 16, 2016 11:35
-
-
Save akhileshs/a6e514fb36e5e521fa19882092b05af8 to your computer and use it in GitHub Desktop.
This file contains 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 #-} | |
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