turtle
turtle copied to clipboard
Added toLines.
Resolves #354. Probably needs a better name, should avoid partial functions, and needs verification if bang patterns are needed, especially on stream
.
@GregorySchwartz: If you don't mind, I put up an alternative fix here: https://github.com/Gabriel439/Haskell-Turtle-Library/pull/356
The main reason for doing it the other way was to ensure that toLines
can handle infinite streams and operate in constant space when possible
For educational purposes, is the constant space due to the strict pair?
@GregorySchwartz: The main difference is that in the streaming version the outer step'
function immediately passes each complete Line
to the inner step
function here:
-- Emit each complete `Line`
x' <- foldM step x (NonEmpty.init lines')
... before moving on to the next Text
chunk. In particular, the streaming version does not store the complete sequence of Line
s inside of the accumulator as a Shell Line
.
Passing completed Line
s to the inner step
function immediately instead of holding onto them ensures that even if there are a large (or infinite) number of output Line
s then the toLine
s function still makes progress and doesn't wait until computing all of the Line
s to begin forwarding them downstream.
Ah, I never thought of building a Shell
outside of fold
or reduce
, so that makes sense.
I'll go ahead and merge #356, then. However, I'd still like you to review #357 if you can to see if it addresses your use case before merging that one