@@ -17,6 +17,9 @@
18module Streamly.Internal.Data.Stream.IsStream.Eliminate
18module Streamly.Internal.Data.Stream.IsStream.Eliminate
20+ -- * Running Examples
20 -- * Running a 'Fold'
23 -- * Running a 'Fold'
21 -- See "Streamly.Internal.Data.Fold".
24 -- See "Streamly.Internal.Data.Fold".
@@ -183,8 +186,9 @@ import Prelude hiding
189+-- >>> import Streamly.Prelude (SerialT)
186-- >>> import qualified Streamly.Prelude as Stream
190-- >>> import qualified Streamly.Prelude as Stream
187−-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream (parse)
191+-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
188-- >>> import qualified Streamly.Internal.Data.Parser as Parser
192-- >>> import qualified Streamly.Internal.Data.Parser as Parser
189-- >>> import qualified Streamly.Data.Fold as Fold
193-- >>> import qualified Streamly.Data.Fold as Fold
@@ -362,7 +366,7 @@ parseK = parse
365−-- fold f = parse (Parser.fromFold f)
369+-- fold f = Stream.parse (Parser.fromFold f)
368-- @parse p@ is not the same as @head . parseMany p@ on an empty stream.
372-- @parse p@ is not the same as @head . parseMany p@ on an empty stream.
@@ -393,7 +397,7 @@ parse_ = parseD_ . PRK.fromParserK
394-- XXX this can utilize parallel mapping if we implement it as drain . mapM
398-- XXX this can utilize parallel mapping if we implement it as drain . mapM
396−-- > mapM_ = drain . mapM
400+-- > mapM_ = Stream.drain . Stream.mapM
398-- Apply a monadic action to each element of the stream and discard the output
402-- Apply a monadic action to each element of the stream and discard the output
399-- of the action. This is not really a pure transformation operation but a
403-- of the action. This is not really a pure transformation operation but a
@@ -406,11 +410,11 @@ mapM_ f m = S.mapM_ f $ toStreamS m
408-- > drain = mapM_ (\_ -> return ())
412-- > drain = mapM_ (\_ -> return ())
409−-- > drain = fold Fold.drain
413+-- > drain = Stream.fold Fold.drain
411-- Run a stream, discarding the results. By default it interprets the stream
415-- Run a stream, discarding the results. By default it interprets the stream
412-- as 'SerialT', to run other types of streams use the type adapting
416-- as 'SerialT', to run other types of streams use the type adapting
413−-- combinators for example @drain . 'asyncly'@.
417+-- combinators for example @Stream.drain . 'asyncly'@.
415-- @since 0.7.0
419-- @since 0.7.0
416{-# INLINE drain #-}
420{-# INLINE drain #-}
@@ -418,8 +422,8 @@ drain :: Monad m => SerialT m a -> m ()
418drain = P.drain
422drain = P.drain
421−-- > drainN n = drain . take n
425+-- > drainN n = Stream.drain . Stream.take n
422−-- > drainN n = fold (Fold.take n Fold.drain)
426+-- > drainN n = Stream.fold (Fold.take n Fold.drain)
424-- Run maximum up to @n@ iterations of a stream.
428-- Run maximum up to @n@ iterations of a stream.
@@ -440,7 +444,7 @@ runN :: Monad m => Int -> SerialT m a -> m ()
440runN = drainN
444runN = drainN
443−-- > drainWhile p = drain . takeWhile p
447+-- > drainWhile p = Stream.drain . Stream.takeWhile p
445-- Run a stream as long as the predicate holds true.
449-- Run a stream as long as the predicate holds true.
@@ -472,7 +476,7 @@ runStream = drain
473-- | Determine whether the stream is empty.
477-- | Determine whether the stream is empty.
475−-- > null = fold Fold.null
479+-- > null = Stream.fold Fold.null
477-- @since 0.1.1
481-- @since 0.1.1
478{-# INLINE null #-}
482{-# INLINE null #-}
@@ -482,7 +486,7 @@ null = S.null . toStreamS
482-- | Extract the first element of the stream, if any.
486-- | Extract the first element of the stream, if any.
484-- > head = (!! 0)
488-- > head = (!! 0)
485−-- > head = fold Fold.head
489+-- > head = Stream.fold Fold.head
487-- @since 0.1.0
491-- @since 0.1.0
488{-# INLINE head #-}
492{-# INLINE head #-}
@@ -499,7 +503,7 @@ headElse :: Monad m => a -> SerialT m a -> m a
499headElse x = D.headElse x . toStreamD
503headElse x = D.headElse x . toStreamD
502−-- > tail = fmap (fmap snd) . uncons
506+-- > tail = fmap (fmap snd) . Stream.uncons
504-- Extract all but the first element of the stream, if any.
508-- Extract all but the first element of the stream, if any.
@@ -517,8 +521,8 @@ init m = K.init (K.adapt m)
518-- | Extract the last element of the stream, if any.
522-- | Extract the last element of the stream, if any.
520−-- > last xs = xs !! (length xs - 1)
524+-- > last xs = xs !! (Stream.length xs - 1)
521−-- > last = fold Fold.last
525+-- > last = Stream.fold Fold.last
523-- @since 0.1.1
527-- @since 0.1.1
524{-# INLINE last #-}
528{-# INLINE last #-}
@@ -527,7 +531,7 @@ last m = S.last $ toStreamS m
528-- | Determine whether an element is present in the stream.
532-- | Determine whether an element is present in the stream.
530−-- > elem = fold Fold.elem
534+-- > elem = Stream.fold Fold.elem
532-- @since 0.1.0
536-- @since 0.1.0
533{-# INLINE elem #-}
537{-# INLINE elem #-}
@@ -536,7 +540,7 @@ elem e m = S.elem e (toStreamS m)
537-- | Determine whether an element is not present in the stream.
541-- | Determine whether an element is not present in the stream.
539−-- > notElem = fold Fold.length
543+-- > notElem = Stream.fold Fold.length
541-- @since 0.1.0
545-- @since 0.1.0
542{-# INLINE notElem #-}
546{-# INLINE notElem #-}
@@ -552,7 +556,7 @@ length = foldl' (\n _ -> n + 1) 0
553-- | Determine whether all elements of a stream satisfy a predicate.
557-- | Determine whether all elements of a stream satisfy a predicate.
555−-- > all = fold Fold.all
559+-- > all = Stream.fold Fold.all
557-- @since 0.1.0
561-- @since 0.1.0
558{-# INLINE all #-}
562{-# INLINE all #-}
@@ -561,7 +565,7 @@ all p m = S.all p (toStreamS m)
562-- | Determine whether any of the elements of a stream satisfy a predicate.
566-- | Determine whether any of the elements of a stream satisfy a predicate.
564−-- > any = fold Fold.any
568+-- > any = Stream.fold Fold.any
566-- @since 0.1.0
570-- @since 0.1.0
567{-# INLINE any #-}
571{-# INLINE any #-}
@@ -570,7 +574,7 @@ any p m = S.any p (toStreamS m)
571-- | Determines if all elements of a boolean stream are True.
575-- | Determines if all elements of a boolean stream are True.
573−-- > and = fold Fold.and
577+-- > and = Stream.fold Fold.and
575-- @since 0.5.0
579-- @since 0.5.0
576{-# INLINE and #-}
580{-# INLINE and #-}
@@ -579,7 +583,7 @@ and = all (==True)
580-- | Determines whether at least one element of a boolean stream is True.
584-- | Determines whether at least one element of a boolean stream is True.
582−-- > or = fold Fold.or
586+-- > or = Stream.fold Fold.or
584-- @since 0.5.0
588-- @since 0.5.0
585{-# INLINE or #-}
589{-# INLINE or #-}
@@ -590,7 +594,7 @@ or = any (==True)
590-- the stream is empty. Note that this is not numerically stable for floating
594-- the stream is empty. Note that this is not numerically stable for floating
591-- point numbers.
595-- point numbers.
593−-- > sum = fold Fold.sum
597+-- > sum = Stream.fold Fold.sum
595-- @since 0.1.0
599-- @since 0.1.0
596{-# INLINE sum #-}
600{-# INLINE sum #-}
@@ -600,7 +604,7 @@ sum = foldl' (+) 0
600-- | Determine the product of all elements of a stream of numbers. Returns @1@
604-- | Determine the product of all elements of a stream of numbers. Returns @1@
601-- when the stream is empty.
605-- when the stream is empty.
603−-- > product = fold Fold.product
607+-- > product = Stream.fold Fold.product
605-- @since 0.1.1
609-- @since 0.1.1
606{-# INLINE product #-}
610{-# INLINE product #-}
@@ -609,7 +613,7 @@ product = foldl' (*) 1
610-- | Fold a stream of monoid elements by appending them.
614-- | Fold a stream of monoid elements by appending them.
612−-- > mconcat = fold Fold.mconcat
616+-- > mconcat = Stream.fold Fold.mconcat
614-- /Pre-release/
618-- /Pre-release/
615{-# INLINE mconcat #-}
619{-# INLINE mconcat #-}
@@ -619,7 +623,7 @@ mconcat = foldr mappend mempty
621-- minimum = 'minimumBy' compare
625-- minimum = 'minimumBy' compare
622− -- minimum = fold Fold.minimum
626+ -- minimum = Stream.fold Fold.minimum
625-- Determine the minimum element in a stream.
629-- Determine the minimum element in a stream.
@@ -632,7 +636,7 @@ minimum m = S.minimum (toStreamS m)
632-- | Determine the minimum element in a stream using the supplied comparison
636-- | Determine the minimum element in a stream using the supplied comparison
633-- function.
637-- function.
635−-- > minimumBy = fold Fold.minimumBy
639+-- > minimumBy = Stream.fold Fold.minimumBy
637-- @since 0.6.0
641-- @since 0.6.0
638{-# INLINE minimumBy #-}
642{-# INLINE minimumBy #-}
@@ -642,7 +646,7 @@ minimumBy cmp m = S.minimumBy cmp (toStreamS m)
644-- maximum = 'maximumBy' compare
648-- maximum = 'maximumBy' compare
645−-- maximum = fold Fold.maximum
649+-- maximum = Stream.fold Fold.maximum
648-- Determine the maximum element in a stream.
652-- Determine the maximum element in a stream.
@@ -655,7 +659,7 @@ maximum = P.maximum
655-- | Determine the maximum element in a stream using the supplied comparison
659-- | Determine the maximum element in a stream using the supplied comparison
656-- function.
660-- function.
658−-- > maximumBy = fold Fold.maximumBy
662+-- > maximumBy = Stream.fold Fold.maximumBy
660-- @since 0.6.0
664-- @since 0.6.0
661{-# INLINE maximumBy #-}
665{-# INLINE maximumBy #-}
@@ -684,8 +688,8 @@ m !! i = toStreamS m S.!! i
684-- | In a stream of (key-value) pairs @(a, b)@, return the value @b@ of the
688-- | In a stream of (key-value) pairs @(a, b)@, return the value @b@ of the
685-- first pair where the key equals the given value @a@.
689-- first pair where the key equals the given value @a@.
687−-- > lookup = snd <$> find ((==) . fst)
691+-- > lookup = snd <$> Stream.find ((==) . fst)
688−-- > lookup = fold Fold.lookup
692+-- > lookup = Stream.fold Fold.lookup
690-- @since 0.5.0
694-- @since 0.5.0
691{-# INLINE lookup #-}
695{-# INLINE lookup #-}
@@ -695,7 +699,7 @@ lookup a m = S.lookup a (toStreamS m)
695-- | Like 'findM' but with a non-monadic predicate.
699-- | Like 'findM' but with a non-monadic predicate.
697-- > find p = findM (return . p)
701-- > find p = findM (return . p)
698−-- > find = fold Fold.find
702+-- > find = Stream.fold Fold.find
700-- @since 0.5.0
704-- @since 0.5.0
701{-# INLINE find #-}
705{-# INLINE find #-}
@@ -704,7 +708,7 @@ find p m = S.find p (toStreamS m)
705-- | Returns the first element that satisfies the given predicate.
709-- | Returns the first element that satisfies the given predicate.
707−-- > findM = fold Fold.findM
711+-- > findM = Stream.fold Fold.findM
709-- @since 0.6.0
713-- @since 0.6.0
710{-# INLINE findM #-}
714{-# INLINE findM #-}
@@ -713,7 +717,7 @@ findM p m = S.findM p (toStreamS m)
714-- | Returns the first index that satisfies the given predicate.
718-- | Returns the first index that satisfies the given predicate.
716−-- > findIndex = fold Fold.findIndex
720+-- > findIndex = Stream.fold Fold.findIndex
718-- @since 0.5.0
722-- @since 0.5.0
719{-# INLINE findIndex #-}
723{-# INLINE findIndex #-}
@@ -722,7 +726,7 @@ findIndex p = head . findIndices p
723-- | Returns the first index where a given value is found in the stream.
727-- | Returns the first index where a given value is found in the stream.
725−-- > elemIndex a = findIndex (== a)
729+-- > elemIndex a = Stream.findIndex (== a)
727-- @since 0.5.0
731-- @since 0.5.0
728{-# INLINE elemIndex #-}
732{-# INLINE elemIndex #-}
@@ -735,7 +739,7 @@ elemIndex a = findIndex (== a)
738−-- toList = S.foldr (:) []
742+-- toList = Stream.foldr (:) []
741-- Convert a stream into a list in the underlying monad. The list can be
745-- Convert a stream into a list in the underlying monad. The list can be
@@ -752,7 +756,7 @@ toList = P.toList
755−-- toListRev = S.foldl' (flip (:)) []
759+-- toListRev = Stream.foldl' (flip (:)) []
758-- Convert a stream into a list in reverse order in the underlying monad.
762-- Convert a stream into a list in reverse order in the underlying monad.
@@ -787,7 +791,7 @@ toHandle h = go
787-- | Convert a stream to a pure stream.
791-- | Convert a stream to a pure stream.
790−-- toStream = foldr cons nil
794+-- toStream = Stream.foldr Stream.cons Stream.nil
793-- /Pre-release/
797-- /Pre-release/
@@ -799,7 +803,7 @@ toStream = foldr K.cons K.nil
799-- | Convert a stream to a pure stream in reverse order.
803-- | Convert a stream to a pure stream in reverse order.
802−-- toStreamRev = foldl' (flip cons) nil
806+-- toStreamRev = Stream.foldl' (flip Stream.cons) Stream.nil
805-- /Pre-release/
809-- /Pre-release/
@@ -825,10 +829,18 @@ toStreamRev = foldl' (flip K.cons) K.nil
825-- The @.@ at the end of the operator is a mnemonic for termination of the
829-- The @.@ at the end of the operator is a mnemonic for termination of the
826-- stream.
830-- stream.
828−-- @
832+-- In the example below, each stage introduces a delay of 1 sec but output is
833+-- printed every second because both stages are concurrent.
835+-- >>> import Control.Concurrent (threadDelay)
836+-- >>> import Streamly.Prelude ((|$.))
829−-- S.foldlM' (\\_ a -> threadDelay 1000000 >> print a) ()
838+-- Stream.foldlM' (\_ a -> threadDelay 1000000 >> print a) (return ())
830−-- |$. S.repeatM (threadDelay 1000000 >> return 1)
839+-- |$. Stream.replicateM 3 (threadDelay 1000000 >> return 1)
833-- /Concurrent/
845-- /Concurrent/
@@ -850,13 +862,9 @@ infixr 0 |$.
850foldAsync :: (IsStream t, MonadAsync m) => (t m a -> m b) -> (t m a -> m b)
862foldAsync :: (IsStream t, MonadAsync m) => (t m a -> m b) -> (t m a -> m b)
851foldAsync = (|$.)
863foldAsync = (|$.)
853−-- | Parallel reverse function application operator for applying a run or fold
865+-- | Same as '|$.' but with arguments reversed.
854−-- functions to a stream. Just like '|$.' except that the operands are reversed.
856−-- @
867+-- > (|&.) = flip (|$.)
857−-- S.repeatM (threadDelay 1000000 >> return 1)
858−-- |&. S.foldlM' (\\_ a -> threadDelay 1000000 >> print a) ()
861-- /Concurrent/
869-- /Concurrent/
@@ -876,10 +884,8 @@ infixl 1 |&.
876-- | Returns 'True' if the first stream is the same as or a prefix of the
884-- | Returns 'True' if the first stream is the same as or a prefix of the
877-- second. A stream is a prefix of itself.
885-- second. A stream is a prefix of itself.
880−-- > S.isPrefixOf (S.fromList "hello") (S.fromList "hello" :: SerialT IO Char)
887+-- >>> Stream.isPrefixOf (Stream.fromList "hello") (Stream.fromList "hello" :: SerialT IO Char)
884-- @since 0.6.0
890-- @since 0.6.0
885{-# INLINE isPrefixOf #-}
891{-# INLINE isPrefixOf #-}
@@ -889,10 +895,8 @@ isPrefixOf m1 m2 = D.isPrefixOf (toStreamD m1) (toStreamD m2)
889-- | Returns 'True' if the first stream is an infix of the second. A stream is
895-- | Returns 'True' if the first stream is an infix of the second. A stream is
890-- considered an infix of itself.
896-- considered an infix of itself.
893−-- > S.isInfixOf (S.fromList "hello") (S.fromList "hello" :: SerialT IO Char)
898+-- > Stream.isInfixOf (Stream.fromList "hello") (Stream.fromList "hello" :: SerialT IO Char)
897-- Space: @O(n)@ worst case where @n@ is the length of the infix.
901-- Space: @O(n)@ worst case where @n@ is the length of the infix.
@@ -928,10 +932,8 @@ isInfixOf infx stream = do
928-- | Returns 'True' if the first stream is a suffix of the second. A stream is
932-- | Returns 'True' if the first stream is a suffix of the second. A stream is
929-- considered a suffix of itself.
933-- considered a suffix of itself.
932−-- > S.isSuffixOf (S.fromList "hello") (S.fromList "hello" :: SerialT IO Char)
935+-- >>> Stream.isSuffixOf (Stream.fromList "hello") (Stream.fromList "hello" :: SerialT IO Char)
936-- Space: @O(n)@, buffers entire input stream and the suffix.
938-- Space: @O(n)@, buffers entire input stream and the suffix.
@@ -947,10 +949,8 @@ isSuffixOf suffix stream = reverse suffix `isPrefixOf` reverse stream
947-- the second stream. The elements do not have to occur consecutively. A stream
949-- the second stream. The elements do not have to occur consecutively. A stream
948-- is a subsequence of itself.
950-- is a subsequence of itself.
951−-- > S.isSubsequenceOf (S.fromList "hlo") (S.fromList "hello" :: SerialT IO Char)
952+-- >>> Stream.isSubsequenceOf (Stream.fromList "hlo") (Stream.fromList "hello" :: SerialT IO Char)
955-- @since 0.6.0
955-- @since 0.6.0
956{-# INLINE isSubsequenceOf #-}
956{-# INLINE isSubsequenceOf #-}