Skip to content

Instantly share code, notes, and snippets.

@Heimdell
Created December 12, 2016 18:33
Show Gist options
  • Select an option

  • Save Heimdell/0370cd22c4902727900e3371ab8f3091 to your computer and use it in GitHub Desktop.

Select an option

Save Heimdell/0370cd22c4902727900e3371ab8f3091 to your computer and use it in GitHub Desktop.
{-# language TupleSections #-}
import Control.Arrow ((&&&))
import Data.Function (on)
import Data.List (groupBy, sortBy)
import Data.Ord (comparing)
f :: Ord k => [(v, [(k, x)])] -> [(k, [(v, x)])]
f = map (fst.head &&& map snd) -- [[(k, (v, x))]] -> [(k, [(v, x)])]
. groupBy ((==) `on` fst) -- [(k, (v, x))] -> [[(k, (v, x))]]
. sortBy (comparing fst) -- [(k, (v, x))] -> [(k, (v, x))]
. map (\(a, (b, c)) -> (b, (a, c))) -- [(v, (k, x))] -> [(k, (v, x))]
. (uncurry (map . (,)) =<<) -- [(v, [(k, x)])] -> [(v, (k, x))]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment