summaryrefslogtreecommitdiff
path: root/src/Database/InfluxDB/Stream.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Database/InfluxDB/Stream.hs')
-rw-r--r--src/Database/InfluxDB/Stream.hs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/Database/InfluxDB/Stream.hs b/src/Database/InfluxDB/Stream.hs
index e418651..7820616 100644
--- a/src/Database/InfluxDB/Stream.hs
+++ b/src/Database/InfluxDB/Stream.hs
@@ -1,3 +1,4 @@
+{-# LANGUAGE BangPatterns #-}
module Database.InfluxDB.Stream where
import Prelude hiding (mapM)
@@ -16,3 +17,25 @@ mapM f (Yield a mb) = do
a' <- f a
b <- mb
return $ Yield a' (mapM f b)
+
+-- | Monadic left fold for 'Stream'.
+fold :: Monad m => (b -> a -> m b) -> b -> Stream m a -> m b
+fold f = loop
+ where
+ loop z stream = case stream of
+ Done -> return z
+ Yield a nextStream -> do
+ b <- f z a
+ stream' <- nextStream
+ loop b stream'
+
+-- | Strict version of 'fold'.
+fold' :: Monad m => (b -> a -> m b) -> b -> Stream m a -> m b
+fold' f = loop
+ where
+ loop z stream = case stream of
+ Done -> return z
+ Yield a nextStream -> do
+ !b <- f z a
+ stream' <- nextStream
+ loop b stream'