For the benefit of others, the definition of `draw10` would look like this:

    draw10 = zoom (splitAt 10) drawAll

I think a way to simplify this further and remove the intermediate list would be to define a function of type:

    foldAll :: (Monad m) => L.Fold a b -> Parser a m b

Then you could simplify this further to:

    loop = do
        (avgTemp, avgHumidity) <- zoom (splitAt 10) (foldAll averages)
        ...

This would also improve memory usage because you wouldn't need to bring the intermediate list into memory.

I just added this to the `lenses` branch of `pipes-parse` as `foldAll` and `foldMAll`, so give that a try and see if that works for you. These don't use the `Fold` type but you can unpack any `Fold`'s arguments and pass it to these. Here's some example usage:

    import Control.Monad.Trans.State.Strict
    import Lens.Family.State.Strict
    import Pipes
    import Pipes.Parse
    import Prelude hiding (splitAt)

    sum10 :: (Monad m) => Parser Double m Double
    sum10 = zoom (splitAt 10) (foldAll (+) 0 id)

    main = print $ evalState sum10 (each [1..])
    -- prints 55

On 12/19/2013 12:24 PM, Pierre R wrote:
This is actually getting easier with the new API:
        loop = do
            records <- draw10
            let (avgTemp, avgHumidity) = L.fold averages records
liftIO $ printf "-- Report: average temperature is %d°C, average humidity is %d%% \n" avgTemp avgHumidity
            eof <- PP.isEndOfInput
            unless eof loop

Cheers,

On Wednesday, December 18, 2013 9:29:32 PM UTC+1, Pierre R wrote:

    Hi,

    I am having a go with the new parsing API (just for learning
    purposes). How should I convert the following code ?

    (avgTemp,  avgHum)  <-  fold'  averages  (input  >->  P.take  10)


    https://github.com/PierreR/pipes-zmq3/blob/master/sub.hs#L66
    <https://github.com/PierreR/pipes-zmq3/blob/master/sub.hs#L66>

    Do I replace (input >-> P.take 10) with zoom (splitAt 10) drawAll
    ? Then how do I use the parser with Foldl ?

    As a note, it seems to me that I don't need to check for stream
    termination with zeromq because I always get one message or
    nothing at all. The haskell zeromq lib takes care of dealing with
    error conditions already.
    Is that right ?

    Thanks for your help,

    Cheers

--
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