on master help sign in
Make streamBuild/streamFold accept an SVar too.
commite7d60a8b06fb80532086326b035d5f0c5d45bd08 authorHarendra Kumar <harendra.kumar@gmail.com> dateSat 28 Oct 2017, 14:36 (9 years ago) parent45aee37
1 file changed+19−12
src/Asyncly/Streams.hs+19−12
src/Asyncly/Streams.hs+19−12
@@ -735,17 +735,23 @@ zippingAsync x = x
735735-- Constructing a stream
736736------------------------------------------------------------------------------
737737
738−-- XXX Need to accept an SVar as well
739738-- | 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.
741743streamBuild :: Streaming t
742− => (forall r. (a -> Maybe (t m a) -> m r) -> m r -> m r) -> t m a
743−streamBuild k = fromStream $ Stream $ \_ stp yld ->
744+ => (forall r. Maybe (SVar m a)
745+ -> (a -> Maybe (t m a) -> m r)
746+ -> m r
747+ -> m r)
748+ -> t m a
749+streamBuild k = fromStream $ Stream $ \sv stp yld ->
744750 let yield a Nothing = yld a Nothing
745751 yield a (Just r) = yld a (Just (toStream r))
746− in k yield stp
752+ in k sv yield stp
747753
748−-- | Convert a callback into a stream.
754+-- | Build a singleton stream from a callback function.
749755fromCallback :: (Streaming t) => (forall r. (a -> m r) -> m r) -> t m a
750756fromCallback k = fromStream $ Stream $ \_ _ yld -> k (\a -> yld a Nothing)
751757
@@ -758,14 +764,15 @@ fromSVar sv = fromStream $ streamSVar sv
758764-- Destroying a stream
759765------------------------------------------------------------------------------
760766
761−-- | Fold a stream using its church encoding. The first argument is the
762−-- "step" function consuming one element and the rest of the stream. The second
763−-- argument is the "done" function that is called when the stream is over.
764−streamFold :: Streaming t => (a -> Maybe (t m a) -> m r) -> m r -> t m a -> m r
765−streamFold step done m =
767+-- | Fold a stream using its church encoding. The second argument is the "step"
768+-- function consuming an element and the remaining stream, if any. The third
769+-- argument is for consuming an "empty" stream that yields nothing.
770+streamFold :: Streaming t
771+ => Maybe (SVar m a) -> (a -> Maybe (t m a) -> m r) -> m r -> t m a -> m r
772+streamFold sv step blank m =
766773 let yield a Nothing = step a Nothing
767774 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
769776
770777-- | Run a streaming composition, discard the results.
771778runStreaming :: (Monad m, Streaming t) => t m a -> m ()