module GridBox.Grouping
( GroupedByRow
, GroupedRow
, groupBoxesByRow
) where
import GridBox.Box (Box, BoxRow, BoxCol, fromBoxes)
import Data.List (sortBy, groupBy)
import Data.Function (on)
import Data.Ord (comparing)
import Control.Arrow ((&&&))
type GroupedByRow = [GroupedRow]
type GroupedRow = (BoxRow, [BoxCol])
groupBoxesByRow :: [Box] -> GroupedByRow
groupBoxesByRow =
groupPairs . fromBoxes
groupPairs :: Ord a => [(a, b)] -> [(a, [b])]
groupPairs =
map ((fst . head) &&& map snd)
. groupBy ((==) `on` fst)
. sortBy (comparing fst)