In Haskell, will calling length on a Lazy ByteString force the entire string into memory?
I am reading a large data stream using lazy bytestrings, and want to know if at least X more bytes is available while p开发者_如何学Pythonarsing it. That is, I want to know if the bytestring is at least X bytes long.
Will calling length
on it result in the entire stream getting loaded, hence defeating the purpose of using the lazy bytestring?
If yes, then the followup would be: How to tell if it has at least X bytes without loading the entire stream?
EDIT: Originally I asked in the context of reading files but understand that there are better ways to determine filesize. Te ultimate solution I need however should not depend on the lazy bytestring source.
Yes.
length . take x
.
Is there a reason you're not using hFileSize :: Handle -> IO Integer
for getting the length of the file?
EDIT: sorry. I guess I was thinking bytestrings were lists. There is no genericLength for bytestrings.
length
is strict because the type it returns Int
is strict. You can use genericLength
from Data.List and import a library that defines lazy Peano numbers and gives you a Num instance for them, e.g the numbers library:
That would let you express your function in the way you would like, but ephemient's answer is functionally the same, and doesn't require importing a new library.
I just did a blog post on the subject here, if that sounds like an approach you might be interested in:
http://coder.bsimmons.name/blog/2010/03/lazy-arithmetic-in-haskell/
It will have to iterate the entire string, but if you don't keep a reference to the entire lazy byte-string anywhere else, I believe it should be able to free the head of the string as it progresses towards its tail.
精彩评论