Skip to content

Instantly share code, notes, and snippets.

@dminuoso
Created December 3, 2020 22:37
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 dminuoso/78b9e52de12306ce5511d367200dbebe to your computer and use it in GitHub Desktop.
Save dminuoso/78b9e52de12306ce5511d367200dbebe to your computer and use it in GitHub Desktop.
class Normalize f where
normalize :: f -> f
default normalize :: (Generic f, GenericNormalize (Rep f)) => f -> f
normalize = to . genericNormalize . from
instance {-# OVERLAPPABLE #-} Normalize a where
normalize = id
instance Normalize (Maybe T.Text) where
normalize (Just "") = Nothing
normalize x = x
instance Normalize (Maybe [a]) where
normalize (Just []) = Nothing
normalize x = x
class GenericNormalize f where
genericNormalize :: f p -> f p
instance (GenericNormalize f, GenericNormalize g) => GenericNormalize (f :*: g) where
genericNormalize (a :*: b) =
genericNormalize a :*: genericNormalize b
instance (GenericNormalize f, GenericNormalize g) => GenericNormalize (f :+: g) where
genericNormalize (L1 a) = L1 (genericNormalize a)
genericNormalize (R1 a) = R1 (genericNormalize a)
instance GenericNormalize f => GenericNormalize (M1 i c f) where
genericNormalize (M1 a) = M1 (genericNormalize a)
instance (Normalize a) => GenericNormalize (Rec0 (Maybe a)) where
genericNormalize (K1 a) = K1 (normalize a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment