@@ -147,7 +147,7 @@ import Streamly.Internal.Data.Stream.StreamD.Type
148-- | A stream that terminates without producing any output or side effect.
148-- | A stream that terminates without producing any output or side effect.
150−-- >>> Stream.fold Fold.toList Stream.nil
150+-- >>> Stream.toList Stream.nil
153{-# INLINE_NORMAL nil #-}
153{-# INLINE_NORMAL nil #-}
@@ -157,21 +157,22 @@ nil = Stream (\_ _ -> pure Stop) ()
157-- XXX implement in terms of consM?
157-- XXX implement in terms of consM?
158-- cons x = consM (return x)
158-- cons x = consM (return x)
160+-- | WARNING! You almost always need StreamK.'Streamly.Data.StreamK.cons'
160−-- | Fuse a pure value at the head of an existing stream::
163+-- Fuse a pure value at the head of an existing stream::
162-- >>> s = 1 `Stream.cons` Stream.fromList [2,3]
165-- >>> s = 1 `Stream.cons` Stream.fromList [2,3]
163−-- >>> Stream.fold Fold.toList s
166+-- >>> Stream.toList s
164-- [1,2,3]
167-- [1,2,3]
166−-- This function should not be used to dynamically construct a stream. If a
169+-- From an implementation perspective, StreamK.cons translates into a
167−-- stream is constructed by successive use of this function it would take
170+-- functional call whereas fused cons translates into a conditional branch
168−-- O(n^2) time to consume the stream.
171+-- (jump). However, the overhead of the function call in StreamK.cons only
169−--
172+-- occurs once, while the overhead of the conditional branch in fused cons is
170−-- This function should only be used to statically fuse an element with a
173+-- incurred for each subsequent element in the stream. As a result,
171−-- stream. Do not use this recursively or where it cannot be inlined.
174+-- StreamK.cons has a time complexity of O(n), while fused cons has a time
172−--
175+-- complexity of O(n^2), where @n@ represents the number of 'cons' used.
173−-- See "Streamly.Data.StreamK" for a 'cons' that can be used to
174−-- construct a stream recursively.
176-- Definition:
177-- Definition:
@@ -205,7 +206,7 @@ cons x (Stream step state) = Stream step1 Nothing
205-- if b > 2
206-- if b > 2
206-- then return Nothing
207-- then return Nothing
207-- else return (Just (b, b + 1))
208-- else return (Just (b, b + 1))
208−-- in Stream.fold Fold.toList $ Stream.unfoldrM f 0
209+-- in Stream.toList $ Stream.unfoldrM f 0
210-- [0,1,2]
211-- [0,1,2]
@@ -224,15 +225,7 @@ unfoldrM next = Stream step
224 Nothing -> Stop
225 Nothing -> Stop
231−-- Nothing -> Stream.nil
232−-- Just (a, b) -> a `Stream.cons` unfoldr step b
235−-- Build a stream by unfolding a /pure/ step function @step@ starting from a
228+-- | Build a stream by unfolding a /pure/ step function @step@ starting from a
236-- seed @s@. The step function returns the next element in the stream and the
229-- seed @s@. The step function returns the next element in the stream and the
237-- next seed value. When it is done it returns 'Nothing' and the stream ends.
230-- next seed value. When it is done it returns 'Nothing' and the stream ends.
238-- For example,
231-- For example,
@@ -242,7 +235,7 @@ unfoldrM next = Stream step
242-- if b > 2
235-- if b > 2
243-- then Nothing
236-- then Nothing
244-- else Just (b, b + 1)
237-- else Just (b, b + 1)
245−-- in Stream.fold Fold.toList $ Stream.unfoldr f 0
238+-- in Stream.toList $ Stream.unfoldr f 0
247-- [0,1,2]
240-- [0,1,2]
@@ -256,8 +249,6 @@ unfoldr f = unfoldrM (return . f)
258-- >>> repeatM = Stream.sequence . Stream.repeat
251-- >>> repeatM = Stream.sequence . Stream.repeat
259−-- >>> repeatM = fix . Stream.consM
260−-- >>> repeatM = cycle1 . Stream.fromEffect
262-- Generate a stream by repeatedly executing a monadic action forever.
253-- Generate a stream by repeatedly executing a monadic action forever.
@@ -420,10 +411,10 @@ enumerateFromThenToIntegralDn from next to = Stream step EnumInit
420-- first element is @from@, the second element is @then@ and the successive
411-- first element is @from@, the second element is @then@ and the successive
421-- elements are in increments of @then - from@ up to @to@.
412-- elements are in increments of @then - from@ up to @to@.
423−-- >>> Stream.fold Fold.toList $ Stream.enumerateFromThenToIntegral 0 2 6
414+-- >>> Stream.toList $ Stream.enumerateFromThenToIntegral 0 2 6
424-- [0,2,4,6]
415-- [0,2,4,6]
426−-- >>> Stream.fold Fold.toList $ Stream.enumerateFromThenToIntegral 0 (-2) (-6)
417+-- >>> Stream.toList $ Stream.enumerateFromThenToIntegral 0 (-2) (-6)
427-- [0,-2,-4,-6]
418-- [0,-2,-4,-6]
429{-# INLINE_NORMAL enumerateFromThenToIntegral #-}
420{-# INLINE_NORMAL enumerateFromThenToIntegral #-}
@@ -444,10 +435,10 @@ enumerateFromThenToIntegral from next to
444-- is @then@ and the successive elements are in increments of @then - from@.
435-- is @then@ and the successive elements are in increments of @then - from@.
445-- The stream is bounded by the size of the 'Integral' type.
436-- The stream is bounded by the size of the 'Integral' type.
447−-- >>> Stream.fold Fold.toList $ Stream.take 4 $ Stream.enumerateFromThenIntegral (0 :: Int) 2
438+-- >>> Stream.toList $ Stream.take 4 $ Stream.enumerateFromThenIntegral (0 :: Int) 2
448-- [0,2,4,6]
439-- [0,2,4,6]
450−-- >>> Stream.fold Fold.toList $ Stream.take 4 $ Stream.enumerateFromThenIntegral (0 :: Int) (-2)
441+-- >>> Stream.toList $ Stream.take 4 $ Stream.enumerateFromThenIntegral (0 :: Int) (-2)
451-- [0,-2,-4,-6]
442-- [0,-2,-4,-6]
453{-# INLINE_NORMAL enumerateFromThenIntegral #-}
444{-# INLINE_NORMAL enumerateFromThenIntegral #-}
@@ -471,10 +462,10 @@ enumerateFromThenIntegral from next =
471-- CAUTION: This function is not safe for finite integral types. It does not
462-- CAUTION: This function is not safe for finite integral types. It does not
472-- check for overflow, underflow or bounds.
463-- check for overflow, underflow or bounds.
474−-- >>> Stream.fold Fold.toList $ Stream.take 4 $ Stream.enumerateFromStepIntegral 0 2
465+-- >>> Stream.toList $ Stream.take 4 $ Stream.enumerateFromStepIntegral 0 2
475-- [0,2,4,6]
466-- [0,2,4,6]
477−-- >>> Stream.fold Fold.toList $ Stream.take 3 $ Stream.enumerateFromStepIntegral 0 (-2)
468+-- >>> Stream.toList $ Stream.take 3 $ Stream.enumerateFromStepIntegral 0 (-2)
478-- [0,-2,-4]
469-- [0,-2,-4]
480{-# INLINE_NORMAL enumerateFromStepIntegral #-}
471{-# INLINE_NORMAL enumerateFromStepIntegral #-}
@@ -495,7 +486,7 @@ enumerateFromStepIntegral from stride =
495-- element is @from@ and successive elements are in increments of @1@ up to
486-- element is @from@ and successive elements are in increments of @1@ up to
498−-- >>> Stream.fold Fold.toList $ Stream.enumerateFromToIntegral 0 4
489+-- >>> Stream.toList $ Stream.enumerateFromToIntegral 0 4
499-- [0,1,2,3,4]
490-- [0,1,2,3,4]
501{-# INLINE enumerateFromToIntegral #-}
492{-# INLINE enumerateFromToIntegral #-}
@@ -507,7 +498,7 @@ enumerateFromToIntegral from to =
507-- stream whose first element is @from@ and the successive elements are in
498-- stream whose first element is @from@ and the successive elements are in
508-- increments of @1@. The stream is bounded by the size of the 'Integral' type.
499-- increments of @1@. The stream is bounded by the size of the 'Integral' type.
510−-- >>> Stream.fold Fold.toList $ Stream.take 4 $ Stream.enumerateFromIntegral (0 :: Int)
501+-- >>> Stream.toList $ Stream.take 4 $ Stream.enumerateFromIntegral (0 :: Int)
511-- [0,1,2,3]
502-- [0,1,2,3]
513{-# INLINE enumerateFromIntegral #-}
504{-# INLINE enumerateFromIntegral #-}
@@ -534,7 +525,7 @@ enumerateFromIntegral from = enumerateFromToIntegral from maxBound
535-- This is the equivalent to 'enumFrom' for 'Fractional' types. For example:
526-- This is the equivalent to 'enumFrom' for 'Fractional' types. For example:
537−-- >>> Stream.fold Fold.toList $ Stream.take 4 $ Stream.enumerateFromFractional 1.1
528+-- >>> Stream.toList $ Stream.take 4 $ Stream.enumerateFromFractional 1.1
538-- [1.1,2.1,3.1,4.1]
529-- [1.1,2.1,3.1,4.1]
540{-# INLINE enumerateFromFractional #-}
531{-# INLINE enumerateFromFractional #-}
@@ -550,10 +541,10 @@ enumerateFromFractional = enumerateFromNum
550-- This is the equivalent of 'enumFromThen' for 'Fractional' types. For
541-- This is the equivalent of 'enumFromThen' for 'Fractional' types. For
551-- example:
542-- example:
553−-- >>> Stream.fold Fold.toList $ Stream.take 4 $ Stream.enumerateFromThenFractional 1.1 2.1
544+-- >>> Stream.toList $ Stream.take 4 $ Stream.enumerateFromThenFractional 1.1 2.1
554-- [1.1,2.1,3.1,4.1]
545-- [1.1,2.1,3.1,4.1]
556−-- >>> Stream.fold Fold.toList $ Stream.take 4 $ Stream.enumerateFromThenFractional 1.1 (-2.1)
547+-- >>> Stream.toList $ Stream.take 4 $ Stream.enumerateFromThenFractional 1.1 (-2.1)
557-- [1.1,-2.1,-5.300000000000001,-8.500000000000002]
548-- [1.1,-2.1,-5.300000000000001,-8.500000000000002]
559{-# INLINE enumerateFromThenFractional #-}
550{-# INLINE enumerateFromThenFractional #-}
@@ -570,10 +561,10 @@ enumerateFromThenFractional = enumerateFromThenNum
570-- This is the equivalent of 'enumFromTo' for 'Fractional' types. For
561-- This is the equivalent of 'enumFromTo' for 'Fractional' types. For
571-- example:
562-- example:
573−-- >>> Stream.fold Fold.toList $ Stream.enumerateFromToFractional 1.1 4
564+-- >>> Stream.toList $ Stream.enumerateFromToFractional 1.1 4
574-- [1.1,2.1,3.1,4.1]
565-- [1.1,2.1,3.1,4.1]
576−-- >>> Stream.fold Fold.toList $ Stream.enumerateFromToFractional 1.1 4.6
567+-- >>> Stream.toList $ Stream.enumerateFromToFractional 1.1 4.6
577-- [1.1,2.1,3.1,4.1,5.1]
568-- [1.1,2.1,3.1,4.1,5.1]
579-- Notice that the last element is equal to the specified @to@ value after
570-- Notice that the last element is equal to the specified @to@ value after
@@ -594,10 +585,10 @@ enumerateFromToFractional from to =
594-- This is the equivalent of 'enumFromThenTo' for 'Fractional' types. For
585-- This is the equivalent of 'enumFromThenTo' for 'Fractional' types. For
595-- example:
586-- example:
597−-- >>> Stream.fold Fold.toList $ Stream.enumerateFromThenToFractional 0.1 2 6
588+-- >>> Stream.toList $ Stream.enumerateFromThenToFractional 0.1 2 6
598-- [0.1,2.0,3.9,5.799999999999999]
589-- [0.1,2.0,3.9,5.799999999999999]
600−-- >>> Stream.fold Fold.toList $ Stream.enumerateFromThenToFractional 0.1 (-2) (-6)
591+-- >>> Stream.toList $ Stream.enumerateFromThenToFractional 0.1 (-2) (-6)
601-- [0.1,-2.0,-4.1000000000000005,-6.200000000000001]
592-- [0.1,-2.0,-4.1000000000000005,-6.200000000000001]
603{-# INLINE_NORMAL enumerateFromThenToFractional #-}
594{-# INLINE_NORMAL enumerateFromThenToFractional #-}
@@ -672,13 +663,13 @@ class Enum a => Enumerable a where
672 -- @from@, enumerating up to 'maxBound' when the type is 'Bounded' or
663 -- @from@, enumerating up to 'maxBound' when the type is 'Bounded' or
673 -- generating an infinite stream when the type is not 'Bounded'.
664 -- generating an infinite stream when the type is not 'Bounded'.
675− -- >>> Stream.fold Fold.toList $ Stream.take 4 $ Stream.enumerateFrom (0 :: Int)
666+ -- >>> Stream.toList $ Stream.take 4 $ Stream.enumerateFrom (0 :: Int)
676 -- [0,1,2,3]
667 -- [0,1,2,3]
678 -- For 'Fractional' types, enumeration is numerically stable. However, no
669 -- For 'Fractional' types, enumeration is numerically stable. However, no
679 -- overflow or underflow checks are performed.
670 -- overflow or underflow checks are performed.
681− -- >>> Stream.fold Fold.toList $ Stream.take 4 $ Stream.enumerateFrom 1.1
672+ -- >>> Stream.toList $ Stream.take 4 $ Stream.enumerateFrom 1.1
682 -- [1.1,2.1,3.1,4.1]
673 -- [1.1,2.1,3.1,4.1]
684 enumerateFrom :: (Monad m) => a -> Stream m a
675 enumerateFrom :: (Monad m) => a -> Stream m a
@@ -687,16 +678,16 @@ class Enum a => Enumerable a where
687 -- the type up to the value @to@. If @to@ is smaller than @from@ then an
678 -- the type up to the value @to@. If @to@ is smaller than @from@ then an
688 -- empty stream is returned.
679 -- empty stream is returned.
690− -- >>> Stream.fold Fold.toList $ Stream.enumerateFromTo 0 4
681+ -- >>> Stream.toList $ Stream.enumerateFromTo 0 4
691 -- [0,1,2,3,4]
682 -- [0,1,2,3,4]
693 -- For 'Fractional' types, the last element is equal to the specified @to@
684 -- For 'Fractional' types, the last element is equal to the specified @to@
694 -- value after rounding to the nearest integral value.
685 -- value after rounding to the nearest integral value.
696− -- >>> Stream.fold Fold.toList $ Stream.enumerateFromTo 1.1 4
687+ -- >>> Stream.toList $ Stream.enumerateFromTo 1.1 4
697 -- [1.1,2.1,3.1,4.1]
688 -- [1.1,2.1,3.1,4.1]
699− -- >>> Stream.fold Fold.toList $ Stream.enumerateFromTo 1.1 4.6
690+ -- >>> Stream.toList $ Stream.enumerateFromTo 1.1 4.6
700 -- [1.1,2.1,3.1,4.1,5.1]
691 -- [1.1,2.1,3.1,4.1,5.1]
702 enumerateFromTo :: (Monad m) => a -> a -> Stream m a
693 enumerateFromTo :: (Monad m) => a -> a -> Stream m a
@@ -708,10 +699,10 @@ class Enum a => Enumerable a where
708 -- 'Bounded' types the stream ends when 'maxBound' is reached, for
699 -- 'Bounded' types the stream ends when 'maxBound' is reached, for
709 -- unbounded types it keeps enumerating infinitely.
700 -- unbounded types it keeps enumerating infinitely.
711− -- >>> Stream.fold Fold.toList $ Stream.take 4 $ Stream.enumerateFromThen 0 2
702+ -- >>> Stream.toList $ Stream.take 4 $ Stream.enumerateFromThen 0 2
712 -- [0,2,4,6]
703 -- [0,2,4,6]
714− -- >>> Stream.fold Fold.toList $ Stream.take 4 $ Stream.enumerateFromThen 0 (-2)
705+ -- >>> Stream.toList $ Stream.take 4 $ Stream.enumerateFromThen 0 (-2)
715 -- [0,-2,-4,-6]
706 -- [0,-2,-4,-6]
717 enumerateFromThen :: (Monad m) => a -> a -> Stream m a
708 enumerateFromThen :: (Monad m) => a -> a -> Stream m a
@@ -722,10 +713,10 @@ class Enum a => Enumerable a where
722 -- occur downwards or upwards depending on whether @then@ comes before or
713 -- occur downwards or upwards depending on whether @then@ comes before or
723 -- after @from@.
714 -- after @from@.
725− -- >>> Stream.fold Fold.toList $ Stream.enumerateFromThenTo 0 2 6
716+ -- >>> Stream.toList $ Stream.enumerateFromThenTo 0 2 6
726 -- [0,2,4,6]
717 -- [0,2,4,6]
728− -- >>> Stream.fold Fold.toList $ Stream.enumerateFromThenTo 0 (-2) (-6)
719+ -- >>> Stream.toList $ Stream.enumerateFromThenTo 0 (-2) (-6)
729 -- [0,-2,-4,-6]
720 -- [0,-2,-4,-6]
731 enumerateFromThenTo :: (Monad m) => a -> a -> a -> Stream m a
722 enumerateFromThenTo :: (Monad m) => a -> a -> a -> Stream m a
@@ -1075,17 +1066,14 @@ generate n gen = generateM n (return . gen)
1075-- Iteration
1066-- Iteration
1076-------------------------------------------------------------------------------
1067-------------------------------------------------------------------------------
1079−-- >>> iterateM f m = m >>= \a -> return a `Stream.consM` iterateM f (f a)
1081−-- Generate an infinite stream with the first element generated by the action
1069+-- | Generate an infinite stream with the first element generated by the action
1082−-- @m@ and each successive element derived by applying the monadic function
1070+-- @m@ and each successive element derived by applying the monadic function @f@
1083−-- @f@ on the previous element.
1071+-- on the previous element.
1085-- >>> :{
1073-- >>> :{
1086-- Stream.iterateM (\x -> print x >> return (x + 1)) (return 0)
1074-- Stream.iterateM (\x -> print x >> return (x + 1)) (return 0)
1087-- & Stream.take 3
1075-- & Stream.take 3
1088−-- & Stream.fold Fold.toList
1076+-- & Stream.toList
@@ -1099,14 +1087,11 @@ iterateM step = unfold (Unfold.iterateM step)
1099iterateM step = Stream (\_ st -> st >>= \(!x) -> return $ Yield x (step x))
1087iterateM step = Stream (\_ st -> st >>= \(!x) -> return $ Yield x (step x))
1103−-- >>> iterate f x = x `Stream.cons` iterate f x
1105−-- Generate an infinite stream with @x@ as the first element and each
1090+-- | Generate an infinite stream with @x@ as the first element and each
1106-- successive element derived by applying the function @f@ on the previous
1091-- successive element derived by applying the function @f@ on the previous
1107-- element.
1092-- element.
1109−-- >>> Stream.fold Fold.toList $ Stream.take 5 $ Stream.iterate (+1) 1
1094+-- >>> Stream.toList $ Stream.take 5 $ Stream.iterate (+1) 1
1110-- [1,2,3,4,5]
1095-- [1,2,3,4,5]
1112{-# INLINE_NORMAL iterate #-}
1097{-# INLINE_NORMAL iterate #-}
@@ -1196,7 +1181,7 @@ fromPtrN n = take n . fromPtr
1196-- Note that this is completely safe when reading from Haskell string
1181-- Note that this is completely safe when reading from Haskell string
1197-- literals because they are guaranteed to be NULL terminated:
1182-- literals because they are guaranteed to be NULL terminated:
1199−-- >>> Stream.fold Fold.toList $ Stream.fromByteStr# "\1\2\3\0"#
1184+-- >>> Stream.toList $ Stream.fromByteStr# "\1\2\3\0"#
1200-- [1,2,3]
1185-- [1,2,3]
1202{-# INLINE fromByteStr# #-}
1187{-# INLINE fromByteStr# #-}