Created
February 3, 2014 12:06
-
-
Save supki/8782775 to your computer and use it in GitHub Desktop.
An "introduction" to how to make *really* lazy lenses
This file contains 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
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