Hi,
I'm working through Simon Marlows Parallel and Concurrent
Programming in Haskell.
In Chapter 4, p. 69, or
http://chimera.labs.oreilly.com/books/1230000000929/ch04.html#_rate_limiting_the_producer
there is the task to extend a stream processing module based on
Control.Monad.Par in such a way that the producer is rate-limited,
i.e. the producer should not produce more than the consumer can
consume.
The book suggests the following type:
data IList a
= Nil
| Cons a (IVar (IList a))
| Fork (Par ()) (IList a)
I struggle to construct the functions for this type, as I don't know
what to do with the IList in the Fork case. It seems more natural to
me, to have an IVar (IList a) instead, whose value is provided by
the computation in Par () in the following way:
Fork (Par ()) (IVar (IList a))
streamFromListLimited :: NFData a => Int -> Int -> [a]
-> Par (Stream a)
streamFromListLimited f d xs = do
var <- new
fork $ loop f xs var
return var
where
loop :: NFData a => Int -> [a] -> Stream a -> Par ()
loop _ [] var = put var Nil
loop 0 (x:xs) var = do
tail <- new
put var (Fork (loop d xs tail) tail)
loop n (x:xs) var = do
tail <- new
put var (Cons x tail)
loop (n-1) xs tail
Can someone give me a hint?
Thanks,
Kilian