Skip to content

Instantly share code, notes, and snippets.

@tonymorris
Created June 18, 2019 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonymorris/ea6705967a798d8e14caae63e3e3fb35 to your computer and use it in GitHub Desktop.
Save tonymorris/ea6705967a798d8e14caae63e3e3fb35 to your computer and use it in GitHub Desktop.
import Data.Functor.Identity
data A = A
deriving (Eq, Show)
data B = B
deriving (Eq, Show)
data C = C
deriving (Eq, Show)
data D = D
deriving (Eq, Show)
-- solved by: classy lens
getC_ABC :: (A, B, C) -> C
getC_ABC (_, _, c) = c
getC_ABCD :: (A, B, C, D) -> C
getC_ABCD (_, _, c, _) = c
insertRecord_ABC' :: f (A, B, C) -> [f (A, B, C)] -> [f (A, B, C)]
insertRecord_ABC' = (:)
insertRecord_ABC :: (A, B, C) -> [(A, B, C)] -> [(A, B, C)]
insertRecord_ABC r rs = runIdentity <$> (insertRecord_ABC' (Identity r) (Identity <$> rs))
insertRecord_ABCD :: (A, B, C, D) -> [(A, B, C, D)] -> [(A, B, C, D)]
insertRecord_ABCD r rs = let liftRec (a, b, c, d) = (d, (a, b, c))
unliftRec (d, (a, b, c)) = (a, b, c, d)
in unliftRec <$> (insertRecord_ABC' (liftRec r) (liftRec <$> rs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment