Skip to content

Instantly share code, notes, and snippets.

@paolino
Last active March 12, 2022 17:52
Show Gist options
  • Save paolino/5707d291a00d329426cee13d94b2533c to your computer and use it in GitHub Desktop.
Save paolino/5707d291a00d329426cee13d94b2533c to your computer and use it in GitHub Desktop.
Parameterize the cartesian sorting
{-# LANGUAGE BlockArguments #-}
module CartesianProductEx where
import Control.Arrow ((***))
import Data.List (sortOn)
import Test.Hspec (shouldBe)
-- given
data A = A1 | A2 deriving (Show, Enum, Bounded, Eq)
data B = B1 | B2 deriving (Show, Enum, Bounded, Eq)
allOf :: (Bounded a, Enum a) => [a]
allOf = [minBound .. maxBound]
-- define
evalB :: Int -> Int
evalB = error "not implemented"
evalA :: Int -> Int
evalA = error "not implemented"
composeEval :: Int -> Int -> Int
composeEval = error "not implemented"
-- such that
sortedAxB :: [(A, B)]
sortedAxB = sortOn
do uncurry composeEval . (evalA . fromEnum *** evalB . fromEnum)
do (,) <$> allOf <*> allOf
-- satisfies
main :: IO ()
main = sortedAxB `shouldBe` [(A2, B1), (A1, B1), (A1, B2), (A2, B2)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment