Oops that was supposed to be
mapOutList list = M.fromListWith (++) $ map (second (: [])) list
In my application I find an issue coming up frequently. I have a map
Ord k => Map k [a]
That is, a list of things may be stored at each key. I sometimes want to convert it to
[(k,a)]
where each value is paired with its key, and some keys may repeat in that form of the list. No particular order is needed.
so I have the following code. Would appreciate hearing if there are any tricks I'm missing
import qualified Map as M
listOutMap :: Ord k => Map k [a] -> [(k,a)]
listOutMap m = concatMap (\(k,a) -> zip (repeat k) a) (M.toList m)
mapOutList :: Ord k => [(k,a)] -> Mpa k [a]
mapOutList list = M.fromList $ map (second (: [])) list