Skip to content

Commit

Permalink
Introduce (post)scanl in Unfold module and deprecate (post)scan
Browse files Browse the repository at this point in the history
  • Loading branch information
adithyaov committed Jan 2, 2025
1 parent e7dd9be commit ec6ec47
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions core/src/DocTestDataUnfold.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
>>> :m
>>> import Streamly.Data.Unfold (Unfold)
>>> import qualified Streamly.Data.Fold as Fold
>>> import qualified Streamly.Data.Scanl as Scanl
>>> import qualified Streamly.Data.Stream as Stream
>>> import qualified Streamly.Data.Unfold as Unfold
Expand Down
33 changes: 25 additions & 8 deletions core/src/Streamly/Internal/Data/Unfold.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ module Streamly.Internal.Data.Unfold
-- ** Mapping on Output
, postscanlM'
, postscan
, scan
, scanMany
, scanl
, scanlMany
, foldMany
-- pipe

Expand Down Expand Up @@ -107,6 +107,10 @@ module Streamly.Internal.Data.Unfold
-- stream of arrays before flattening it to a stream of chars.
, onException
, handle

-- ** Deprecated
, scan
, scanMany
)
where

Expand All @@ -118,6 +122,7 @@ import Control.Monad.Catch (MonadCatch)
import Data.Functor (($>))
import GHC.Types (SPEC(..))
import Streamly.Internal.Data.Fold.Type (Fold(..))
import Streamly.Internal.Data.Scanl.Type (Scanl(..))
import Streamly.Internal.Data.IOFinalizer
(newIOFinalizer, runIOFinalizer, clearingIOFinalizer)
import Streamly.Internal.Data.Stream.Type (Stream(..))
Expand All @@ -134,7 +139,7 @@ import Streamly.Internal.Data.Unfold.Enumeration
import Streamly.Internal.Data.Unfold.Type
import Prelude
hiding (map, mapM, takeWhile, take, filter, const, zipWith
, drop, dropWhile, either)
, drop, dropWhile, either, scanl)
import Control.Monad.IO.Class (MonadIO (liftIO))
import Foreign (Storable, peek, sizeOf)
import Foreign.Ptr
Expand Down Expand Up @@ -347,8 +352,8 @@ postscan (Fold stepF initial extract final) (Unfold stepU injectU) =
data ScanState s f = ScanInit s | ScanDo s !f | ScanDone

{-# INLINE_NORMAL scanWith #-}
scanWith :: Monad m => Bool -> Fold m b c -> Unfold m a b -> Unfold m a c
scanWith restart (Fold fstep initial extract final) (Unfold stepU injectU) =
scanWith :: Monad m => Bool -> Scanl m b c -> Unfold m a b -> Unfold m a c
scanWith restart (Scanl fstep initial extract final) (Unfold stepU injectU) =
Unfold step inject

where
Expand Down Expand Up @@ -379,14 +384,20 @@ scanWith restart (Fold fstep initial extract final) (Unfold stepU injectU) =
-- | Scan the output of an 'Unfold' to change it in a stateful manner.
-- Once fold is done it will restart from its initial state.
--
-- >>> u = Unfold.scanMany (Fold.take 2 Fold.sum) Unfold.fromList
-- >>> u = Unfold.scanlMany (Scanl.take 2 Scanl.sum) Unfold.fromList
-- >>> Unfold.fold Fold.toList u [1,2,3,4,5]
-- [0,1,3,0,3,7,0,5]
--
-- /Pre-release/
{-# INLINE_NORMAL scanlMany #-}
scanlMany :: Monad m => Scanl m b c -> Unfold m a b -> Unfold m a c
scanlMany = scanWith True

-- When we remove extract from Fold this function should be removed.
{-# DEPRECATED scanMany "Please use scanlMany instead" #-}
{-# INLINE_NORMAL scanMany #-}
scanMany :: Monad m => Fold m b c -> Unfold m a b -> Unfold m a c
scanMany = scanWith True
scanMany (Fold s i e f) = scanWith True (Scanl s i e f)

-- scan2 :: Monad m => Refold m a b c -> Unfold m a b -> Unfold m a c

Expand All @@ -398,9 +409,15 @@ scanMany = scanWith True
-- [0,1,3]
--
-- /Pre-release/
{-# INLINE_NORMAL scanl #-}
scanl :: Monad m => Scanl m b c -> Unfold m a b -> Unfold m a c
scanl = scanWith False

-- When we remove extract from Fold this function should be removed.
{-# DEPRECATED scan "Please use scanl instead" #-}
{-# INLINE_NORMAL scan #-}
scan :: Monad m => Fold m b c -> Unfold m a b -> Unfold m a c
scan = scanWith False
scan (Fold s i e f) = scanWith False (Scanl s i e f)

-- | Scan the output of an 'Unfold' to change it in a stateful manner.
--
Expand Down

0 comments on commit ec6ec47

Please sign in to comment.