[Haskell-cafe] Array Binary IO & molecular simulation

Grigory Sarnitskiy sargrigory at ya.ru
Sun May 3 07:42:48 EDT 2009


To sum up here is the example that can write two arrays in one file and then read this two arrays back. To restore written data it just reads the file into bytestring, then splits the bytestring into equal parts. The parts are decoded. I suppose the method is suitable for decoding files with unboxed arrays of equal size.

import Data.Array.Unboxed
import Data.Binary
import qualified Data.ByteString.Lazy as BL
import IO

a = listArray ((1,1),(3,2)) [3,4,5,6,7,8] :: UArray (Int, Int) Float
b = listArray ((1,1),(3,2)) [9,10,11,12,13,14] :: UArray (Int, Int) Float

encodeFile2 f = BL.appendFile f . encode

encoder = do
    encodeFile "Results.txt" a
    encodeFile2 "Results.txt" b

decoder = do
    contents <- BL.readFile "Results.txt"
    print $ (show (decode (fst (BL.splitAt 118 contents)) :: UArray (Int, Int) Float))
    print $ (show (decode (snd (BL.splitAt 118 contents)) :: UArray (Int, Int) Float))


More information about the Haskell-Cafe mailing list