on master help sign in
Replace IsStream functions with Stream exports
commit4a2d68d08a96223862ff216bebcd93773fc65fc2 authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateFri 29 Jul 2022, 14:59 (4 years ago) parent3dc7269
3 files changed+10−131
src/Streamly/Internal/Data/Stream/IsStream.hs+1−1 src/Streamly/Internal/Data/Stream/IsStream/Eliminate.hs+9−120 src/Streamly/Internal/Data/Stream/IsStream/Type.hs−10
src/Streamly/Internal/Data/Stream/IsStream.hs+1−1
@@ -32,5 +32,5 @@ import Streamly.Internal.Data.Stream.IsStream.Expand
3232import Streamly.Internal.Data.Stream.IsStream.Reduce
3333import Streamly.Internal.Data.Stream.IsStream.Transform
3434import Streamly.Internal.Data.Stream.IsStream.Type
35− hiding (cmpBy, drain, eqBy, foldl', foldrM, foldr, fold, toList, toStream
35+ hiding (cmpBy, drain, eqBy, foldl', fold, toList, toStream
3636 , fromEffect, fromPure, repeat)
src/Streamly/Internal/Data/Stream/IsStream/Eliminate.hs+9−120
@@ -28,11 +28,11 @@ module Streamly.Internal.Data.Stream.IsStream.Eliminate
2828
2929 -- * Running a 'Parser'
3030 -- "Streamly.Internal.Data.Parser".
31− , parse
32− , parseK
33− , parseD
31+ , Stream.parse
32+ , Stream.parseK
33+ , Stream.parseD
3434 , Stream.parseBreak
35− , parseBreakD
35+ , Stream.parseBreakD
3636
3737 -- * Stream Deconstruction
3838 -- | foldr and foldl do not provide the remaining stream. 'uncons' is more
@@ -42,8 +42,8 @@ module Streamly.Internal.Data.Stream.IsStream.Eliminate
4242 , uncons
4343
4444 -- * Right Folds
45− , foldrM
46− , foldr
45+ , Stream.foldrM
46+ , Stream.foldr
4747
4848 -- * Left Folds
4949 -- Lazy left folds are useful only for reversing the stream
@@ -139,7 +139,7 @@ module Streamly.Internal.Data.Stream.IsStream.Eliminate
139139 -- trimming sequences
140140 , stripPrefix
141141 -- , stripInfix
142− , stripSuffix
142+ , Stream.stripSuffix
143143
144144 -- * Deprecated
145145 , foldx
@@ -154,12 +154,10 @@ where
154154
155155#include "inline.hs"
156156
157−import Control.Monad.Catch (MonadThrow)
158157import Control.Monad.IO.Class (MonadIO(..))
159158import Control.Monad.Trans.Class (MonadTrans(..))
160159import qualified Foreign.Storable as Storable (Storable)
161160import Streamly.Internal.Control.Concurrent (MonadAsync)
162−import Streamly.Internal.Data.Parser (Parser (..))
163161import Streamly.Internal.Data.SVar (defState)
164162import Streamly.Internal.Data.Stream.IsStream.Common
165163 ( fold, foldBreak, foldContinue, drop, findIndices, reverse, splitOnSeq
@@ -174,8 +172,6 @@ import qualified Streamly.Internal.Data.Fold as FL
174172import qualified Streamly.Internal.Data.Stream.IsStream.Type as IsStream
175173import qualified Streamly.Internal.Data.Stream.StreamD as D
176174import 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
179175#ifdef USE_STREAMK_ONLY
180176import qualified Streamly.Internal.Data.Stream.StreamK as S
181177#else
@@ -234,46 +230,6 @@ uncons = fmap (fmap (fmap IsStream.fromStream)) . K.uncons . Stream.toStreamK
234230------------------------------------------------------------------------------
235231-- Right Folds
236232------------------------------------------------------------------------------
237−
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.
247−--
248−-- Example, determine if any element is 'odd' in a stream:
249−--
250−-- >>> Stream.foldrM (\x xs -> if odd x then return True else xs) (return False) $ Stream.fromList (2:4:5:undefined)
251−-- True
252−--
253−-- /Since: 0.7.0 (signature changed)/
254−--
255−-- /Since: 0.2.0 (signature changed)/
256−--
257−-- /Since: 0.1.0/
258−{-# INLINE foldrM #-}
259−foldrM :: Monad m => (a -> m b -> m b) -> m b -> SerialT m a -> m b
260−foldrM = IsStream.foldrM
261−
262−-- | Right fold, lazy for lazy monads and pure streams, and strict for strict
263−-- monads.
264−--
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
270−-- all its input.
271−--
272−-- @since 0.1.0
273−{-# INLINE foldr #-}
274−foldr :: Monad m => (a -> b -> b) -> b -> SerialT m a -> m b
275−foldr = IsStream.foldr
276−
277233-- XXX This seems to be of limited use as it cannot be used to construct
278234-- recursive structures and for reduction foldl1' is better.
279235--
@@ -376,54 +332,6 @@ foldlM' step begin = S.foldlM' step begin . IsStream.toStreamS
376332runSink :: Monad m => Sink m a -> SerialT m a -> m ()
377333runSink = fold . toFold
378334-}
379−
380−------------------------------------------------------------------------------
381−-- Running a Parser
382−------------------------------------------------------------------------------
383−
384−-- | Parse a stream using the supplied ParserD 'PRD.Parser'.
385−--
386−-- /Internal/
387−--
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
391−
392−-- | Parse a stream using the supplied ParserK 'PRK.Parser'.
393−--
394−-- /Internal/
395−{-# INLINE parseK #-}
396−parseK :: MonadThrow m => PRK.Parser m a b -> SerialT m a -> m b
397−parseK = parse
398−
399−-- | Parse a stream using the supplied 'Parser'.
400−--
401−-- Unlike folds, parsers may not always result in a valid output, they may
402−-- result in an error. For example:
403−--
404−-- >>> Stream.parse (Parser.takeEQ 1 Fold.drain) Stream.nil
405−-- *** Exception: ParseError "takeEQ: Expecting exactly 1 elements, input terminated on 0"
406−--
407−-- Note:
408−--
409−-- @
410−-- fold f = Stream.parse (Parser.fromFold f)
411−-- @
412−--
413−-- @parse p@ is not the same as @head . parseMany p@ on an empty stream.
414−--
415−-- /Pre-release/
416−--
417−{-# INLINE [3] parse #-}
418−parse :: MonadThrow m => Parser m a b -> SerialT m a -> m b
419−parse = parseD . PRD.fromParserK
420−
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)
426−
427335------------------------------------------------------------------------------
428336-- Specific Fold Functions
429337------------------------------------------------------------------------------
@@ -651,7 +559,7 @@ product = foldl' (*) 1
651559-- /Pre-release/
652560{-# INLINE mconcat #-}
653561mconcat :: (Monad m, Monoid a) => SerialT m a -> m a
654−mconcat = foldr mappend mempty
562+mconcat = Stream.foldr mappend mempty
655563
656564-- |
657565-- @
@@ -831,7 +739,7 @@ toHandle h = go
831739--
832740{-# INLINE toStream #-}
833741toStream :: Monad m => SerialT m a -> m (SerialT n a)
834−toStream = foldr IsStream.cons IsStream.nil
742+toStream = Stream.foldr IsStream.cons IsStream.nil
835743
836744-- | Convert a stream to a pure stream in reverse order.
837745--
@@ -1011,25 +919,6 @@ stripPrefix
1011919stripPrefix m1 m2 = fmap fromStreamD <$>
1012920 D.stripPrefix (toStreamD m1) (toStreamD m2)
1013921
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.
1017−--
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
1020−-- instance.
1021−--
1022−-- See also "Streamly.Internal.Data.Stream.IsStream.Nesting.dropSuffix".
1023−--
1024−-- Space: @O(n)@, buffers the entire input stream as well as the suffix
1025−--
1026−-- /Pre-release/
1027−{-# INLINE stripSuffix #-}
1028−stripSuffix
1029− :: (Monad m, Eq a)
1030− => SerialT m a -> SerialT m a -> m (Maybe (SerialT m a))
1031−stripSuffix m1 m2 = fmap reverse <$> stripPrefix (reverse m1) (reverse m2)
1032−
1033922------------------------------------------------------------------------------
1034923-- Comparison
1035924------------------------------------------------------------------------------
src/Streamly/Internal/Data/Stream/IsStream/Type.hs−10
@@ -88,9 +88,7 @@ module Streamly.Internal.Data.Stream.IsStream.Type
8888 , toList
8989
9090 -- * Fold operations
91− , foldrM
9291 , foldrMx
93− , foldr
9492
9593 , foldlx'
9694 , foldlMx'
@@ -354,19 +352,11 @@ mkStreamStream = K.MkStream
354352-- Folds
355353------------------------------------------------------------------------------
356354
357−{-# INLINE foldrM #-}
358−foldrM :: (IsStream t, Monad m) => (a -> m b -> m b) -> m b -> t m a -> m b
359−foldrM step acc m = S.foldrM step acc $ toStreamS m
360−
361355{-# INLINE foldrMx #-}
362356foldrMx :: (IsStream t, Monad m)
363357 => (a -> m x -> m x) -> m x -> (m x -> m b) -> t m a -> m b
364358foldrMx step final project m = D.foldrMx step final project $ toStreamD m
365359
366−{-# INLINE foldr #-}
367−foldr :: (IsStream t, Monad m) => (a -> b -> b) -> b -> t m a -> m b
368−foldr f z = foldrM (\a b -> f a <$> b) (return z)
369−
370360-- | Like 'foldlx'', but with a monadic step function.
371361--
372362-- @since 0.7.0