Ah yes, Ben's idea of just adding the logging to the producer itself via a pipe, rather than adding it to the fold over the producer, fits with the goal of preserving the purity of the work already done. I was taking that as part of the desideratum, but obviously adding a suitable pipe only adds impurity where we are already committed to it, and keeps things within the system of concepts already being used.
On Friday, October 2, 2015 at 10:44:33 AM UTC-4, Gabriel Gonzalez wrote: > > Actually, I like Ben's solution better than my own. Just print each > element going into the fold before you fold it. My only contribution > here will be to point out that the `notify` function already exists as > `Pipes.Prelude.chain`, so you could just write: > > foldBlocks initialBlockState (yourProducer >-> Pipes.Prelude.chain > (liftIO . print)) > > On 10/01/2015 08:17 AM, Ben Gamari wrote: > > Rune Kjær Svendsen <rune...@gmail.com <javascript:>> writes: > > > >> How would I go about printing log messages, while folding Blocks into > the > >> BlockState? The fold may take tens of minutes to complete, so printing > >> information about the processing would be very useful. > >> > >> Printing a message every time a block is read is simple, because > reading a > >> block from the disk already happens in the IO monad. The fold "step" > >> function is pure, so producing a log file requires using something > other > >> than Pipes.Prelude.fold, as far as I can see. > >> > >> Is there a better alternative? > >> > > You needn't touch your consumer. Instead just compose your producer with > > a something that runs an action on every downstream value, > > > > notify :: (a -> m ()) -> Pipe a a m r > > notify action = forever $ do > > x <- await > > action x > > yield x > > > > producer :: Producer a m () > > producer = undefined > > > > statusProducer :: MonadIO m => Producer a m () > > statusProducer = producer >-> notify (\_ -> liftIO $ putStr ".") > > > > Cheers, > > > > - Ben > > -- 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 haskell-pipes+unsubscr...@googlegroups.com. To post to this group, send email to haskell-pipes@googlegroups.com.