Last active
March 19, 2017 16:28
-
-
Save markhibberd/9202073 to your computer and use it in GitHub Desktop.
haskell for lenses
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
data Lens a b = Lens { | |
get :: a -> b | |
, set :: b -> a -> a | |
} | |
type Lens' a b = | |
forall f. Functor f => (b -> f b) -> a -> f a | |
newtype Identity a = | |
Identity { runIdentity :: a } | |
newtype Const x a = | |
Const { runConst :: x } | |
class Functor f where | |
fmap :: (a -> b) -> f a -> f b | |
instance Functor (Const x) where | |
fmap f (Const x) = Const x | |
instance Functor Identity where | |
fmap f (Identity a) = Identity (f a) | |
get'' :: Lens' a b -> a -> b | |
get'' l a = | |
let x = Const -- :: b -> Const b b | |
y = l x -- :: a -> Const b a | |
z = y a -- :: Const b a | |
in runConst z | |
set'' :: Lens’ a b -> b -> a -> a | |
set'' l b a = | |
let x = const $ Identity b -- :: b -> Identity b | |
y = l x -- :: a -> Identity a | |
z = y a -- :: Identity a | |
in runIdentity z |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment