[Haskell-cafe] Data.Map: Values to keys and keys to values
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Data.Map: Values to keys and keys to values
On Thu, Jun 16, 2011 at 3:01 PM, Dmitri O.Kondratiev doko...@gmail.com 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? Note that such a function would be lossy as there might be duplicate values in the map. Cheers, Johan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Data.Map: Values to keys and keys to values
Why not make it unlossy and have: trans :: (Ord k, Ord a) = Map k a - Map a (Set k) On Thu, Jun 16, 2011 at 9:10 AM, Johan Tibell johan.tib...@gmail.comwrote: On Thu, Jun 16, 2011 at 3:01 PM, Dmitri O.Kondratiev doko...@gmail.com 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? Note that such a function would be lossy as there might be duplicate values in the map. Cheers, Johan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Data.Map: Values to keys and keys to values
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@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. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Data.Map: Values to keys and keys to values
On Thu, Jun 16, 2011 at 3:01 PM, Dmitri O.Kondratiev doko...@gmail.com 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 I don't think implementing this function in the library would add much as it cannot be implemented more efficiently with access to the internal representation than it can using the public API. Just write transMap = M.fromList . map swap . M.toList and stick it in some utility file. Johan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Data.Map: Values to keys and keys to values
On Thu, Jun 16, 2011 at 5:38 PM, Johan Tibell johan.tib...@gmail.comwrote: On Thu, Jun 16, 2011 at 3:01 PM, Dmitri O.Kondratiev doko...@gmail.com 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 I don't think implementing this function in the library would add much as it cannot be implemented more efficiently with access to the internal representation than it can using the public API. Just write transMap = M.fromList . map swap . M.toList and stick it in some utility file. Johan Yes, this is a good one. Thanks! ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Data.Map: Values to keys and keys to values
On Thu, Jun 16, 2011 at 04:17:55PM +0200, Francesco Mazzoli wrote: 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@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 ? Or, if you want to keep duplicates, import qualified Data.Set as S import Control.Arrow (second) transMap = M.fromListWith S.union . map (second S.singleton . swap) . M.toList -Brent ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe