Not enough strictness in toListM
GregorySchwartz opened this issue · comments
Using toListM when large files are used results in stack space overflows. When replaced with this (albeit horrible) definition,
P.fold go [] id
where
go acc [] = acc
go acc [x] = x : acc
there is no more stack space overflow. A fix would very much be appreciated as there is a lot of use to get out of toListM in my case!
Yeah, it's totally okay to use toListM
if you really need it.
Also, there's nothing ugly about what you wrote (there are slight errors, but I recognize this diff-list trick). I use this pattern all the time in my own code when building a list. In fact, what you wrote is basically equivalent to:
import qualified Control.Foldl (purely, list)
import qualified Pipes.Prelude (fold)
toListM = purely fold list
... since the list
fold from the foldl
library uses the same diff-list trick to assemble the intermediate list.
Could you contribute a pull request for this? You don't need to use foldl
, just use what you wrote.
I should mention, to do this trick I needed P.map (: []) beforehand.
Actually, you can just write:
toListM = Pipes.fold step begin done
where
step x a = x . (a:)
begin = id
done x = x []
This is the "diff-list" trick that I was referring to.
I'll mark this closed now that your pull request is merged