Reputation: 1320
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)
Upvotes: 1
Views: 2513
Reputation: 64740
But packing a unpacking doesn't [make] sense.
Well, it's certainly wasteful.
Can't you just read the data in bytestring form without unpacking?
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 individual Word8
naturally. 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.
or just unpack a segment of the data?
Sure:
import Data.ByteString as B
getPortion n m = B.unpack . B.take n . B.drop m
Upvotes: 8