
22 Jul
2011
22 Jul
'11
9:40 p.m.
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