1

I am confused by the lazy concept.

I am new to Haskell and I met the httpLBS function in the Network.HTTP.Simple module, which says:

Perform an HTTP request and return the body as a lazy ByteString. Note that the entire value will be read into memory at once (no lazy I/O will be performed). The advantage of a lazy ByteString here (versus using httpBS) is--if needed--a better in-memory representation.

What is the value? Is it the entire response? If the entire response is in memory then what is the point of a lazy byte string here? How does this work? What is the difference between the lazy IO and lazy byte string?

pepe
  • 814
  • 8
  • 12
  • One of the advantages I can come up with, from "a better in-memory representation," is that a lazy bytestring doesn't need large contiguous memory even when a response is large. – snak Jan 24 '23 at 12:25
  • @snak thank you. What if I download a 1GB file with `httpLBS`. Will that file be stored entirely in the memory? And I can process that file chunk by chunk? – pepe Jan 24 '23 at 12:35
  • Yes. As the doc says, it doesn't use lazy I/O, so the entire file will be stored in memory. You can process it with any functions taking a lazy bytestring. Usually, you don't need to take care of chunks by yourself because those functions take care of it, but you can still do it if you want. – snak Jan 24 '23 at 13:15
  • @snak That makes sense. I assume there must be another function somewhere else that can download big files using lazy IO. Thank you for your replies. – pepe Jan 24 '23 at 14:45
  • Actually, many, not all people think lazy I/O isn't a very good idea. Even though they're convenient, it's hard to see when the I/O is performed, it can easily lead to resource leaks and so on. You might want to combine httpSink with sinkFile (https://hackage.haskell.org/package/conduit-1.3.4.3/docs/Conduit.html#v:sinkFile), for example, to consume a response in a streaming manner. This answer (https://stackoverflow.com/a/66837197/1378868) might help as well. – snak Jan 24 '23 at 15:06

0 Answers0