on master help sign in
Add a foldStream signature
ed85ad8Harendra Kumar
core/src/Streamly/Internal/Data/Fold/Type.hs+12
@@ -411,6 +411,7 @@ module Streamly.Internal.Data.Fold.Type
411 , snoc
411 , snoc
412 , duplicate
412 , duplicate
413 , finish
413 , finish
414+ , isDone
414 )
415 )
415where
416where
416
417
@@ -1429,6 +1430,17 @@ finish (Fold _ initial extract) = do
1429 Partial fs -> extract fs
1430 Partial fs -> extract fs
1430 Done b -> return b
1431 Done b -> return b
1431
1432
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+
1432------------------------------------------------------------------------------
1444------------------------------------------------------------------------------
1433-- Parsing
1445-- Parsing
1434------------------------------------------------------------------------------
1446------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/Stream/StreamD/Type.hs+14
@@ -44,6 +44,7 @@ module Streamly.Internal.Data.Stream.StreamD.Type
44 , fold
44 , fold
45 , foldBreak
45 , foldBreak
46 , foldContinue
46 , foldContinue
47+ , foldStream
47
48
48 -- * Right Folds
49 -- * Right Folds
49 , foldrT
50 , foldrT
@@ -305,6 +306,9 @@ foldBreak (Fold fstep begin done) (UnStream step state) = do
305 b <- done fs
306 b <- done fs
306 return $! (b, Stream (\ _ _ -> return Stop) ())
307 return $! (b, Stream (\ _ _ -> return Stop) ())
307
308
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.
308{-# INLINE_NORMAL foldContinue #-}
312{-# INLINE_NORMAL foldContinue #-}
309foldContinue :: Monad m => Fold m a b -> Stream m a -> Fold m a b
313foldContinue :: Monad m => Fold m a b -> Stream m a -> Fold m a b
310foldContinue (Fold fstep finitial fextract) (Stream sstep state) =
314foldContinue (Fold fstep finitial fextract) (Stream sstep state) =
@@ -330,6 +334,16 @@ foldContinue (Fold fstep finitial fextract) (Stream sstep state) =
330 Skip s -> go SPEC fs s
334 Skip s -> go SPEC fs s
331 Stop -> return $ FL.Partial fs
335 Stop -> return $ FL.Partial fs
332
336
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+
333------------------------------------------------------------------------------
347------------------------------------------------------------------------------
334-- Right Folds
348-- Right Folds
335------------------------------------------------------------------------------
349------------------------------------------------------------------------------