[Haskell-cafe] Saving a data structure to a file

Bulat Ziganshin bulat.ziganshin at gmail.com
Fri Apr 28 16:24:23 EDT 2006


Hello Neil,

Friday, April 28, 2006, 7:59:22 PM, you wrote:

> I have a program which needs to cache intermediate data structures to
> a file (finite, non-cyclic, relatively simple, around ~100Kb when
> shown). Is there any easy way to acheive this?

use NewBinary (darcs get http://www.n-heptane.com/nhlab/repos/NewBinary/)
or jhc's Binary module together with DrIFT to generate Binary
instances. these modules need minimal tweaks to implement Hugs compatibility

code generated by DrIFT will use strict get. if you want to get lazy
reading, you can replace get/put_ with lazyGet/lazyPut in appropriate
instances

in order to make it run faster, you should work with memory buffer
and read/write entire buffer from/to file:

writing:
  bh <- openBinMem 1 undefined
  lazyPut bh data
  ... put more data
  writeBinMem bh "filename"

reading:
  bh <- readBinMem "filename"
  data <- lazyGet bh
  .... get more data

-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell-Cafe mailing list