Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 parsing 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.

like image 377
me2 Avatar asked Feb 03 '23 05:02

me2


1 Answers

Yes.

length . take x.

like image 146
ephemient Avatar answered Feb 16 '23 03:02

ephemient