Rename fold_/parse_/foldOn

fold_ to foldBreak
foldOn to foldContinue
parse_ to parseBreak

Use Parser instead of ParserD in parseBreak API
This commit is contained in:
Harendra Kumar 2022-05-13 22:21:46 +05:30
parent 420f387a20
commit e715e4a115
7 changed files with 54 additions and 54 deletions

View File

@ -17,7 +17,7 @@ module Streamly.Internal.Data.Stream.StreamD.Eliminate
-- -- * Running a 'Parser'
, parse
, parse_
, parseBreak
-- * Stream Deconstruction
, uncons
@ -141,17 +141,17 @@ parse
-> Stream m a
-> m b
parse parser strm = do
(b, _) <- parse_ parser strm
(b, _) <- parseBreak parser strm
return b
-- | Run a 'Parse' over a stream and return rest of the Stream.
{-# INLINE_NORMAL parse_ #-}
parse_
{-# INLINE_NORMAL parseBreak #-}
parseBreak
:: MonadThrow m
=> PRD.Parser m a b
-> Stream m a
-> m (b, Stream m a)
parse_ (PRD.Parser pstep initial extract) stream@(Stream step state) = do
parseBreak (PRD.Parser pstep initial extract) stream@(Stream step state) = do
res <- initial
case res of
PRD.IPartial s -> go SPEC state (List []) s

View File

@ -42,8 +42,8 @@ module Streamly.Internal.Data.Stream.StreamD.Type
-- * Running a 'Fold'
, fold
, fold_
, foldOn
, foldBreak
, foldContinue
-- * Right Folds
, foldrT
@ -268,12 +268,12 @@ toStreamK (Stream step state) = go state
{-# INLINE_NORMAL fold #-}
fold :: Monad m => Fold m a b -> Stream m a -> m b
fold fld strm = do
(b, _) <- fold_ fld strm
(b, _) <- foldBreak fld strm
return b
{-# INLINE_NORMAL fold_ #-}
fold_ :: Monad m => Fold m a b -> Stream m a -> m (b, Stream m a)
fold_ (Fold fstep begin done) (Stream step state) = do
{-# INLINE_NORMAL foldBreak #-}
foldBreak :: Monad m => Fold m a b -> Stream m a -> m (b, Stream m a)
foldBreak (Fold fstep begin done) (Stream step state) = do
res <- begin
case res of
FL.Partial fs -> go SPEC fs state
@ -295,9 +295,9 @@ fold_ (Fold fstep begin done) (Stream step state) = do
b <- done fs
return $! (b, Stream (\ _ _ -> return Stop) ())
{-# INLINE_NORMAL foldOn #-}
foldOn :: Monad m => Fold m a b -> Stream m a -> Fold m a b
foldOn (Fold fstep finitial fextract) (Stream sstep state) =
{-# INLINE_NORMAL foldContinue #-}
foldContinue :: Monad m => Fold m a b -> Stream m a -> Fold m a b
foldContinue (Fold fstep finitial fextract) (Stream sstep state) =
Fold fstep initial fextract
where

View File

@ -611,10 +611,10 @@ parseD (PRD.Parser pstep initial extract) stream@(D.Stream step state) = do
{-# INLINE_NORMAL parse #-}
parse ::
(MonadIO m, MonadThrow m, Storable a)
=> PRD.Parser m a b
=> PR.Parser m a b
-> SerialT m (A.Array a)
-> m (b, SerialT m (A.Array a))
parse p s = fmap fromStreamD <$> parseD p (toStreamD s)
parse p s = fmap fromStreamD <$> parseD (PRD.fromParserK p) (toStreamD s)
-------------------------------------------------------------------------------
-- Elimination - Running Array Folds and parsers

View File

@ -22,9 +22,9 @@ module Streamly.Internal.Data.Stream.IsStream.Common
, relTimesWith
-- * Elimination
, foldOn
, foldContinue
, fold
, fold_
, foldBreak
-- * Transformation
, map
@ -270,18 +270,18 @@ relTimesWith = fmap snd . timesWith
-- Elimination - Running a Fold
------------------------------------------------------------------------------
-- | We can create higher order folds using 'foldOn'. We can fold a number of
-- streams to a given fold efficiently with full stream fusion. For example, to
-- fold a list of streams on the same sum fold:
-- | We can create higher order folds using 'foldContinue'. We can fold a
-- number of streams to a given fold efficiently with full stream fusion. For
-- example, to fold a list of streams on the same sum fold:
--
-- >>> concatFold = Prelude.foldl Stream.foldOn Fold.sum
-- >>> concatFold = Prelude.foldl Stream.foldContinue Fold.sum
--
-- >>> fold f = Fold.finish . Stream.foldOn f
-- >>> fold f = Fold.finish . Stream.foldContinue f
--
-- /Internal/
{-# INLINE foldOn #-}
foldOn :: Monad m => Fold m a b -> SerialT m a -> Fold m a b
foldOn f s = D.foldOn f $ IsStream.toStreamD s
{-# INLINE foldContinue #-}
foldContinue :: Monad m => Fold m a b -> SerialT m a -> Fold m a b
foldContinue f s = D.foldContinue f $ IsStream.toStreamD s
-- | Fold a stream using the supplied left 'Fold' and reducing the resulting
-- expression strictly at each step. The behavior is similar to 'foldl''. A
@ -308,13 +308,13 @@ foldOn f s = D.foldOn f $ IsStream.toStreamD s
{-# INLINE fold #-}
fold :: Monad m => Fold m a b -> SerialT m a -> m b
fold fl strm = do
(b, _) <- fold_ fl strm
(b, _) <- foldBreak fl strm
return $! b
{-# INLINE fold_ #-}
fold_ :: Monad m => Fold m a b -> SerialT m a -> m (b, SerialT m a)
fold_ fl strm = do
(b, str) <- D.fold_ fl $ IsStream.toStreamD strm
{-# INLINE foldBreak #-}
foldBreak :: Monad m => Fold m a b -> SerialT m a -> m (b, SerialT m a)
foldBreak fl strm = do
(b, str) <- D.foldBreak fl $ IsStream.toStreamD strm
return $! (b, IsStream.fromStreamD str)
------------------------------------------------------------------------------

View File

@ -23,16 +23,16 @@ module Streamly.Internal.Data.Stream.IsStream.Eliminate
-- * Running a 'Fold'
-- See "Streamly.Internal.Data.Fold".
fold
, fold_
, foldOn
, foldBreak
, foldContinue
-- * Running a 'Parser'
-- "Streamly.Internal.Data.Parser".
, parse
, parseK
, parseD
, parse_
, parseD_
, parseBreak
, parseBreakD
-- * Stream Deconstruction
-- | foldr and foldl do not provide the remaining stream. 'uncons' is more
@ -162,8 +162,8 @@ import Streamly.Internal.Control.Concurrent (MonadAsync)
import Streamly.Internal.Data.Parser (Parser (..))
import Streamly.Internal.Data.SVar (defState)
import Streamly.Internal.Data.Stream.IsStream.Common
( fold, fold_, foldOn, drop, findIndices, reverse, splitOnSeq, take
, takeWhile, mkParallel)
( fold, foldBreak, foldContinue, drop, findIndices, reverse, splitOnSeq
, take , takeWhile, mkParallel)
import Streamly.Internal.Data.Stream.IsStream.Type
(IsStream, toStreamS, fromStreamD, toStreamD)
import Streamly.Internal.Data.Stream.Serial (SerialT(..))
@ -405,19 +405,19 @@ parseK = parse
parse :: MonadThrow m => Parser m a b -> SerialT m a -> m b
parse = parseD . PRD.fromParserK
{-# INLINE_NORMAL parseD_ #-}
parseD_ :: MonadThrow m => PRD.Parser m a b -> SerialT m a -> m (b, SerialT m a)
parseD_ parser strm = do
(b, strmD) <- D.parse_ parser (toStreamD strm)
{-# INLINE_NORMAL parseBreakD #-}
parseBreakD :: MonadThrow m => PRD.Parser m a b -> SerialT m a -> m (b, SerialT m a)
parseBreakD parser strm = do
(b, strmD) <- D.parseBreak parser (toStreamD strm)
return $! (b, fromStreamD strmD)
-- | Parse a stream using the supplied 'Parser'.
--
-- /Internal/
--
{-# INLINE [3] parse_ #-}
parse_ :: MonadThrow m => Parser m a b -> SerialT m a -> m (b, SerialT m a)
parse_ = parseD_ . PRD.fromParserK
{-# INLINE [3] parseBreak #-}
parseBreak :: MonadThrow m => Parser m a b -> SerialT m a -> m (b, SerialT m a)
parseBreak = parseBreakD . PRD.fromParserK
------------------------------------------------------------------------------
-- Specific Fold Functions

View File

@ -9,7 +9,7 @@ import Test.QuickCheck.Monadic (monadicIO, run)
import qualified Streamly.Internal.Data.Array.Foreign as Array
import qualified Streamly.Internal.Data.Array.Stream.Foreign as ArrayStream
import qualified Streamly.Internal.Data.Fold as Fold
import qualified Streamly.Internal.Data.Parser.ParserD as ParserD
import qualified Streamly.Internal.Data.Parser as Parser
import qualified Streamly.Internal.Data.Stream.IsStream as Stream
import qualified Test.Hspec as Hspec
@ -33,8 +33,8 @@ chooseAny = MkGen (\r _ -> let (x,_) = random r in x)
maxTestCount :: Int
maxTestCount = 100
parse :: Property
parse = do
parseBreak :: Property
parseBreak = do
let len = 200
-- ls = input list (stream)
-- clen = chunk size
@ -50,8 +50,8 @@ parse = do
let input =
Stream.chunksOf
clen (Array.writeN clen) (Stream.fromList ls)
parser = ParserD.fromFold (Fold.take tlen Fold.toList)
in run $ ArrayStream.parse parser input
parser = Parser.fromFold (Fold.take tlen Fold.toList)
in run $ ArrayStream.parseBreak parser input
ls2 <- run $ Stream.toList $ ArrayStream.concat str
listEquals (==) (ls1 ++ ls2) ls
@ -69,4 +69,4 @@ main =
modifyMaxSuccess (const maxTestCount) $ do
describe moduleName $ do
describe "Stream parsing" $ do
prop "parse" parse
prop "parseBreak" parseBreak

View File

@ -547,9 +547,9 @@ many =
split i = FL.take i FL.toList
headAndRest :: [Int] -> Property
headAndRest ls = monadicIO $ do
(mbh, rest) <- run $ Stream.fold_ FL.head (S.fromList ls)
foldBreak :: [Int] -> Property
foldBreak ls = monadicIO $ do
(mbh, rest) <- run $ Stream.foldBreak FL.head (S.fromList ls)
rests <- run $ S.toList rest
assert (mbh == headl ls)
listEquals (==) rests (taill ls)
@ -769,4 +769,4 @@ main = hspec $ do
-- concatMap
-- chunksOf
prop "head from fold_" headAndRest
prop "foldBreak" foldBreak