[Haskell-cafe] Ord k => Data.Set.Set k -> (k->v) -> Data.Map.Map k v

Matthias Görgens matthias.goergens at googlemail.com
Wed Dec 2 08:16:02 EST 2009


I feel that Data.Set and Data.Map should be working together more
closely.  For example you can already get the keyset of a Map, but the
`other way' is not built-in.  I mean a function with a signature like

Ord k => Data.Set.Set k -> (k->v) -> Data.Map.Map k v

You can implement it in O(n):

> assoc :: (a->b) -> [a] -> [(a,b)]
> assoc f = map (\x -> (x, f x))

> mapToMap :: Ord k => (k -> v) -> Data.Set.Set k -> Data.Map.Map k v
> mapToMap f = Data.Map.fromAscList . assoc f . Data.Set.toAscList

The name assoc alludes to the assoc-lists of Lisp.


More information about the Haskell-Cafe mailing list