Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

haskell how to remove list usage from this code?

I am trying to learn Haskell and struggling with binary IO. This code is adapted from the code given in this answer to one of my earlier questions.

I am trying to read a lot of Word32 values and a few Word8 values (metadata) from a given file. Currently, I have written a program that just reads Word32 values from a file into an unboxed vector.

The trouble with this code is it uses list (via replicateM) while reading multiple Word32 values from a file. I want to get rid of the intermediate list (for efficiency reasons) and want to read the data directly in the vector from the file using ByteString.

Here is my complete working code.

import Data.Word
import qualified Data.ByteString.Lazy as BIN
import Data.Binary.Get
import Data.Binary.Put
import qualified Data.Vector.Unboxed as V
import Control.Monad
import System.IO

main = do
  h_in  <- openFile "dat.bin" ReadMode
  v <- readInVector 10 h_in 
  putStrLn (show v)
  hClose h_in

readInVector n h_in = do
 bin1 <- BIN.hGet h_in (n*100)
 let ints1 = runGet (replicateM n getWord32le) bin1
 let v = V.fromList (ints1) :: V.Vector Word32
 return v

How to do this? I wish a complete working code as answer, not just pointers to documentation on Haskell site which has a lot of text with almost no working examples.

like image 903
mntk123 Avatar asked Jul 14 '26 07:07

mntk123


1 Answers

The vector-package has its own replicateM function for vectors which you can use to directly initialize a vector without building an intermediate list.

readInVector n h_in = do
  bin1 <- BIN.hGet h_in (n*100)
  return $ runGet (V.replicateM n getWord32le) bin1
like image 55
shang Avatar answered Jul 17 '26 20:07

shang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!