@@ -172,6 +172,18 @@ import qualified Streamly.Streams.StreamK as K
172172import qualified Streamly.Streams.StreamD as D
173173import qualified Streamly.Streams.Serial as Serial
174174
175+------------------------------------------------------------------------------
176+-- Conversion to and from direct style stream
177+------------------------------------------------------------------------------
178+
179+{-# INLINE fromStreamD #-}
180+fromStreamD :: (IsStream t, Monad m) => D.Stream m a -> t m a
181+fromStreamD = fromStream . D.toStreamK
182+
183+{-# INLINE toStreamD #-}
184+toStreamD :: (IsStream t, Monad m) => t m a -> D.Stream m a
185+toStreamD = D.fromStreamK . toStream
186+
175187------------------------------------------------------------------------------
176188-- Deconstruction
177189------------------------------------------------------------------------------
@@ -211,7 +223,7 @@ uncons m =
211223-- @since 0.1.0
212224{-# INLINE_EARLY unfoldr #-}
213225unfoldr :: (Monad m, IsStream t) => (b -> Maybe (a, b)) -> b -> t m a
214−unfoldr step seed = fromStream $ D.toStreamK (D.unfoldr step seed)
226+unfoldr step seed = fromStreamD (D.unfoldr step seed)
215227{-# RULES "unfoldr fallback to StreamK" [1]
216228 forall a b. D.toStreamK (D.unfoldr a b) = K.unfoldr a b #-}
217229
@@ -253,7 +265,7 @@ unfoldrM = K.unfoldrM
253265{-# RULES "unfoldrM serial" unfoldrM = unfoldrMSerial #-}
254266{-# INLINE_EARLY unfoldrMSerial #-}
255267unfoldrMSerial :: MonadAsync m => (b -> m (Maybe (a, b))) -> b -> SerialT m a
256−unfoldrMSerial step seed = fromStream $ D.toStreamK (D.unfoldrM step seed)
268+unfoldrMSerial step seed = fromStreamD (D.unfoldrM step seed)
257269
258270------------------------------------------------------------------------------
259271-- Specialized Generation
@@ -333,7 +345,7 @@ iterateM step = go
333345-- @since 0.4.0
334346{-# INLINE_EARLY fromList #-}
335347fromList :: (Monad m, IsStream t) => [a] -> t m a
336−fromList = fromStream . D.toStreamK . D.fromList
348+fromList = fromStreamD . D.fromList
337349{-# RULES "fromList fallback to StreamK" [1]
338350 forall a. D.toStreamK (D.fromList a) = K.fromFoldable a #-}
339351
@@ -344,7 +356,7 @@ fromList = fromStream . D.toStreamK . D.fromList
344356-- @since 0.4.0
345357{-# INLINE_EARLY fromListM #-}
346358fromListM :: (MonadAsync m, IsStream t) => [m a] -> t m a
347−fromListM = fromStream . D.toStreamK . D.fromListM
359+fromListM = fromStreamD . D.fromListM
348360{-# RULES "fromListM fallback to StreamK" [1]
349361 forall a. D.toStreamK (D.fromListM a) = fromFoldableM a #-}
350362
@@ -388,6 +400,19 @@ fromHandle h = fromStream go
388400-- Elimination by Folding
389401------------------------------------------------------------------------------
390402
403+-- | Lazy right fold with a monadic step function. For example, to fold a
404+-- stream into a list:
405+--
406+-- @
407+-- >> runIdentity $ foldrM (\\x xs -> return (x : xs)) [] (serially $ fromFoldable [1,2,3])
408+-- [1,2,3]
409+-- @
410+--
411+-- @since 0.2.0
412+{-# INLINE foldrM #-}
413+foldrM :: Monad m => (a -> b -> m b) -> b -> SerialT m a -> m b
414+foldrM step acc m = D.foldrM step acc $ toStreamD m
415+
391416-- | Lazy right associative fold. For example, to fold a stream into a list:
392417--
393418-- @
@@ -402,19 +427,6 @@ foldr :: Monad m => (a -> b -> b) -> b -> SerialT m a -> m b
402427-- foldr step acc m = D.foldr step acc $ D.fromStreamK (toStream m)
403428foldr f = foldrM (\a b -> return (f a b))
404429
405−-- | Lazy right fold with a monadic step function. For example, to fold a
406−-- stream into a list:
407−--
408−-- @
409−-- >> runIdentity $ foldrM (\\x xs -> return (x : xs)) [] (serially $ fromFoldable [1,2,3])
410−-- [1,2,3]
411−-- @
412−--
413−-- @since 0.2.0
414−{-# INLINE foldrM #-}
415−foldrM :: Monad m => (a -> b -> m b) -> b -> SerialT m a -> m b
416−foldrM step acc m = D.foldrM step acc $ D.fromStreamK (toStream m)
417−
418430-- | Strict left fold with an extraction function. Like the standard strict
419431-- left fold, but applies a user supplied extraction function (the third
420432-- argument) to the folded value at the end. This is designed to work with the
@@ -436,7 +448,7 @@ foldl = foldx
436448-- @since 0.2.0
437449{-# INLINE foldl' #-}
438450foldl' :: Monad m => (b -> a -> b) -> b -> SerialT m a -> m b
439−foldl' step begin m = D.foldl' step begin $ D.fromStreamK (toStream m)
451+foldl' step begin m = D.foldl' step begin $ toStreamD m
440452
441453-- XXX replace the recursive "go" with explicit continuations.
442454-- | Like 'foldx', but with a monadic step function.
@@ -455,7 +467,7 @@ foldlM = foldxM
455467--
456468-- @since 0.2.0
457469foldlM' :: Monad m => (b -> a -> m b) -> b -> SerialT m a -> m b
458−foldlM' step begin m = D.foldlM' step begin $ D.fromStreamK (toStream m)
470+foldlM' step begin m = D.foldlM' step begin $ toStreamD m
459471
460472------------------------------------------------------------------------------
461473-- Specialized folds
@@ -512,7 +524,7 @@ tail m =
512524-- @since 0.1.1
513525{-# INLINE last #-}
514526last :: Monad m => SerialT m a -> m (Maybe a)
515−last m = D.last $ D.fromStreamK (toStream m)
527+last m = D.last $ toStreamD m
516528
517529-- | Determine whether the stream is empty.
518530--
@@ -602,7 +614,7 @@ maximum m = go Nothing (toStream m)
602614-- @since 0.1.0
603615{-# INLINE mapM_ #-}
604616mapM_ :: Monad m => (a -> m b) -> SerialT m a -> m ()
605−mapM_ f m = D.mapM_ f $ D.fromStreamK (toStream m)
617+mapM_ f m = D.mapM_ f $ toStreamD m
606618
607619------------------------------------------------------------------------------
608620-- Conversions
@@ -613,7 +625,7 @@ mapM_ f m = D.mapM_ f $ D.fromStreamK (toStream m)
613625-- @since 0.1.0
614626{-# INLINE toList #-}
615627toList :: Monad m => SerialT m a -> m [a]
616−toList m = D.toList $ D.fromStreamK (toStream m)
628+toList m = D.toList $ toStreamD m
617629
618630-- | Write a stream of Strings to an IO Handle.
619631--
@@ -652,8 +664,7 @@ scan = scanx
652664-- @since 0.4.0
653665{-# INLINE scanlM' #-}
654666scanlM' :: (IsStream t, Monad m) => (b -> a -> m b) -> b -> t m a -> t m b
655−scanlM' step begin m = fromStream $ D.toStreamK $
656− D.scanlM' step begin $ D.fromStreamK (toStream m)
667+scanlM' step begin m = fromStreamD $ D.scanlM' step begin $ toStreamD m
657668
658669-- | Strict left scan. Like 'foldl'', but returns the folded value at each
659670-- step, generating a stream of all intermediate fold results. The first
@@ -676,7 +687,7 @@ scanl' step = scanlM' (\a b -> return (step a b))
676687#if __GLASGOW_HASKELL__ != 802
677688-- GHC 8.2.2 crashes with this code, when used with "stack"
678689filter :: (IsStream t, Monad m) => (a -> Bool) -> t m a -> t m a
679−filter p m = fromStream $ D.toStreamK $ D.filter p (D.fromStreamK $ toStream m)
690+filter p m = fromStreamD $ D.filter p $ toStreamD m
680691#else
681692filter :: IsStream t => (a -> Bool) -> t m a -> t m a
682693filter = K.filter
@@ -687,30 +698,28 @@ filter = K.filter
687698-- @since 0.1.0
688699{-# INLINE take #-}
689700take :: (IsStream t, Monad m) => Int -> t m a -> t m a
690−take n m = fromStream $ D.toStreamK $ D.take n (D.fromStreamK $ toStream m)
701+take n m = fromStreamD $ D.take n $ toStreamD m
691702
692703-- | End the stream as soon as the predicate fails on an element.
693704--
694705-- @since 0.1.0
695706{-# INLINE takeWhile #-}
696707takeWhile :: (IsStream t, Monad m) => (a -> Bool) -> t m a -> t m a
697−takeWhile p m = fromStream $ D.toStreamK $
698− D.takeWhile p (D.fromStreamK $ toStream m)
708+takeWhile p m = fromStreamD $ D.takeWhile p $ toStreamD m
699709
700710-- | Same as 'takeWhile' but with a monadic predicate.
701711--
702712-- @since 0.4.0
703713{-# INLINE takeWhileM #-}
704714takeWhileM :: (IsStream t, Monad m) => (a -> m Bool) -> t m a -> t m a
705−takeWhileM p m = fromStream $ D.toStreamK $
706− D.takeWhileM p (D.fromStreamK $ toStream m)
715+takeWhileM p m = fromStreamD $ D.takeWhileM p $ toStreamD m
707716
708717-- | Discard first 'n' elements from the stream and take the rest.
709718--
710719-- @since 0.1.0
711720{-# INLINE drop #-}
712721drop :: (IsStream t, Monad m) => Int -> t m a -> t m a
713−drop n m = fromStream $ D.toStreamK $ D.drop n (D.fromStreamK $ toStream m)
722+drop n m = fromStreamD $ D.drop n $ toStreamD m
714723
715724-- | Drop elements in the stream as long as the predicate succeeds and then
716725-- take the rest of the stream.
@@ -718,16 +727,14 @@ drop n m = fromStream $ D.toStreamK $ D.drop n (D.fromStreamK $ toStream m)
718727-- @since 0.1.0
719728{-# INLINE dropWhile #-}
720729dropWhile :: (IsStream t, Monad m) => (a -> Bool) -> t m a -> t m a
721−dropWhile p m = fromStream $ D.toStreamK $
722− D.dropWhile p (D.fromStreamK $ toStream m)
730+dropWhile p m = fromStreamD $ D.dropWhile p $ toStreamD m
723731
724732-- | Same as 'dropWhile' but with a monadic predicate.
725733--
726734-- @since 0.4.0
727735{-# INLINE dropWhileM #-}
728736dropWhileM :: (IsStream t, Monad m) => (a -> m Bool) -> t m a -> t m a
729−dropWhileM p m = fromStream $ D.toStreamK $
730− D.dropWhileM p (D.fromStreamK $ toStream m)
737+dropWhileM p m = fromStreamD $ D.dropWhileM p $ toStreamD m
731738
732739------------------------------------------------------------------------------
733740-- Transformation by Mapping
@@ -783,8 +790,8 @@ sequence = K.sequence
783790--
784791-- @since 0.3.0
785792{-# INLINE mapMaybe #-}
786−mapMaybe :: IsStream t => (a -> Maybe b) -> t m a -> t m b
787−mapMaybe = K.mapMaybe
793+mapMaybe :: (IsStream t, Monad m) => (a -> Maybe b) -> t m a -> t m b
794+mapMaybe f m = fromStreamD $ D.mapMaybe f $ toStreamD m
788795
789796-- | Like 'mapMaybe' but maps a monadic function.
790797--