turtle icon indicating copy to clipboard operation
turtle copied to clipboard

Added toLines.

Open GregorySchwartz opened this issue 5 years ago • 5 comments

Resolves #354. Probably needs a better name, should avoid partial functions, and needs verification if bang patterns are needed, especially on stream.

GregorySchwartz avatar Sep 12 '19 14:09 GregorySchwartz

@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

Gabriella439 avatar Sep 13 '19 04:09 Gabriella439

For educational purposes, is the constant space due to the strict pair?

GregorySchwartz avatar Sep 13 '19 12:09 GregorySchwartz

@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 Lines inside of the accumulator as a Shell Line.

Passing completed Lines to the inner step function immediately instead of holding onto them ensures that even if there are a large (or infinite) number of output Lines then the toLines function still makes progress and doesn't wait until computing all of the Lines to begin forwarding them downstream.

Gabriella439 avatar Sep 13 '19 15:09 Gabriella439

Ah, I never thought of building a Shell outside of fold or reduce, so that makes sense.

GregorySchwartz avatar Sep 13 '19 15:09 GregorySchwartz

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

Gabriella439 avatar Sep 13 '19 15:09 Gabriella439