@@ -28,11 +28,11 @@ module Streamly.Internal.Data.Stream.IsStream.Eliminate
29 -- * Running a 'Parser'
29 -- * Running a 'Parser'
30 -- "Streamly.Internal.Data.Parser".
30 -- "Streamly.Internal.Data.Parser".
31− , parse
31+ , Stream.parse
32− , parseK
32+ , Stream.parseK
33− , parseD
33+ , Stream.parseD
34 , Stream.parseBreak
34 , Stream.parseBreak
35− , parseBreakD
35+ , Stream.parseBreakD
37 -- * Stream Deconstruction
37 -- * Stream Deconstruction
38 -- | foldr and foldl do not provide the remaining stream. 'uncons' is more
38 -- | foldr and foldl do not provide the remaining stream. 'uncons' is more
@@ -42,8 +42,8 @@ module Streamly.Internal.Data.Stream.IsStream.Eliminate
44 -- * Right Folds
44 -- * Right Folds
45− , foldrM
45+ , Stream.foldrM
46− , foldr
46+ , Stream.foldr
48 -- * Left Folds
48 -- * Left Folds
49 -- Lazy left folds are useful only for reversing the stream
49 -- Lazy left folds are useful only for reversing the stream
@@ -139,7 +139,7 @@ module Streamly.Internal.Data.Stream.IsStream.Eliminate
139 -- trimming sequences
139 -- trimming sequences
140 , stripPrefix
140 , stripPrefix
141 -- , stripInfix
141 -- , stripInfix
142− , stripSuffix
142+ , Stream.stripSuffix
144 -- * Deprecated
144 -- * Deprecated
@@ -154,12 +154,10 @@ where
155#include "inline.hs"
155#include "inline.hs"
157−import Control.Monad.Catch (MonadThrow)
158import Control.Monad.IO.Class (MonadIO(..))
157import Control.Monad.IO.Class (MonadIO(..))
159import Control.Monad.Trans.Class (MonadTrans(..))
158import Control.Monad.Trans.Class (MonadTrans(..))
160import qualified Foreign.Storable as Storable (Storable)
159import qualified Foreign.Storable as Storable (Storable)
161import Streamly.Internal.Control.Concurrent (MonadAsync)
160import Streamly.Internal.Control.Concurrent (MonadAsync)
162−import Streamly.Internal.Data.Parser (Parser (..))
163import Streamly.Internal.Data.SVar (defState)
161import Streamly.Internal.Data.SVar (defState)
164import Streamly.Internal.Data.Stream.IsStream.Common
162import Streamly.Internal.Data.Stream.IsStream.Common
165 ( fold, foldBreak, foldContinue, drop, findIndices, reverse, splitOnSeq
163 ( fold, foldBreak, foldContinue, drop, findIndices, reverse, splitOnSeq
@@ -174,8 +172,6 @@ import qualified Streamly.Internal.Data.Fold as FL
174import qualified Streamly.Internal.Data.Stream.IsStream.Type as IsStream
172import qualified Streamly.Internal.Data.Stream.IsStream.Type as IsStream
175import qualified Streamly.Internal.Data.Stream.StreamD as D
173import qualified Streamly.Internal.Data.Stream.StreamD as D
176import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
174import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
177−import qualified Streamly.Internal.Data.Parser.ParserD as PRD
178−import qualified Streamly.Internal.Data.Parser.ParserK.Type as PRK
179#ifdef USE_STREAMK_ONLY
175#ifdef USE_STREAMK_ONLY
180import qualified Streamly.Internal.Data.Stream.StreamK as S
176import qualified Streamly.Internal.Data.Stream.StreamK as S
@@ -234,46 +230,6 @@ uncons = fmap (fmap (fmap IsStream.fromStream)) . K.uncons . Stream.toStreamK
234------------------------------------------------------------------------------
230------------------------------------------------------------------------------
235-- Right Folds
231-- Right Folds
236------------------------------------------------------------------------------
232------------------------------------------------------------------------------
238−-- | Right associative/lazy pull fold. @foldrM build final stream@ constructs
239−-- an output structure using the step function @build@. @build@ is invoked with
240−-- the next input element and the remaining (lazy) tail of the output
241−-- structure. It builds a lazy output expression using the two. When the "tail
242−-- structure" in the output expression is evaluated it calls @build@ again thus
243−-- lazily consuming the input @stream@ until either the output expression built
244−-- by @build@ is free of the "tail" or the input is exhausted in which case
245−-- @final@ is used as the terminating case for the output structure. For more
246−-- details see the description in the previous section.
248−-- Example, determine if any element is 'odd' in a stream:
250−-- >>> Stream.foldrM (\x xs -> if odd x then return True else xs) (return False) $ Stream.fromList (2:4:5:undefined)
253−-- /Since: 0.7.0 (signature changed)/
255−-- /Since: 0.2.0 (signature changed)/
258−{-# INLINE foldrM #-}
259−foldrM :: Monad m => (a -> m b -> m b) -> m b -> SerialT m a -> m b
260−foldrM = IsStream.foldrM
262−-- | Right fold, lazy for lazy monads and pure streams, and strict for strict
265−-- Please avoid using this routine in strict monads like IO unless you need a
266−-- strict right fold. This is provided only for use in lazy monads (e.g.
267−-- Identity) or pure streams. Note that with this signature it is not possible
268−-- to implement a lazy foldr when the monad @m@ is strict. In that case it
269−-- would be strict in its accumulator and therefore would necessarily consume
274−foldr :: Monad m => (a -> b -> b) -> b -> SerialT m a -> m b
275−foldr = IsStream.foldr
277-- XXX This seems to be of limited use as it cannot be used to construct
233-- XXX This seems to be of limited use as it cannot be used to construct
278-- recursive structures and for reduction foldl1' is better.
234-- recursive structures and for reduction foldl1' is better.
@@ -376,54 +332,6 @@ foldlM' step begin = S.foldlM' step begin . IsStream.toStreamS
376runSink :: Monad m => Sink m a -> SerialT m a -> m ()
332runSink :: Monad m => Sink m a -> SerialT m a -> m ()
377runSink = fold . toFold
333runSink = fold . toFold
380−------------------------------------------------------------------------------
382−------------------------------------------------------------------------------
384−-- | Parse a stream using the supplied ParserD 'PRD.Parser'.
388−{-# INLINE_NORMAL parseD #-}
389−parseD :: MonadThrow m => PRD.Parser m a b -> SerialT m a -> m b
390−parseD p = D.parse p . toStreamD
392−-- | Parse a stream using the supplied ParserK 'PRK.Parser'.
395−{-# INLINE parseK #-}
396−parseK :: MonadThrow m => PRK.Parser m a b -> SerialT m a -> m b
399−-- | Parse a stream using the supplied 'Parser'.
401−-- Unlike folds, parsers may not always result in a valid output, they may
402−-- result in an error. For example:
404−-- >>> Stream.parse (Parser.takeEQ 1 Fold.drain) Stream.nil
405−-- *** Exception: ParseError "takeEQ: Expecting exactly 1 elements, input terminated on 0"
410−-- fold f = Stream.parse (Parser.fromFold f)
413−-- @parse p@ is not the same as @head . parseMany p@ on an empty stream.
417−{-# INLINE [3] parse #-}
418−parse :: MonadThrow m => Parser m a b -> SerialT m a -> m b
419−parse = parseD . PRD.fromParserK
421−{-# INLINE_NORMAL parseBreakD #-}
422−parseBreakD :: MonadThrow m => PRD.Parser m a b -> SerialT m a -> m (b, SerialT m a)
423−parseBreakD parser strm = do
424− (b, strmD) <- D.parseBreak parser (toStreamD strm)
425− return $! (b, fromStreamD strmD)
427------------------------------------------------------------------------------
335------------------------------------------------------------------------------
428-- Specific Fold Functions
336-- Specific Fold Functions
429------------------------------------------------------------------------------
337------------------------------------------------------------------------------
@@ -651,7 +559,7 @@ product = foldl' (*) 1
651-- /Pre-release/
559-- /Pre-release/
652{-# INLINE mconcat #-}
560{-# INLINE mconcat #-}
653mconcat :: (Monad m, Monoid a) => SerialT m a -> m a
561mconcat :: (Monad m, Monoid a) => SerialT m a -> m a
654−mconcat = foldr mappend mempty
562+mconcat = Stream.foldr mappend mempty
@@ -831,7 +739,7 @@ toHandle h = go
832{-# INLINE toStream #-}
740{-# INLINE toStream #-}
833toStream :: Monad m => SerialT m a -> m (SerialT n a)
741toStream :: Monad m => SerialT m a -> m (SerialT n a)
834−toStream = foldr IsStream.cons IsStream.nil
742+toStream = Stream.foldr IsStream.cons IsStream.nil
836-- | Convert a stream to a pure stream in reverse order.
744-- | Convert a stream to a pure stream in reverse order.
@@ -1011,25 +919,6 @@ stripPrefix
1011stripPrefix m1 m2 = fmap fromStreamD <$>
919stripPrefix m1 m2 = fmap fromStreamD <$>
1012 D.stripPrefix (toStreamD m1) (toStreamD m2)
920 D.stripPrefix (toStreamD m1) (toStreamD m2)
1014−-- | Drops the given suffix from a stream. Returns 'Nothing' if the stream does
1015−-- not end with the given suffix. Returns @Just nil@ when the suffix is the
1016−-- same as the stream.
1018−-- It may be more efficient to convert the stream to an Array and use
1019−-- stripSuffix on that especially if the elements have a Storable or Prim
1022−-- See also "Streamly.Internal.Data.Stream.IsStream.Nesting.dropSuffix".
1024−-- Space: @O(n)@, buffers the entire input stream as well as the suffix
1027−{-# INLINE stripSuffix #-}
1030− => SerialT m a -> SerialT m a -> m (Maybe (SerialT m a))
1031−stripSuffix m1 m2 = fmap reverse <$> stripPrefix (reverse m1) (reverse m2)
1033------------------------------------------------------------------------------
922------------------------------------------------------------------------------
1034-- Comparison
923-- Comparison
1035------------------------------------------------------------------------------
924------------------------------------------------------------------------------