on master help sign in
Add some notes about parseBreak op in ArrayStream
3bab5b7Harendra Kumar
core/src/Streamly/Internal/Data/Array/Stream.hs+35−5
@@ -36,10 +36,11 @@ module Streamly.Internal.Data.Array.Stream
36 -- could also be implemented using parseBreak.
36 -- could also be implemented using parseBreak.
37 , foldBreak
37 , foldBreak
38 , foldBreakD
38 , foldBreakD
39− -- The byte level parseBreak cannot work efficiently. Because the stream
39+ -- This is chunked parseBreak. A byte level parseBreak cannot work
40+ -- efficiently. Because the stream will have to be a StreamK for
40− -- will have to be a StreamK for backtracking, StreamK at byte level would
41+ -- backtracking, StreamK at byte level would not be efficient.
41− -- not be efficient.
42− , parseBreak -- StreamK.parseBreakChunks
42+ -- parseBreak p = K.parseBreakChunks (ParserK.adaptC p)
43+ , parseBreak
43 -- , parseBreakD
44 -- , parseBreakD
44 -- , foldManyChunks
45 -- , foldManyChunks
45 -- , parseManyChunks
46 -- , parseManyChunks
@@ -77,6 +78,8 @@ where
77import Data.Bifunctor (second)
78import Data.Bifunctor (second)
78import Control.Exception (assert)
79import Control.Exception (assert)
79import Control.Monad.IO.Class (MonadIO(..))
80import Control.Monad.IO.Class (MonadIO(..))
81+-- import Data.Bifunctor (first)
82+-- import Data.Either (fromRight)
80import Data.Proxy (Proxy(..))
83import Data.Proxy (Proxy(..))
81import Data.Word (Word8)
84import Data.Word (Word8)
82import Streamly.Internal.Data.Unbox (Unbox(..))
85import Streamly.Internal.Data.Unbox (Unbox(..))
@@ -103,6 +106,7 @@ import qualified Streamly.Internal.Data.Fold.Type as FL (Fold(..), Step(..))
103import qualified Streamly.Internal.Data.Parser as PR
106import qualified Streamly.Internal.Data.Parser as PR
104import qualified Streamly.Internal.Data.Parser as PRD
107import qualified Streamly.Internal.Data.Parser as PRD
105 (Parser(..), Initial(..))
108 (Parser(..), Initial(..))
109+-- import qualified Streamly.Internal.Data.ParserK as ParserK
106import qualified Streamly.Internal.Data.Stream as D
110import qualified Streamly.Internal.Data.Stream as D
107import qualified Streamly.Internal.Data.StreamK as K
111import qualified Streamly.Internal.Data.StreamK as K
108
112
@@ -403,6 +407,17 @@ foldBreakK (FL.Fold fstep initial _ final) stream = do
403--
407--
404-- > foldBreak f = runArrayFoldBreak (ChunkFold.fromFold f)
408-- > foldBreak f = runArrayFoldBreak (ChunkFold.fromFold f)
405--
409--
410+-- Instead of using this we can adapt the fold to ParserK and use
411+-- parseBreakChunks instead. ParserK allows composing using Monad as well.
412+--
413+-- @
414+-- foldBreak f s =
415+-- fmap (first (fromRight undefined))
416+-- $ K.parseBreakChunks (ParserK.adaptC (PR.fromFold f)) s
417+-- @
418+--
419+-- We can compare perf and remove this one or define it in terms of that.
420+--
406-- /Internal/
421-- /Internal/
407--
422--
408{-# INLINE_NORMAL foldBreak #-}
423{-# INLINE_NORMAL foldBreak #-}
@@ -411,8 +426,14 @@ foldBreak ::
411 => Fold m a b
426 => Fold m a b
412 -> StreamK m (A.Array a)
427 -> StreamK m (A.Array a)
413 -> m (b, StreamK m (A.Array a))
428 -> m (b, StreamK m (A.Array a))
414−-- foldBreak f s = fmap fromStreamD <$> foldBreakD f (toStreamD s)
415foldBreak = foldBreakK
429foldBreak = foldBreakK
430+--
431+-- foldBreak f s = fmap fromStreamD <$> foldBreakD f (toStreamD s)
432+--
433+-- foldBreak f s =
434+-- fmap (first (fromRight undefined))
435+-- $ K.parseBreakChunks (ParserK.adaptC (PR.fromFold f)) s
436+--
416-- If foldBreak performs better than runArrayFoldBreak we can use a rewrite
437-- If foldBreak performs better than runArrayFoldBreak we can use a rewrite
417-- rule to rewrite runArrayFoldBreak to fold.
438-- rule to rewrite runArrayFoldBreak to fold.
418-- foldBreak f = runArrayFoldBreak (ChunkFold.fromFold f)
439-- foldBreak f = runArrayFoldBreak (ChunkFold.fromFold f)
@@ -799,6 +820,14 @@ parseBreakK (PRD.Parser pstep initial extract) stream = do
799-- | Parse an array stream using the supplied 'Parser'. Returns the parse
820-- | Parse an array stream using the supplied 'Parser'. Returns the parse
800-- result and the unconsumed stream. Throws 'ParseError' if the parse fails.
821-- result and the unconsumed stream. Throws 'ParseError' if the parse fails.
801--
822--
823+-- >> parseBreak p = K.parseBreakChunks (ParserK.adaptC p)
824+--
825+-- This is redundant and we can just use parseBreakChunks, as ParserK can be
826+-- composed using Monad. The only advantage of this is that we do not need to
827+-- adapt.
828+--
829+-- We can compare perf and remove this one or define it in terms of that.
830+--
802-- /Internal/
831-- /Internal/
803--
832--
804{-# INLINE_NORMAL parseBreak #-}
833{-# INLINE_NORMAL parseBreak #-}
@@ -812,6 +841,7 @@ parseBreak p s =
812 fmap fromStreamD <$> parseBreakD (PRD.fromParserK p) (toStreamD s)
841 fmap fromStreamD <$> parseBreakD (PRD.fromParserK p) (toStreamD s)
813-}
842-}
814parseBreak = parseBreakK
843parseBreak = parseBreakK
844+-- parseBreak p = K.parseBreakChunks (ParserK.adaptC p)
815
845
816-------------------------------------------------------------------------------
846-------------------------------------------------------------------------------
817-- Elimination - Running Array Folds and parsers
847-- Elimination - Running Array Folds and parsers