on master help sign in
Make streamBuild/streamFold accept an SVar too.
e7d60a8Harendra Kumar
src/Asyncly/Streams.hs+19−12
@@ -735,17 +735,23 @@ zippingAsync x = x
735-- Constructing a stream
735-- Constructing a stream
736------------------------------------------------------------------------------
736------------------------------------------------------------------------------
737
737
738−-- XXX Need to accept an SVar as well
739-- | Build a stream from its church encoding. The function passed maps
738-- | Build a stream from its church encoding. The function passed maps
740−-- directly to the underlying representation of the stream type.
739+-- directly to the underlying representation of the stream type. The second
740+-- parameter to the function is the "yield" function yielding a value and the
741+-- remaining stream if any otherwise 'Nothing'. The third parameter is to
742+-- represent an "empty" stream.
741streamBuild :: Streaming t
743streamBuild :: Streaming t
744+ => (forall r. Maybe (SVar m a)
742− => (forall r. (a -> Maybe (t m a) -> m r) -> m r -> m r) -> t m a
745+ -> (a -> Maybe (t m a) -> m r)
746+ -> m r
747+ -> m r)
748+ -> t m a
743−streamBuild k = fromStream $ Stream $ \_ stp yld ->
749+streamBuild k = fromStream $ Stream $ \sv stp yld ->
744 let yield a Nothing = yld a Nothing
750 let yield a Nothing = yld a Nothing
745 yield a (Just r) = yld a (Just (toStream r))
751 yield a (Just r) = yld a (Just (toStream r))
746− in k yield stp
752+ in k sv yield stp
747
753
748−-- | Convert a callback into a stream.
754+-- | Build a singleton stream from a callback function.
749fromCallback :: (Streaming t) => (forall r. (a -> m r) -> m r) -> t m a
755fromCallback :: (Streaming t) => (forall r. (a -> m r) -> m r) -> t m a
750fromCallback k = fromStream $ Stream $ \_ _ yld -> k (\a -> yld a Nothing)
756fromCallback k = fromStream $ Stream $ \_ _ yld -> k (\a -> yld a Nothing)
751
757
@@ -758,14 +764,15 @@ fromSVar sv = fromStream $ streamSVar sv
758-- Destroying a stream
764-- Destroying a stream
759------------------------------------------------------------------------------
765------------------------------------------------------------------------------
760
766
761−-- | Fold a stream using its church encoding. The first argument is the
767+-- | Fold a stream using its church encoding. The second argument is the "step"
762−-- "step" function consuming one element and the rest of the stream. The second
768+-- function consuming an element and the remaining stream, if any. The third
763−-- argument is the "done" function that is called when the stream is over.
769+-- argument is for consuming an "empty" stream that yields nothing.
764−streamFold :: Streaming t => (a -> Maybe (t m a) -> m r) -> m r -> t m a -> m r
770+streamFold :: Streaming t
771+ => Maybe (SVar m a) -> (a -> Maybe (t m a) -> m r) -> m r -> t m a -> m r
765−streamFold step done m =
772+streamFold sv step blank m =
766 let yield a Nothing = step a Nothing
773 let yield a Nothing = step a Nothing
767 yield a (Just x) = step a (Just (fromStream x))
774 yield a (Just x) = step a (Just (fromStream x))
768− in (runStream (toStream m)) Nothing done yield
775+ in (runStream (toStream m)) sv blank yield
769
776
770-- | Run a streaming composition, discard the results.
777-- | Run a streaming composition, discard the results.
771runStreaming :: (Monad m, Streaming t) => t m a -> m ()
778runStreaming :: (Monad m, Streaming t) => t m a -> m ()