How does one make their own streaming code? I was generating about 1,000,000,000 random pairs of war decks, and I wanted them to be lazy streamed into a foldl', but I got a space leak! Here is the relevant section of code:
main = do
games <- replicateM 1000000000 $ deal <$> sDeck --Would be a trillion, but Int only goes so high
let res = experiment Ace games --experiment is a foldl'
print res --res is tiny
When I run it with -O2, it first starts freezing up my computer, and then the program dies and the computer comes back to life (and Google Chrome then has the resources it needs to yell at me for using up all its resources.)
Note: I tried unsafeInterleaveIO, and it didn't work.
Full code is at: http://lpaste.net/109977
replicateM
doesn't do lazy streaming. If you need to stream results from monadic actions, you should use a library such asconduit
orpipes
.Your example code could be written to support streaming with conduits like this:
The
Data.Conduit.Combinators
module is from theconduit-combinators
package.As a quick-and-dirty solution you could implement a streaming version of
replicateM
using lazy IO.But I recommend using a proper streaming library.