`folds` is folding each `FreeT` layer individually, and each individually
folded layer is `yield`ed as a separate folded result.  So in your example
you are folding each line and not folding the entire file.

Printing only the last line cannot be done in constant space because you
can't know in advance which line is the last line.  That means that you
must load each line entirely into memory no matter what you do, so the only
solution is something like this:

      Pipes.Prelude.last Pipes.stdinLn
On May 12, 2014 7:40 AM, "Andrew Browne" <[email protected]> wrote:

> Hi All,
>
> I was trying to use folds to return the last line of a text file but I
> can't to get the behaviour I want. Specifically it seems to run
> my fold multiple times instead of once over the whole file.
>
> lastline :: IO ()
> lastline = runEffect $ (myFold . view T.lines) T.stdin >-> T.stdout
>
> myFold :: Monad m => FreeT (Producer Text m) m () -> Producer Text m ()
> myFold = folds acc "" extract
> acc :: Show a => t -> a -> a
> acc _ a = trace ("acc " ++ show a) a
> extract :: Show a => a -> a
> extract a = trace ("extract " ++ show a) a
>
> When running it with a text file containing
> 1
> 2
> 3
>
> The result I get is:
> acc "1"
> extract "1"
> acc "2"
> extract "2"
> acc "3"
> extract "3"
> 123
>
> When what I expected was:
> acc "1"
> acc "2"
> acc "3"
> extract "3"
> 3
>
> I'm sure I'm doing something silly and I'm guessing it's in this part:
> "myFold . view T.lines". If someone is able to point me in the right
> direction that would be greatly
> appreciated.
>
> cheers
> Andrew
>
> --
> You received this message because you are subscribed to the Google Groups
> "Haskell Pipes" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
>

-- 
You received this message because you are subscribed to the Google Groups 
"Haskell Pipes" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].

Reply via email to