on master help sign in
Add a foldStream signature

And a "done" checking function for folds.

commited85ad849a0f85a00f8c4ab4268eddb3caac42b0 authorHarendra Kumar <harendra@composewell.com> dateMon 22 Aug 2022, 22:29 (4 years ago) parent7ddd91d
2 files changed+26
core/src/Streamly/Internal/Data/Fold/Type.hs+12 core/src/Streamly/Internal/Data/Stream/StreamD/Type.hs+14
core/src/Streamly/Internal/Data/Fold/Type.hs+12
@@ -411,6 +411,7 @@ module Streamly.Internal.Data.Fold.Type
411411 , snoc
412412 , duplicate
413413 , finish
414+ , isDone
414415 )
415416where
416417
@@ -1429,6 +1430,17 @@ finish (Fold _ initial extract) = do
14291430 Partial fs -> extract fs
14301431 Done b -> return b
14311432
1433+-- | Check if the fold is done and can take no more input.
1434+--
1435+-- /Pre-release/
1436+{-# INLINE isDone #-}
1437+isDone :: Monad m => Fold m a b -> m (Maybe b)
1438+isDone (Fold _ initial _) = do
1439+ res <- initial
1440+ return $ case res of
1441+ Partial _ -> Nothing
1442+ Done b -> Just b
1443+
14321444------------------------------------------------------------------------------
14331445-- Parsing
14341446------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/Stream/StreamD/Type.hs+14
@@ -44,6 +44,7 @@ module Streamly.Internal.Data.Stream.StreamD.Type
4444 , fold
4545 , foldBreak
4646 , foldContinue
47+ , foldStream
4748
4849 -- * Right Folds
4950 , foldrT
@@ -305,6 +306,9 @@ foldBreak (Fold fstep begin done) (UnStream step state) = do
305306 b <- done fs
306307 return $! (b, Stream (\ _ _ -> return Stop) ())
307308
309+-- | If the fold finishes before the stream, we can detect that the fold is
310+-- done by checking if the initial action returns Done. But the remaining
311+-- stream is discarded.
308312{-# INLINE_NORMAL foldContinue #-}
309313foldContinue :: Monad m => Fold m a b -> Stream m a -> Fold m a b
310314foldContinue (Fold fstep finitial fextract) (Stream sstep state) =
@@ -330,6 +334,16 @@ foldContinue (Fold fstep finitial fextract) (Stream sstep state) =
330334 Skip s -> go SPEC fs s
331335 Stop -> return $ FL.Partial fs
332336
337+-- | Returns when either the fold or the stream finishes. If the Fold finishes
338+-- first we can check that using Fold.done. If the fold is not done then stream
339+-- would be nil.
340+--
341+-- /Unimplemented/
342+{-# INLINE_NORMAL foldStream #-}
343+foldStream :: -- Monad m =>
344+ Fold m a b -> Stream m a -> m (Fold m a b, Stream m a)
345+foldStream = undefined
346+
333347------------------------------------------------------------------------------
334348-- Right Folds
335349------------------------------------------------------------------------------