[Haskell-cafe] Sparse records/ADTs
Roman Cheplyaka
roma at ro-che.info
Wed Oct 24 13:06:12 CEST 2012
* Roman Cheplyaka <roma at ro-che.info> [2012-10-24 13:55:17+0300]
> * Jon Fairbairn <jon.fairbairn at cl.cam.ac.uk> [2012-10-24 11:08:29+0100]
> > Is there a convenient way of handling a data structure with lots
> > of fields of different types that may or may not be filled in?
> >
> > Something equivalent to
> >
> > data D = D {a::Maybe A, b::Maybe B, c::Maybe C, …}
> >
> > but with better space efficiency and a more convenient empty
> > object.
> >
> > An easy alternative is
> >
> > data E = Ea A | Eb B | Ec C | …
> > type R = [E]
> >
> > which has a straightforward empty object, but one then must
> > define
> >
> > getA e = listToMaybe [a | Ea a <- e]
> >
> > for each field, which is tedious (and O(n)). Obviously Templates
> > would help, but is there an alternative I’ve missed?
>
> For runtime efficiency it's better to use Data.Map.
Actually, you can use Data.IntMap for even better performance, if you
define an Enum instance for your keys.
Roman
More information about the Haskell-Cafe
mailing list