remove a Set of keys from a Map

Roman Cheplyaka roma at ro-che.info
Sat Dec 10 13:16:37 CET 2011


* Henning Thielemann <lemming at henning-thielemann.de> [2011-12-10 12:33:28+0100]
> I have (x :: Set key) and (m :: Map key a) and want to remove all
> keys and corresponding values from m that are in x. I could solve it
> efficiently with a
> 
> differenceSet :: Map key a -> Set key -> Map key a
> 
> or with a
> 
> Map.fromKeySet :: a -> Set key -> Map key a
> 
> or a
> 
> Map.fromKeySet :: (key -> a) -> Set key -> Map key a
> 
> but I did not find one of these functions in containers-0.4.2.

You can implement e.g. fromKeySet as

  fromKeySet a = Map.fromAscList . map (, a) . Set.toAscList

In practice, when conversion between Sets and Maps is needed often, I
found it convenient (and efficient) to emulate "Set a" by "Map a ()".

-- 
Roman I. Cheplyaka :: http://ro-che.info/



More information about the Libraries mailing list