Last active
August 29, 2015 13:57
-
-
Save jroesch/9794876 to your computer and use it in GitHub Desktop.
A simple lens presentation, saving for posterities sake.
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
| {-# 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