design - What data structure to pick for genericity but safety? -


say have long data structure definition

data = {   x1 :: string , x2 :: string ... , x50 :: string } 

now have 3 tasks:

  1. create draft instance of { x1 = "this x1", ... }
  2. create instance of other data structure
  3. create data instance instance of a

the 3 tasks involve tediuous copying of lables x1, ..., x50. better solution generic list

[   foo "x1" avalue1 , foo "x2" avalue2 ... ] 

because make traversal , creating draft easier (the list definition draft already). downside mapping other data structures , more dangerous, since lose static type checking.

does make sense? there generic safe solution?

edit: give better idea, it's mapping business data textual representation forms , letters. e.g.:

data taxdata = taxdata {   taxid :: string , income :: money , taxpayed :: money, , ismarried :: bool ... }   data taxforma = taxforma {   taxid :: text , ismarried :: text   ... } data taxformb = taxformb {   taxid :: text , taxpayedrounded :: text ... } 

those transformed stream of text, representing actual forms. if create form tax data in 1 pass , next year form field have moved, there e.g. stray "0.0" , not know belongs. that's intermediate datat strcuture for: makes easy create draft data.

so need map actual taxdata intermediate form data; need map form data actual form textual representation; need create draft intermediate form data. on 1 hand hate repeating data labels, on other hand gives me saftey, don't confuse label while mapping. there silver bullet?

deeply structured data idiomatically expressed in haskell nested, algebraic data types, have done. why? gives type structure , safety data, preventing functions putting data wrong format. further safety can gained newtyping of types, increase differences between data in each field.

however, large adts can unwieldy name , manipulate. common situation in compiler design specifying such large adt, example, , write code compiler tend use lot of generic programming tricks: syb, meta-programming, template haskell, generate boilerplate need.

so, in summary, i'd keep adt approach taking, @ using generics (e.g. syb or template haskell) generate of definitions , helper functions.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -