EnumSet and EnumMap

Chris Kuklewicz haskell at list.mightyreason.com
Fri Feb 23 10:24:27 EST 2007


I could not quickly find anyone else writing this boiler plate, so I have posted
 this useful wrapper on the Haskell wiki at
http://haskell.org/haskellwiki/EnumSet_EnumMap

This uses a cheap newtype to wrap IntSet and IntMap so that you can store any
Enum in them.  It saves either writing many toEnum/fromEnum functions or using
the slower and more generic Data.Set and Data.Map.

The original motivation was to go from Map Char to IntMap.

And as a bonus, the type signature of the newtype is the same kind as Data.Set
and Data.Map (which matters when declaring instances...)

> newtype EnumSet e = EnumSet {unEnumSet :: S.IntSet}
>   deriving (Monoid,Eq,Read,Show,Ord)

> newtype EnumMap k a = EnumMap {unEnumMap :: M.IntMap a}
>   deriving (Eq,Read,Show,Ord,Monoid,Foldable,Functor)

This has been quickly tested with GHC 6.6 and may contain typographical errors I
have not caught yet.

-- 
Chris



More information about the Libraries mailing list