on master help sign in
Implement scan for Unfold #1304
commita71505377ba488a1b37a14fe952589b9d2aef457 authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateThu 28 Oct 2021, 16:06 (5 years ago) parentb0a8f57
1 file changed+30−3
src/Streamly/Internal/Data/Unfold.hs+30−3
src/Streamly/Internal/Data/Unfold.hs+30−3
@@ -427,10 +427,37 @@ either (Unfold step1 inject1) = Unfold step inject
427427--
428428-- | Scan the output of an 'Unfold' to change it in a stateful manner.
429429--
430−-- /Unimplemented/
430+-- /Pre-release/
431431{-# INLINE_NORMAL scan #-}
432−scan :: Fold m b c -> Unfold m a b -> Unfold m a c
433−scan = undefined
432+scan :: Monad m => Fold m b c -> Unfold m a b -> Unfold m a c
433+scan (Fold stepF initial extract) (Unfold stepU injectU) =
434+ Unfold step inject
435+
436+ where
437+
438+ inject a = do
439+ fs <- initial
440+ case fs of
441+ FL.Partial fs1 -> do
442+ us <- injectU a
443+ return (fs1, Just us)
444+ FL.Done _ -> return (undefined, Nothing)
445+
446+ {-# INLINE_LATE step #-}
447+ step (fs, Just us) = do
448+ ru <- stepU us
449+ case ru of
450+ Yield x s -> do
451+ rf <- stepF fs x
452+ case rf of
453+ FL.Done v -> return $ Yield v (fs, Nothing)
454+ FL.Partial fs1 -> do
455+ v <- extract fs1
456+ return $ Yield v (fs1, Just s)
457+ Skip s -> return $ Skip (fs, Just s)
458+ Stop -> return Stop
459+
460+ step (_, Nothing) = return Stop
434461
435462-- | Scan the output of an 'Unfold' to change it in a stateful manner.
436463--