@@ -32,6 +32,8 @@ module Streamly.Streams.Serial
32 , InterleavedT -- deprecated
32 , InterleavedT -- deprecated
35 , (<=>) -- deprecated
37 , (<=>) -- deprecated
36 , wSerially
38 , wSerially
37 , interleaving -- deprecated
39 , interleaving -- deprecated
@@ -295,6 +297,8 @@ instance IsStream WSerialT where
296-- | Polymorphic version of the 'Semigroup' operation '<>' of 'WSerialT'.
298-- | Polymorphic version of the 'Semigroup' operation '<>' of 'WSerialT'.
297-- Interleaves two streams, yielding one element from each stream alternately.
299-- Interleaves two streams, yielding one element from each stream alternately.
300+-- When one stream stops the rest of the other stream is used in the output
299-- @since 0.2.0
303-- @since 0.2.0
300{-# INLINE wSerial #-}
304{-# INLINE wSerial #-}
@@ -305,6 +309,35 @@ wSerial m1 m2 = mkStream $ \st yld sng stp -> do
305 yieldk a r = yld a (wSerial m2 r)
309 yieldk a r = yld a (wSerial m2 r)
306 foldStream st yieldk single stop m1
310 foldStream st yieldk single stop m1
312+-- | Like `wSerial` but stops interleaving as soon as the first stream stops.
315+{-# INLINE wSerialEndByFirst #-}
316+wSerialEndByFirst :: IsStream t => t m a -> t m a -> t m a
317+wSerialEndByFirst m1 m2 = mkStream $ \st yld sng stp -> do
318+ let yieldFirst a r = yld a (yieldSecond r m2)
319+ in foldStream st yieldFirst sng stp m1
323+ yieldSecond s1 s2 = mkStream $ \st yld sng stp -> do
324+ let stop = foldStream st yld sng stp s1
326+ yieldk a r = yld a (wSerial s1 r)
327+ in foldStream st yieldk single stop s2
329+-- | Like `wSerial` but stops interleaving as soon as any of the two streams
333+{-# INLINE wSerialEndByAny #-}
334+wSerialEndByAny :: IsStream t => t m a -> t m a -> t m a
335+wSerialEndByAny m1 m2 = mkStream $ \st yld sng stp -> do
338+ yieldk a r = yld a (wSerial m2 r)
339+ foldStream st yieldk single stop m1
308instance Semigroup (WSerialT m a) where
341instance Semigroup (WSerialT m a) where
309 (<>) = wSerial
342 (<>) = wSerial