I’d like to define a fixed set of values, they are all integral,
but elements are with different type(Int8/Int16/Int32/Integer).
the type of each element is fixed, and there will be only get/set
functions. like ‘java beans’.
with my intuition, It may be:
data MyFixedSet = MyFixedSet {getva :: Int32, getvb :: Int8,
getvc :: Integer, … , getvxx :: Int16}
or use tuples:
data MyFixedSet = MyFixedSet (Int32,Int8,Integer, … Int16)
or :
type MyFixedSet = (Int32,Int8,Integer, … Int16)
then I what to have functions to operate with it:
getVA (va,,,…..,) = va
setVA (,vb,vc,…..vxx) va = (va,vb,vc,…vxx)
getVA (,vb,,…..,) = vb
setVA (va,,vc,…..vxx) vb = (va,vb,vc,…vxx)
…
but I think they are too heavy to use, in my case, there will be
200~500 elements in a set.
Should I use Map?
if I do this:
data Elem = X8 Int8 | X16 Int16 | … | XI Integer
let set = Map.fromList [(0, X32 1234), (1, X8 666), …]
then I have to do some Type-check when I extract things from Map.
I’d like to know is there is a ‘beautiful’ and ‘efficient’ way to do this?
how about their performance?
The record-based solution may indeed be too heavy, especially when updating values. Considering the values in Haskell are immutable, every record update requires construction of a new object. In your case this object will be quite large, so its construction may be slow.
Mapsshould be faster.Now, to make
Maps more type-safe, How about storing one map per your data type?This way you don’t have to type-check extracted values, but you have to lookup the right map.
Returning to the record-based solution, one can improve performance by splitting the large data structure in several parts, like a tree:
You can even do this automatically and generate some nice accessor functions with the help of Template Haskell.