Skip to content

Instantly share code, notes, and snippets.

@steshaw
Created May 6, 2020 05:12
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/33a5c95ce2b9cb199890b97d6bf81821 to your computer and use it in GitHub Desktop.
Save steshaw/33a5c95ce2b9cb199890b97d6bf81821 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE LambdaCase #-}
{-# OPTIONS_GHC -Wall #-}
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 :: [Decl Integer]
decls =
[ TypeSig "Hello" 1,
Law "Fred" 1 42,
FunModel "Fun" 2,
Import 10,
Other 99,
Law "Be good" 2 3,
TypeModel "huh" 0
]
-- Loops forever. Every `let` is `letrec` :-(
partitionThem :: [Decl a] -> ([Decl a], [Decl a], [Decl a], [Decl a])
partitionThem ds =
let (laws, leftovers) = partitionEithers $ extractLaws ds
in let (funModels, leftovers) = partitionEithers $ extractFunModels leftovers
in let (typeModels, leftovers) = partitionEithers $ extractTypeModels leftovers
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