@@ -677,7 +677,16 @@ foldl' fstep begin = foldlM' (\b a -> return (fstep b a)) (return begin)
677-- Special folds
677-- Special folds
678------------------------------------------------------------------------------
678------------------------------------------------------------------------------
680−-- | Run a streaming composition, discard the results.
680+-- >>> drain = mapM_ (\_ -> return ())
685+-- >>> drain = Stream.fold Fold.drain
686+-- >>> drain = Stream.foldrM (\_ xs -> xs) (return ())
688+-- Run a stream, discarding the results.
681{-# INLINE_LATE drain #-}
690{-# INLINE_LATE drain #-}
682drain :: Monad m => Stream m a -> m ()
691drain :: Monad m => Stream m a -> m ()
683-- drain = foldrM (\_ xs -> xs) (return ())
692-- drain = foldrM (\_ xs -> xs) (return ())
@@ -694,6 +703,45 @@ drain (Stream step state) = go SPEC state
694-- To Containers
703-- To Containers
695------------------------------------------------------------------------------
704------------------------------------------------------------------------------
706+-- This toList impl is faster (30% on streaming-benchmarks) than the
707+-- corresponding left fold. The left fold retains an additional argument in the
710+-- Core for the right fold loop:
713+-- = \ sc_s3e6 sc1_s3e5 ->
714+-- case ># sc1_s3e5 100000# of {
716+-- case main_$s$wgo1 sc_s3e6 (+# sc1_s3e5 1#) of
718+-- Core for the left fold loop:
721+-- = \ sc_s3oT sc1_s3oS sc2_s3oR ->
722+-- case sc2_s3oR of fs2_a2lw { __DEFAULT ->
723+-- case ># sc1_s3oS 100000# of {
725+-- let { wild_a2og = I# sc1_s3oS } in
727+-- sc_s3oT (+# sc1_s3oS 1#) (\ x_X9 -> fs2_a2lw (: wild_a2og x_X9));
732+-- >>> toList = Stream.foldr (:) []
733+-- >>> toList = Stream.fold Fold.toList
735+-- Convert a stream into a list in the underlying monad. The list can be
736+-- consumed lazily in a lazy monad (e.g. 'Identity'). In a strict monad (e.g.
737+-- IO) the whole list is generated and buffered before it can be consumed.
739+-- /Warning!/ working on large lists accumulated as buffers in memory could be
740+-- very inefficient, consider using "Streamly.Data.Array" instead.
742+-- Note that this could a bit more efficient compared to @Stream.fold
743+-- Fold.toList@, and it can fuse with pure list consumers.
697{-# INLINE_NORMAL toList #-}
745{-# INLINE_NORMAL toList #-}
698toList :: Monad m => Stream m a -> m [a]
746toList :: Monad m => Stream m a -> m [a]
699toList = Streamly.Internal.Data.Stream.StreamD.Type.foldr (:) []
747toList = Streamly.Internal.Data.Stream.StreamD.Type.foldr (:) []