Skip to content

Instantly share code, notes, and snippets.

@pwm
Last active October 1, 2019 12:20
Show Gist options
  • Save pwm/6d00dd7b1d7d2617241f475a1de59232 to your computer and use it in GitHub Desktop.
Save pwm/6d00dd7b1d7d2617241f475a1de59232 to your computer and use it in GitHub Desktop.
Smart constructor vs. Generic Lens
module NegatePos where
import Control.Lens
import Data.Generics.Product
import Pos
negatePos :: Pos -> Pos
negatePos = position @1 %~ negate
-- (-1) == getPos (negatePos (fromJust (mkPos 1)))
module Pos (Pos, mkPos, getPos) where
import Prelude
import GHC.Generics
newtype Pos = MkPos Int deriving (Eq, Show, Generic)
mkPos :: Int -> Maybe Pos
mkPos n
| n > 0 = Just (MkPos n)
| otherwise = Nothing
getPos :: Pos -> Int
getPos (MkPos n) = n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment