@@ -150,6 +150,7 @@ import Prelude hiding
150150-- $setup
151151-- >>> :m
152152-- >>> import Control.Concurrent (threadDelay)
153+-- >>> import Control.Monad.IO.Class (MonadIO (liftIO))
153154-- >>> import Control.Monad.Trans (lift)
154155-- >>> import Control.Monad.Trans.Identity (runIdentityT)
155156-- >>> import Data.Function ((&))
@@ -192,21 +193,21 @@ transform pipe xs = fromStreamD $ D.transform pipe (toStreamD xs)
192193-- stateful transformations. However, note that the custom map and filter
193194-- routines can be much more efficient than this due to better stream fusion.
194195--
195−-- >>> input = Stream.unfold Unfold.fromList [1..5]
196+-- >>> input = Stream.fromList [1..5]
196197-- >>> Stream.fold Fold.toList $ Stream.foldrS Stream.cons Stream.nil input
197198-- [1,2,3,4,5]
198199--
199200-- Find if any element in the stream is 'True':
200201--
201202-- >>> step x xs = if odd x then return True else xs
202−-- >>> input = Stream.unfold Unfold.fromList (2:4:5:undefined) :: Stream IO Int
203+-- >>> input = Stream.fromList (2:4:5:undefined) :: Stream IO Int
203204-- >>> Stream.fold Fold.toList $ Stream.foldrS step (return False) input
204205-- [True]
205206--
206207-- Map (+2) on odd elements and filter out the even elements:
207208--
208209-- >>> step x xs = if odd x then (x + 2) `Stream.cons` xs else xs
209−-- >>> input = Stream.unfold Unfold.fromList [1..5] :: Stream IO Int
210+-- >>> input = Stream.fromList [1..5] :: Stream IO Int
210211-- >>> Stream.fold Fold.toList $ Stream.foldrS step Stream.nil input
211212-- [3,5,7]
212213--
@@ -252,7 +253,7 @@ foldrT f z s = D.foldrT f z (toStreamD s)
252253-- Apply a monadic function to each element of the stream and replace it with
253254-- the output of the resulting action.
254255--
255−-- >>> s = Stream.unfold Unfold.fromList ["a", "b", "c"]
256+-- >>> s = Stream.fromList ["a", "b", "c"]
256257-- >>> Stream.fold Fold.drain $ Stream.mapM putStr s
257258-- abc
258259--
@@ -263,12 +264,12 @@ mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m b
263264mapM f m = fromStreamK $ D.toStreamK $ D.mapM f $ toStreamD m
264265
265266-- |
266−-- sequence = Stream.mapM id
267+-- >>> sequence = Stream.mapM id
267268--
268269-- Replace the elements of a stream of monadic actions with the outputs of
269270-- those actions.
270271--
271−-- >>> s = Stream.unfold Unfold.fromList [putStr "a", putStr "b", putStrLn "c"]
272+-- >>> s = Stream.fromList [putStr "a", putStr "b", putStrLn "c"]
272273-- >>> Stream.fold Fold.drain $ Stream.sequence s
273274-- abc
274275--
@@ -291,7 +292,7 @@ sequence = mapM id
291292--
292293-- @
293294--
294−-- >>> s = Stream.unfold Unfold.enumerateFromTo (1, 2)
295+-- >>> s = Stream.enumerateFromTo 1 2
295296-- >>> Stream.fold Fold.drain $ Stream.tap (Fold.drainBy print) s
296297-- 1
297298-- 2
@@ -305,7 +306,7 @@ tap f xs = fromStreamD $ D.tap f (toStreamD xs)
305306-- | Apply a monadic function to each element flowing through the stream and
306307-- discard the results.
307308--
308−-- >>> s = Stream.unfold Unfold.enumerateFromTo (1, 2)
309+-- >>> s = Stream.enumerateFromTo 1 2
309310-- >>> Stream.fold Fold.drain $ Stream.trace print s
310311-- 1
311312-- 2
@@ -319,7 +320,7 @@ trace f = mapM (\x -> void (f x) >> return x)
319320-- | Perform a side effect before yielding each element of the stream and
320321-- discard the results.
321322--
322−-- >>> s = Stream.unfold Unfold.enumerateFromTo (1, 2)
323+-- >>> s = Stream.enumerateFromTo 1 2
323324-- >>> Stream.fold Fold.drain $ Stream.trace_ (print "got here") s
324325-- "got here"
325326-- "got here"
@@ -348,16 +349,20 @@ trace_ eff = fromStreamD . D.mapM (\x -> eff >> return x) . toStreamD
348349
349350-- | Strict left scan. Scan a stream using the given monadic fold.
350351--
351−-- >>> s = Stream.unfold Unfold.fromList [1..10]
352+-- >>> s = Stream.fromList [1..10]
352353-- >>> Stream.fold Fold.toList $ Stream.takeWhile (< 10) $ Stream.scan Fold.sum s
353354-- [0,1,3,6]
354355--
356+-- See also: 'usingStateT'
357+--
358+
359+-- EXPLANATION:
355360-- >>> scanl' step z = Stream.scan (Fold.foldl' step z)
356361--
357362-- Like 'map', 'scanl'' too is a one to one transformation,
358363-- however it adds an extra element.
359364--
360−-- >>> s = Stream.unfold Unfold.fromList [1,2,3,4]
365+-- >>> s = Stream.fromList [1,2,3,4]
361366-- >>> Stream.fold Fold.toList $ scanl' (+) 0 s
362367-- [0,1,3,6,10]
363368--
@@ -386,7 +391,7 @@ trace_ eff = fromStreamD . D.mapM (\x -> eff >> return x) . toStreamD
386391-- >>> :{
387392-- foldl' (\(_, p) (s, x) -> (s, p * x)) (0,1)
388393-- $ scanl' (\(s, _) x -> (s + x, x)) (0,1)
389−-- $ Stream.unfold Unfold.fromList [1,2,3,4]
394+-- $ Stream.fromList [1,2,3,4]
390395-- :}
391396-- (10,24)
392397--
@@ -394,8 +399,6 @@ trace_ eff = fromStreamD . D.mapM (\x -> eff >> return x) . toStreamD
394399-- lazy expressions inside the accumulator, it is recommended that a strict
395400-- data structure is used for accumulator.
396401--
397−-- See also: 'usingStateT'
398−--
399402{-# INLINE scan #-}
400403scan :: Monad m => Fold m a b -> Stream m a -> Stream m b
401404scan fld m = fromStreamD $ D.scanOnce fld $ toStreamD m
@@ -441,7 +444,7 @@ filterM p m = fromStreamD $ D.filterM p $ toStreamD m
441444
442445-- | Use a filtering fold on a stream.
443446--
444−-- >>> input = Stream.unfold Unfold.fromList [1..10]
447+-- >>> input = Stream.fromList [1..10]
445448-- >>> Stream.fold Fold.sum $ Stream.foldFilter (Fold.satisfy (> 5)) input
446449-- 40
447450--
@@ -458,7 +461,7 @@ foldFilter p = fromStreamD . D.foldFilter p . toStreamD
458461--
459462-- To strip duplicate path separators:
460463--
461−-- >>> input = Stream.unfold Unfold.fromList "//a//b"
464+-- >>> input = Stream.fromList "//a//b"
462465-- >>> f x y = x == '/' && y == '/'
463466-- >>> Stream.fold Fold.toList $ Stream.uniqBy f input
464467-- "/a/b"
@@ -539,7 +542,7 @@ nubBy = undefined -- fromStreamD . D.nubBy . toStreamD
539542-- | Deletes the first occurrence of the element in the stream that satisfies
540543-- the given equality predicate.
541544--
542−-- >>> input = Stream.unfold Unfold.fromList [1,3,3,5]
545+-- >>> input = Stream.fromList [1,3,3,5]
543546-- >>> Stream.fold Fold.toList $ Stream.deleteBy (==) 3 input
544547-- [1,3,5]
545548--
@@ -579,7 +582,7 @@ dropWhileM p m = fromStreamD $ D.dropWhileM p $ toStreamD m
579582--
580583-- >>> insertBy cmp x = Stream.mergeBy cmp (Stream.fromPure x)
581584--
582−-- >>> input = Stream.unfold Unfold.fromList [1,3,5]
585+-- >>> input = Stream.fromList [1,3,5]
583586-- >>> Stream.fold Fold.toList $ Stream.insertBy compare 2 input
584587-- [1,2,3,5]
585588--
@@ -589,7 +592,7 @@ insertBy cmp x m = fromStreamD $ D.insertBy cmp x (toStreamD m)
589592
590593-- | Insert a pure value between successive elements of a stream.
591594--
592−-- >>> input = Stream.unfold Unfold.fromList "hello"
595+-- >>> input = Stream.fromList "hello"
593596-- >>> Stream.fold Fold.toList $ Stream.intersperse ',' input
594597-- "h,e,l,l,o"
595598--
@@ -600,7 +603,7 @@ intersperse a = fromStreamD . D.intersperse a . toStreamD
600603-- | Insert a side effect before consuming an element of a stream except the
601604-- first one.
602605--
603−-- >>> input = Stream.unfold Unfold.fromList "hello"
606+-- >>> input = Stream.fromList "hello"
604607-- >>> Stream.fold Fold.drain $ Stream.trace putChar $ Stream.intersperseM_ (putChar '.') input
605608-- h.e.l.l.o
606609--
@@ -612,7 +615,7 @@ intersperseM_ m = fromStreamD . D.intersperseM_ m . toStreamD
612615-- | Intersperse a monadic action into the input stream after every @n@
613616-- elements.
614617--
615−-- >> input = Stream.unfold Unfold.fromList "hello"
618+-- >> input = Stream.fromList "hello"
616619-- >> Stream.fold Fold.toList $ Stream.intersperseBySpan 2 (return ',') input
617620-- "he,ll,o"
618621--
@@ -624,7 +627,7 @@ intersperseBySpan _n _f _xs = undefined
624627
625628-- | Insert an effect and its output after consuming an element of a stream.
626629--
627−-- >>> input = Stream.unfold Unfold.fromList "hello"
630+-- >>> input = Stream.fromList "hello"
628631-- >>> Stream.fold Fold.toList $ Stream.trace putChar $ intersperseSuffix (putChar '.' >> return ',') input
629632-- h.,e.,l.,l.,o.,"h,e,l,l,o,"
630633--
@@ -635,7 +638,7 @@ intersperseSuffix m = fromStreamD . D.intersperseSuffix m . toStreamD
635638
636639-- | Insert a side effect after consuming an element of a stream.
637640--
638−-- >>> input = Stream.unfold Unfold.fromList "hello"
641+-- >>> input = Stream.fromList "hello"
639642-- >>> Stream.fold Fold.toList $ Stream.intersperseSuffix_ (threadDelay 1000000) input
640643-- "hello"
641644--
@@ -650,7 +653,7 @@ intersperseSuffix_ m = fromStreamD . D.intersperseSuffix_ m . toStreamD
650653-- | Like 'intersperseSuffix' but intersperses an effectful action into the
651654-- input stream after every @n@ elements and after the last element.
652655--
653−-- >>> input = Stream.unfold Unfold.fromList "hello"
656+-- >>> input = Stream.fromList "hello"
654657-- >>> Stream.fold Fold.toList $ Stream.intersperseSuffixBySpan 2 (return ',') input
655658-- "he,ll,o,"
656659--
@@ -664,7 +667,7 @@ intersperseSuffixBySpan n eff =
664667
665668-- | Insert a side effect before consuming an element of a stream.
666669--
667−-- >>> input = Stream.unfold Unfold.fromList "hello"
670+-- >>> input = Stream.fromList "hello"
668671-- >>> Stream.fold Fold.toList $ Stream.trace putChar $ Stream.interspersePrefix_ (putChar '.' >> return ',') input
669672-- .h.e.l.l.o"hello"
670673--
@@ -682,12 +685,15 @@ interspersePrefix_ m = mapM (\x -> void m >> return x)
682685
683686-- | Introduce a delay of specified seconds between elements of the stream.
684687--
685−-- >>> input = Stream.unfold Unfold.enumerateFromTo (1, 3)
688+-- >>> input = Stream.enumerateFromTo 1 3
686689-- >>> Stream.fold (Fold.drainBy print) $ Stream.delay 1 input
687690-- 1
688691-- 2
689692-- 3
690693--
694+-- >>> sleep n = liftIO $ threadDelay $ round $ n * 1000000
695+-- >>> delay n = Stream.intersperseM_ $ sleep n
696+--
691697{-# INLINE delay #-}
692698delay :: MonadIO m => Double -> Stream m a -> Stream m a
693699delay n = intersperseM_ $ liftIO $ threadDelay $ round $ n * 1000000
@@ -695,7 +701,7 @@ delay n = intersperseM_ $ liftIO $ threadDelay $ round $ n * 1000000
695701-- | Introduce a delay of specified seconds after consuming an element of a
696702-- stream.
697703--
698−-- >>> input = Stream.unfold Unfold.enumerateFromTo (1, 3)
704+-- >>> input = Stream.enumerateFromTo 1 3
699705-- >>> Stream.fold (Fold.drainBy print) $ Stream.delayPost 1 input
700706-- 1
701707-- 2
@@ -710,7 +716,7 @@ delayPost n = intersperseSuffix_ $ liftIO $ threadDelay $ round $ n * 1000000
710716-- | Introduce a delay of specified seconds before consuming an element of a
711717-- stream.
712718--
713−-- >>> input = Stream.unfold Unfold.enumerateFromTo (1, 3)
719+-- >>> input = Stream.enumerateFromTo 1 3
714720-- >>> Stream.fold (Fold.drainBy print) $ Stream.delayPre 1 input
715721-- 1
716722-- 2
@@ -748,11 +754,11 @@ reassembleBy = undefined
748754-- |
749755-- >>> f = Fold.foldl' (\(i, _) x -> (i + 1, x)) (-1,undefined)
750756-- >>> indexed = Stream.postscan f
751−-- >>> indexed = Stream.zipWith (,) (Stream.unfold Unfold.enumerateFrom 0)
757+-- >>> indexed = Stream.zipWith (,) (Stream.enumerateFrom 0)
752758--
753759-- Pair each element in a stream with its index, starting from index 0.
754760--
755−-- >>> Stream.fold Fold.toList $ Stream.indexed $ Stream.unfold Unfold.fromList "hello"
761+-- >>> Stream.fold Fold.toList $ Stream.indexed $ Stream.fromList "hello"
756762-- [(0,'h'),(1,'e'),(2,'l'),(3,'l'),(4,'o')]
757763--
758764{-# INLINE indexed #-}
@@ -763,13 +769,13 @@ indexed = fromStreamD . D.indexed . toStreamD
763769-- >>> f n = Fold.foldl' (\(i, _) x -> (i - 1, x)) (n + 1,undefined)
764770-- >>> indexedR n = Stream.postscan (f n)
765771--
766−-- >>> s n = Stream.unfold Unfold.enumerateFromThen (n, (n - 1))
772+-- >>> s n = Stream.enumerateFromThen n (n - 1)
767773-- >>> indexedR n = Stream.zipWith (,) (s n)
768774--
769775-- Pair each element in a stream with its index, starting from the
770776-- given index @n@ and counting down.
771777--
772−-- >>> Stream.fold Fold.toList $ Stream.indexedR 10 $ Stream.unfold Unfold.fromList "hello"
778+-- >>> Stream.fold Fold.toList $ Stream.indexedR 10 $ Stream.fromList "hello"
773779-- [(10,'h'),(9,'e'),(8,'l'),(7,'l'),(6,'o')]
774780--
775781{-# INLINE indexedR #-}