summaryrefslogtreecommitdiff
path: root/compiler/utils/Stream.hs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/utils/Stream.hs')
-rw-r--r--compiler/utils/Stream.hs24
1 files changed, 22 insertions, 2 deletions
diff --git a/compiler/utils/Stream.hs b/compiler/utils/Stream.hs
index 2ad2b8cc7a..7eabbe1958 100644
--- a/compiler/utils/Stream.hs
+++ b/compiler/utils/Stream.hs
@@ -7,8 +7,8 @@
-- -----------------------------------------------------------------------------
module Stream (
Stream(..), yield, liftIO,
- collect, consume, fromList,
- Stream.map, Stream.mapM, Stream.mapAccumL
+ collect, collect_, consume, fromList,
+ Stream.map, Stream.mapM, Stream.mapAccumL, Stream.mapAccumL_
) where
import GhcPrelude
@@ -71,6 +71,16 @@ collect str = go str []
Left () -> return (reverse acc)
Right (a, str') -> go str' (a:acc)
+-- | Turn a Stream into an ordinary list, by demanding all the elements.
+collect_ :: Monad m => Stream m a r -> m ([a], r)
+collect_ str = go str []
+ where
+ go str acc = do
+ r <- runStream str
+ case r of
+ Left r -> return (reverse acc, r)
+ Right (a, str') -> go str' (a:acc)
+
consume :: Monad m => Stream m a b -> (a -> m ()) -> m b
consume str f = do
r <- runStream str
@@ -113,3 +123,13 @@ mapAccumL f c str = Stream $ do
Right (a, str') -> do
(c',b) <- f c a
return (Right (b, mapAccumL f c' str'))
+
+mapAccumL_ :: Monad m => (c -> a -> m (c,b)) -> c -> Stream m a r
+ -> Stream m b (c, r)
+mapAccumL_ f c str = Stream $ do
+ r <- runStream str
+ case r of
+ Left r -> return (Left (c, r))
+ Right (a, str') -> do
+ (c',b) <- f c a
+ return (Right (b, mapAccumL_ f c' str'))