Skip to content

Instantly share code, notes, and snippets.

@supki
Created February 3, 2014 12:06
Show Gist options
  • Save supki/8782775 to your computer and use it in GitHub Desktop.
Save supki/8782775 to your computer and use it in GitHub Desktop.
An "introduction" to how to make *really* lazy lenses
module LazyPatterns where
import Control.Lens
{-# ANN module "HLint: ignore Use camelCase" #-}
data T = C { _x :: Int, _y :: Char } deriving (Show, Eq)
-- |
--
-- >>> undefined & strictX .~ 7
-- *** Exception: Prelude.undefined
strictX :: Lens' T Int
strictX f (C p y) = f p <&> \p' -> C p' y
-- |
--
-- >>> undefined & lazyX .~ 7
-- C {_x = 7, _y = *** Exception: Prelude.undefined
lazyX :: Lens' T Int
lazyX f ~(C p y) = f p <&> \p' -> C p' y
-- |
--
-- >>> undefined & lazyX .~ 7 & lazyY .~ 'f'
-- C {_x = 7, _y = 'f'}
lazyY :: Lens' T Char
lazyY f ~(C x p) = f p <&> \p' -> C x p'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment