Skip to content

Instantly share code, notes, and snippets.

@markhibberd
Last active March 19, 2017 16:28
Show Gist options
  • Save markhibberd/9202073 to your computer and use it in GitHub Desktop.
Save markhibberd/9202073 to your computer and use it in GitHub Desktop.
haskell for lenses
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