You can use `Data.Sequence` to create a FIFO queue. At each step of the
loop you enqueue an element to the back and dequeue an element from the
front:
import qualified Data.Sequence as S
import Data.Sequence (ViewR(EmptyR, (:>)), (<|))
import Pipes
foo :: Monad m => Pipe Int (Maybe Int, Int) m r
foo = loop (S.replicate 3 Nothing)
where
loop s = do
n <- await
let (mn, s') = case S.viewr s of
EmptyR -> (Nothing, s )
s' :> mn -> (mn , s')
yield (mn, n)
loop (Just n <| s')
You still will have O(N) space overhead, but now you no longer have to
batch steps N at a time and you can instead emit all elements immediately.
On 3/11/2014 8:51 AM, Daniel Mlot wrote:
I am trying my hand at a pipeline which uses delayed effect result
values to process incoming values. For instance, if I set a delay of
three steps, the result obtained in the first step will be used for
processing the fourth incoming value, the fifth result will be used
with the second input, and so forth. For now I managed to make it work
by chunking result values with mapM. Below is a minimal example of the
sort of wiring I aim for:
foo :: Pipe Int (Maybe Int, Int) IO ()
foo = do
let delays = replicate 3 Nothing
runWindow = lowerCodensity . mapM (\y -> lift $
await >>= \x -> yield (y, x) >> return (Just x))
iterateM_ runWindow delays
where
iterateM_ f x = f x >>= iterateM_ f
>>> runEffect $ each [1..6] >-> foo >-> P.print
(Nothing,1)
(Nothing,2)
(Nothing,3)
(Just 1,4)
(Just 2,5)
(Just 3,6)
This seems to do the job - effect sequencing and downsteaming are
fine, and so is memory consumption (linear in the delay size seems
hard to avoid). But is there a way more idiomatic than resorting to mapM?
--
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]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[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].