on master help sign in
Implement scan for Unfold #1304
a715053Ranjeet Kumar Ranjan
src/Streamly/Internal/Data/Unfold.hs+30−3
@@ -427,10 +427,37 @@ either (Unfold step1 inject1) = Unfold step inject
427--
427--
428-- | Scan the output of an 'Unfold' to change it in a stateful manner.
428-- | Scan the output of an 'Unfold' to change it in a stateful manner.
429--
429--
430−-- /Unimplemented/
430+-- /Pre-release/
431{-# INLINE_NORMAL scan #-}
431{-# INLINE_NORMAL scan #-}
432−scan :: Fold m b c -> Unfold m a b -> Unfold m a c
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)
433−scan = undefined
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
434
461
435-- | Scan the output of an 'Unfold' to change it in a stateful manner.
462-- | Scan the output of an 'Unfold' to change it in a stateful manner.
436--
463--