summaryrefslogtreecommitdiff
path: root/src/Database/InfluxDB/Stream.hs
blob: 7820616bebb49aa87e4c386893dd3543e75edf11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{-# LANGUAGE BangPatterns #-}
module Database.InfluxDB.Stream where
import Prelude hiding (mapM)

-- | Effectful stream
data Stream m a
  = Yield a (m (Stream m a))
  -- ^ Yield a value. The stream will be continued.
  | Done
  -- ^ The end of the stream.

-- | Map each element of a stream to a monadic action, evaluate these actions
-- from left to right, and collect the results as a stream.
mapM :: Monad m => (a -> m b) -> Stream m a -> m (Stream m b)
mapM _ Done = return Done
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'