Skip to content

Instantly share code, notes, and snippets.

@steshaw
Created May 6, 2020 04:52
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 steshaw/489cdf4351cb932a3e77ece609241dc1 to your computer and use it in GitHub Desktop.
Save steshaw/489cdf4351cb932a3e77ece609241dc1 to your computer and use it in GitHub Desktop.
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE DerivingStrategies #-}
import Data.Either
data Decl a
= TypeSig String a
| Law String a a
| FunModel String a
| TypeModel String a
| Import a
| Other a
deriving stock Show
decls =
[ TypeSig "Hello" 1,
Law "Fred" 1 42,
FunModel "Fun" 2,
Import 10,
Other 99,
Law "Be good" 2 3,
TypeModel "huh" 0
]
partitionThem decls =
let (laws, others1) = partitionEithers $ extractLaws decls
(funModels, others2) = partitionEithers $ extractFunModels others1
(typeModels, leftovers) = partitionEithers $ extractTypeModels others2
in (laws, funModels, typeModels, leftovers)
where
extractLaws = map (\case law@Law {} -> Left law; d -> Right d)
extractFunModels = map (\case fm@FunModel {} -> Left fm; d -> Right d)
extractTypeModels = map (\case tm@TypeModel {} -> Left tm; d -> Right d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment