I still don’t get how bytestrings work
import qualified Data.ByteString.Lazy as BS
let x = BS.readFile "somefile.txt" --some large file
let z = ((reverse (BS.unpack x)) !! 2) --do stuff here
I know bytestrings can be used to read large amounts of data ,very quickly and efficiently. But unpacking a packing doesn’t make sense.
let z = readArray x 1 --can you read the bytestring like its a array?(something like this)
Can’t you just read the data in bytestring form without unpacking? or just unpack a segment of the data?
Could you explain how it all works?(Code examples)
Well, it’s certainly wasteful.
Do you mean operate on the data without converting it to another form? Sure you can. Exactly how depends on what you want to do. I’ve used FFI (and later, Data.Vector.Storable) to access the ByteString as a set of
Word32‘s. You can pull out any individualWord8naturally. I’m sure you’ve seen ByteString’s Haddock documents, but know that other packages consume bytestrings directly (ex: for communicating an image buffer with C code that is called via FFI).Do you mean “operate on the data without using
[Word8]or[Char]“? The binary, cereal, and other packages can be used to parse bytestrings into arbitrary types.Sure: