2{-# LANGUAGE ExistentialQuantification #-}
3{-# LANGUAGE FlexibleContexts #-}
6-- Module : Streamly.Fold.Types
7-- Copyright : (c) 2019 Composewell Technologies
8-- (c) 2013 Gabriel Gonzalez
10-- Maintainer : harendra.kumar@gmail.com
11-- Stability : experimental
14module Streamly.Fold.Types
16 , toListRevF -- experimental
33import Control.Applicative (liftA2)
34import Control.Concurrent (threadDelay, forkIO, killThread)
35import Control.Concurrent.MVar (MVar, newMVar, takeMVar, putMVar)
36import Control.Exception (SomeException(..), catch, mask)
37import Control.Monad (void)
38import Control.Monad.Catch (throwM)
39import Control.Monad.IO.Class (MonadIO(..))
40import Control.Monad.Trans.Control (control)
41import Data.Maybe (isJust, fromJust)
42#if __GLASGOW_HASKELL__ < 808
43import Data.Semigroup (Semigroup(..))
45import Streamly.Strict (Tuple'(..), Tuple3'(..), Either'(..))
46import Streamly.SVar (MonadAsync)
48------------------------------------------------------------------------------
50------------------------------------------------------------------------------
52-- | Represents a left fold over an input stream of values of type @a@ to a
53-- single value of type @b@ in 'Monad' @m@.
57-- The fold uses an intermediate type @x@ as accumulator. The fold accumulator
58-- is initialized by calling the @init@ function and is then driven by calling
59-- the step function repeatedly. When the fold is done the @extract@ function
60-- is used to map the intermediate type @x@ to the final type @b@. This allows
61-- the state of the fold to be embedded in an arbitrary type @x@.
63 -- | @Fold @ @ step @ @ initial @ @ extract@
64 forall x. Fold (x -> a -> m x) (m x) (x -> m b)
66-- | Maps a function on the output of the fold (the type @b@).
67instance Applicative m => Functor (Fold m a) where
69 fmap f (Fold step start done) = Fold step start done'
71 done' x = fmap f $! done x
76-- | The fold resulting from '<*>' distributes its input to both the argument
77-- folds and combines their output using the supplied function.
78instance Applicative m => Applicative (Fold m a) where
80 pure b = Fold (\() _ -> pure ()) (pure ()) (\() -> pure b)
83 (Fold stepL beginL doneL) <*> (Fold stepR beginR doneR) =
84 let step (Tuple' xL xR) a = Tuple' <$> stepL xL a <*> stepR xR a
85 begin = Tuple' <$> beginL <*> beginR
86 done (Tuple' xL xR) = doneL xL <*> doneR xR
87 in Fold step begin done
95-- | Combines the outputs of the folds (the type @b@) using their 'Semigroup'
97instance (Semigroup b, Monad m) => Semigroup (Fold m a b) where
101-- | Combines the outputs of the folds (the type @b@) using their 'Monoid'
103instance (Semigroup b, Monoid b, Monad m) => Monoid (Fold m a b) where
104 {-# INLINE mempty #-}
107 {-# INLINE mappend #-}
110-- | Combines the fold outputs (type @b@) using their 'Num' instances.
111instance (Monad m, Num b) => Num (Fold m a b) where
112 {-# INLINE fromInteger #-}
113 fromInteger = pure . fromInteger
115 {-# INLINE negate #-}
121 {-# INLINE signum #-}
133-- | Combines the fold outputs (type @b@) using their 'Fractional' instances.
134instance (Monad m, Fractional b) => Fractional (Fold m a b) where
135 {-# INLINE fromRational #-}
136 fromRational = pure . fromRational
144-- | Combines the fold outputs using their 'Floating' instances.
145instance (Monad m, Floating b) => Floating (Fold m a b) where
197 {-# INLINE logBase #-}
198 logBase = liftA2 logBase
200------------------------------------------------------------------------------
202------------------------------------------------------------------------------
204-- This is more efficient than 'toList'. toList is exactly the same as
205-- reversing the list after toListRev.
207-- | Buffers the input stream to a list in the reverse order of the input.
209-- /Warning!/ working on large lists accumulated as buffers in memory could be
210-- very inefficient, consider using "Streamly.Array" instead.
214-- xn : ... : x2 : x1 : []
215{-# INLINABLE toListRevF #-}
216toListRevF :: Monad m => Fold m a [a]
217toListRevF = Fold (\xs x -> return $ x:xs) (return []) return
219-- | @(lmap f fold)@ maps the function @f@ on the input of the fold.
221-- >>> S.runFold (FL.lmap (\x -> x * x) FL.sum) (S.enumerateFromTo 1 100)
225{-# INLINABLE lmap #-}
226lmap :: (a -> b) -> Fold m b r -> Fold m a r
227lmap f (Fold step begin done) = Fold step' begin done
229 step' x a = step x (f a)
231-- | @(lmapM f fold)@ maps the monadic function @f@ on the input of the fold.
234{-# INLINABLE lmapM #-}
235lmapM :: Monad m => (a -> m b) -> Fold m b r -> Fold m a r
236lmapM f (Fold step begin done) = Fold step' begin done
238 step' x a = f a >>= step x
240------------------------------------------------------------------------------
242------------------------------------------------------------------------------
244-- | Include only those elements that pass a predicate.
246-- >>> S.runFold (lfilter (> 5) FL.sum) [1..10]
250{-# INLINABLE lfilter #-}
251lfilter :: Monad m => (a -> Bool) -> Fold m a r -> Fold m a r
252lfilter f (Fold step begin done) = Fold step' begin done
254 step' x a = if f a then step x a else return x
256-- | Like 'lfilter' but with a monadic predicate.
259{-# INLINABLE lfilterM #-}
260lfilterM :: Monad m => (a -> m Bool) -> Fold m a r -> Fold m a r
261lfilterM f (Fold step begin done) = Fold step' begin done
265 if use then step x a else return x
267-- | Transform a fold from a pure input to a 'Maybe' input, consuming only
269{-# INLINE lcatMaybes #-}
270lcatMaybes :: Monad m => Fold m a b -> Fold m (Maybe a) b
271lcatMaybes = lfilter isJust . lmap fromJust
273-- | Take first 'n' elements from the stream and discard the rest.
276{-# INLINABLE ltake #-}
277ltake :: Monad m => Int -> Fold m a b -> Fold m a b
278ltake n (Fold step initial done) = Fold step' initial' done'
280 initial' = fmap (Tuple' 0) initial
281 step' (Tuple' i r) a = do
285 return $ Tuple' (i + 1) res
286 else return $ Tuple' i r
287 done' (Tuple' _ r) = done r
289-- | Takes elements from the input as long as the predicate succeeds.
292{-# INLINABLE ltakeWhile #-}
293ltakeWhile :: Monad m => (a -> Bool) -> Fold m a b -> Fold m a b
294ltakeWhile predicate (Fold step initial done) = Fold step' initial' done'
296 initial' = fmap Left' initial
297 step' (Left' r) a = do
299 then fmap Left' $ step r a
300 else return (Right' r)
302 done' (Left' r) = done r
303 done' (Right' r) = done r
305------------------------------------------------------------------------------
307------------------------------------------------------------------------------
309-- | Modify the fold such that when the fold is done, instead of returning the
310-- accumulator, it returns a fold. The returned fold starts from where we left
311-- i.e. it uses the last accumulator value as the initial value of the
312-- accumulator. Thus we can resume the fold later and feed it more input.
315-- > more <- S.runFold (FL.duplicate FL.sum) (S.enumerateFromTo 1 10)
316-- > evenMore <- S.runFold (FL.duplicate more) (S.enumerateFromTo 11 20)
317-- > S.runFold evenMore (S.enumerateFromTo 21 30)
321{-# INLINABLE duplicate #-}
322duplicate :: Applicative m => Fold m a b -> Fold m a (Fold m a b)
323duplicate (Fold step begin done) =
324 Fold step begin (\x -> pure (Fold step (pure x) done))
326-- | Run the initialization effect of a fold. The returned fold would use the
327-- value returned by this effect as its initial value.
329{-# INLINABLE initialize #-}
330initialize :: Monad m => Fold m a b -> m (Fold m a b)
331initialize (Fold step initial extract) = do
333 return $ Fold step (return i) extract
335-- | Run one step of a fold and store the accumulator as an initial value in
337{-# INLINABLE runStep #-}
338runStep :: Monad m => Fold m a b -> a -> m (Fold m a b)
339runStep (Fold step initial extract) a = do
342 return $ (Fold step (return r) extract)
344-- | For every n input items, apply the first fold and supply the result to the
347{-# INLINE lchunksOf #-}
348lchunksOf :: Monad m => Int -> Fold m a b -> Fold m b c -> Fold m a c
349lchunksOf n (Fold step1 initial1 extract1) (Fold step2 initial2 extract2) =
350 Fold step' initial' extract'
354 initial' = (Tuple3' 0) <$> initial1 <*> initial2
355 step' (Tuple3' i r1 r2) a = do
359 return $ Tuple3' (i + 1) res r2
366 return $ Tuple3' 1 acc1 acc2
367 extract' (Tuple3' _ r1 r2) = do
372-- | Group the input stream into windows of n second each and then fold each
373-- group using the provided fold function.
375-- For example, we can copy and distribute a stream to multiple folds where
376-- each fold can group the input differently e.g. by one second, one minute and
377-- one hour windows respectively and fold each resulting stream of folds.
381-- -----Fold m a b----|-Fold n a c-|-Fold n a c-|-...-|----Fold m a c
384{-# INLINE lsessionsOf #-}
385lsessionsOf :: MonadAsync m => Double -> Fold m a b -> Fold m b c -> Fold m a c
386lsessionsOf n (Fold step1 initial1 extract1) (Fold step2 initial2 extract2) =
387 Fold step' initial' extract'
391 -- XXX MVar may be expensive we need a cheaper synch mechanism here
395 mv1 <- liftIO $ newMVar i1
396 mv2 <- liftIO $ newMVar (Right i2)
397 t <- control $ \run ->
398 mask $ \restore -> do
399 tid <- forkIO $ catch (restore $ void $ run (timerThread mv1 mv2))
400 (handleChildException mv2)
402 return $ Tuple3' t mv1 mv2
403 step' acc@(Tuple3' _ mv1 _) a = do
404 r1 <- liftIO $ takeMVar mv1
406 liftIO $ putMVar mv1 res
408 extract' (Tuple3' tid _ mv2) = do
409 r2 <- liftIO $ takeMVar mv2
410 liftIO $ killThread tid
413 Right x -> extract2 x
415 timerThread mv1 mv2 = do
416 liftIO $ threadDelay (round $ n * 1000000)
418 r1 <- liftIO $ takeMVar mv1
420 liftIO $ putMVar mv1 i1
423 r2 <- liftIO $ takeMVar mv2
426 Right x -> fmap Right $ step2 x res1
427 liftIO $ putMVar mv2 res
430 handleChildException ::
431 MVar (Either SomeException a) -> SomeException -> IO ()
432 handleChildException mv2 e = do
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list