on master help sign in
Merge branch 'Abhiroop-foldrm-diff'
3838eb4Harendra Kumar
Changelog.md+6
@@ -1,3 +1,9 @@
1+## Unrelased
2+
3+### Breaking change
4+* Change the type of `foldrM` to take a pure seed and pure second argument in the step function
5+
6+
1## 0.1.2
7## 0.1.2
2
8
3### Enhancements
9### Enhancements
src/Streamly/Prelude.hs+4−4
@@ -167,13 +167,13 @@ foldr step acc m = go (toStream m)
167
167
168-- | Right fold with a monadic step function. See 'toList' for an example use.
168-- | Right fold with a monadic step function. See 'toList' for an example use.
169{-# INLINE foldrM #-}
169{-# INLINE foldrM #-}
170−foldrM :: Streaming t => (a -> m b -> m b) -> m b -> t m a -> m b
170+foldrM :: (Streaming t, Monad m) => (a -> b -> m b) -> b -> t m a -> m b
171foldrM step acc m = go (toStream m)
171foldrM step acc m = go (toStream m)
172 where
172 where
173 go m1 =
173 go m1 =
174− let stop = acc
174+ let stop = return acc
175 yield a Nothing = step a acc
175 yield a Nothing = step a acc
176− yield a (Just x) = step a (go x)
176+ yield a (Just x) = (go x) >>= (step a)
177 in (runStream m1) Nothing stop yield
177 in (runStream m1) Nothing stop yield
178
178
179-- | Scan left. A strict left fold which accumulates the result of its reduction steps inside a stream, from left.
179-- | Scan left. A strict left fold which accumulates the result of its reduction steps inside a stream, from left.
@@ -255,7 +255,7 @@ toHandle h m = go (toStream m)
255-- | Convert a stream into a list in the underlying monad.
255-- | Convert a stream into a list in the underlying monad.
256{-# INLINABLE toList #-}
256{-# INLINABLE toList #-}
257toList :: (Streaming t, Monad m) => t m a -> m [a]
257toList :: (Streaming t, Monad m) => t m a -> m [a]
258−toList = foldrM (\a xs -> fmap (a :) xs) (return [])
258+toList = foldrM (\a xs -> return (a : xs)) []
259
259
260-- | Take first 'n' elements from the stream and discard the rest.
260-- | Take first 'n' elements from the stream and discard the rest.
261{-# INLINE take #-}
261{-# INLINE take #-}