@@ -29,6 +29,7 @@ module Streamly.Prelude
29 -- ** General Folds
29 -- ** General Folds
@@ -155,6 +156,19 @@ foldrM step acc m = go (toStream m)
155 yield a (Just x) = step a (go x)
156 yield a (Just x) = step a (go x)
156 in (runStream m1) Nothing stop yield
157 in (runStream m1) Nothing stop yield
159+-- | Scan left. A strict left fold which accumulates the result of its reduction steps inside a stream, from left.
161+scan :: Streaming t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
162+scan step begin done m = cons (done begin) $ fromStream $ go (toStream m) begin
164+ go m1 !acc = Stream $ \_ stp yld ->
166+ yield a Nothing = yld (done $ step acc a) Nothing
169+ in yld (done s) (Just (go x s))
170+ in runStream m1 Nothing stop yield
158-- XXX This more straightforward implementation of fold is almost twice as
172-- XXX This more straightforward implementation of fold is almost twice as
159-- costly as the implementation we are using! Need to figure out what's wrong
173-- costly as the implementation we are using! Need to figure out what's wrong
160-- with it. Is it GHC that is not able to optimize this somehow? Or is it
174-- with it. Is it GHC that is not able to optimize this somehow? Or is it