Skip to content

Instantly share code, notes, and snippets.

@jroesch
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save jroesch/9794876 to your computer and use it in GitHub Desktop.

Select an option

Save jroesch/9794876 to your computer and use it in GitHub Desktop.
A simple lens presentation, saving for posterities sake.
{-# LANGUAGE RankNTypes #-}
module SimpleLens where
import Data.Functor.Identity
import Data.Functor.Constant
type LensP s t a b = forall f. Functor f => (a -> f b) -> s -> f t
type Lens s a = LensP s s a a
get :: Lens s a -> s -> a
get ln s = getConstant $ ln Constant s
set :: Lens s a -> a -> s -> s
set ln x = runIdentity . ln (Identity . const x)
_1 :: Lens (a, b) a
_1 f (x, y) = fmap (\x' -> (x', y)) (f x)
_2 :: Lens (a, b) b
_2 f (x, y) = fmap (\y' -> (x, y')) (f y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment