[Haskell-cafe] Data.Map: Values to keys and keys to values

Francesco Mazzoli f at mazzo.li
Thu Jun 16 16:17:55 CEST 2011


On 16/06/11 15:01, Dmitri O.Kondratiev wrote:
> Hi,
> Data.Map has many great functions, yet I could not find the one that
> allows from one map create another map where keys are values and values
> are keys of the first one.
> Something like:
> transMap:: (Ord k, Ord a) => Map k a -> Map a k
>
> Does such function exist?
> Thanks!
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

What about something like

transMap :: (Ord k, Ord a) => Map k a -> Map a k
transMap = M.fromList . map swap . M.toList

?

Francesco.



More information about the Haskell-Cafe mailing list