module Streamly.Internal.Data.Stream.IsStream.Reduce
(
dropPrefix
, dropInfix
, dropSuffix
, foldMany
, foldManyPost
, refoldMany
, foldSequence
, foldIterateM
, refoldIterateM
, chunksOf
, arraysOf
, intervalsOf
, chunksOfTimeout
, splitOn
, splitOnSuffix
, splitOnPrefix
, splitWithSuffix
, splitBySeq
, splitOnSeq
, splitOnSuffixSeq
, splitWithSuffixSeq
, classifySessionsBy
, classifySessionsOf
, classifyKeepAliveSessions
, parseMany
, parseManyD
, parseManyTill
, parseSequence
, parseIterate
, wordsBy
, groups
, groupsBy
, groupsByRolling
, splitInnerBy
, splitInnerBySuffix
)
where
#include "inline.hs"
import Control.Concurrent (threadDelay)
import Control.Exception (assert)
import Control.Monad.Catch (MonadThrow)
import Control.Monad.IO.Class (MonadIO(..))
import Data.Heap (Entry(..))
import Data.Kind (Type)
import Data.Maybe (isNothing)
import Foreign.Storable (Storable)
import Streamly.Internal.Control.Concurrent (MonadAsync)
import Streamly.Internal.Data.Fold.Type (Fold (..))
import Streamly.Internal.Data.Refold.Type (Refold (..))
import Streamly.Internal.Data.Parser (Parser (..))
import Streamly.Internal.Data.Array.Foreign.Type (Array)
import Streamly.Internal.Data.Stream.IsStream.Common
( concatMap
, fold
, interjectSuffix
, intersperseM
, map
, parallelFst
, repeatM
, scanlMAfter'
, splitOnSeq
, fromPure)
import Streamly.Internal.Data.Stream.IsStream.Type
(IsStream(..), fromStreamD, toStreamD, cons)
import Streamly.Internal.Data.Time.Units
( AbsTime, MilliSecond64(..), addToAbsTime, toRelTime
, toAbsTime)
import qualified Data.Heap as H
import qualified Data.Map.Strict as Map
import qualified Streamly.Internal.Data.Array.Foreign.Type as A
import qualified Streamly.Internal.Data.Fold as FL
import qualified Streamly.Internal.Data.Parser.ParserK.Type as PRK
import qualified Streamly.Internal.Data.Parser.ParserD as PRD
import qualified Streamly.Internal.Data.Stream.IsStream.Type as IsStream
import qualified Streamly.Internal.Data.Stream.StreamD as D
import qualified Streamly.Internal.Data.Stream.IsStream.Transform as Transform
import Prelude hiding (concatMap, map)
{-# INLINE dropPrefix #-}
dropPrefix ::
t m a -> t m a -> t m a
dropPrefix :: t m a -> t m a -> t m a
dropPrefix = [Char] -> t m a -> t m a -> t m a
forall a. HasCallStack => [Char] -> a
error [Char]
"Not implemented yet!"
{-# INLINE dropInfix #-}
dropInfix ::
t m a -> t m a -> t m a
dropInfix :: t m a -> t m a -> t m a
dropInfix = [Char] -> t m a -> t m a -> t m a
forall a. HasCallStack => [Char] -> a
error [Char]
"Not implemented yet!"
{-# INLINE dropSuffix #-}
dropSuffix ::
t m a -> t m a -> t m a
dropSuffix :: t m a -> t m a -> t m a
dropSuffix = [Char] -> t m a -> t m a -> t m a
forall a. HasCallStack => [Char] -> a
error [Char]
"Not implemented yet!"
{-# INLINE foldManyPost #-}
foldManyPost
:: (IsStream t, Monad m)
=> Fold m a b
-> t m a
-> t m b
foldManyPost :: Fold m a b -> t m a -> t m b
foldManyPost Fold m a b
f t m a
m = Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$ Fold m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> Stream m b
D.foldManyPost Fold m a b
f (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m)
{-# INLINE foldMany #-}
foldMany
:: (IsStream t, Monad m)
=> Fold m a b
-> t m a
-> t m b
foldMany :: Fold m a b -> t m a -> t m b
foldMany Fold m a b
f t m a
m = Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$ Fold m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> Stream m b
D.foldMany Fold m a b
f (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m)
{-# INLINE refoldMany #-}
refoldMany :: (IsStream t, Monad m) =>
Refold m c a b -> m c -> t m a -> t m b
refoldMany :: Refold m c a b -> m c -> t m a -> t m b
refoldMany Refold m c a b
f m c
action = Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m b -> t m b) -> (t m a -> Stream m b) -> t m a -> t m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Refold m c a b -> m c -> Stream m a -> Stream m b
forall (m :: * -> *) x a b.
Monad m =>
Refold m x a b -> m x -> Stream m a -> Stream m b
D.refoldMany Refold m c a b
f m c
action (Stream m a -> Stream m b)
-> (t m a -> Stream m a) -> t m a -> Stream m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD
{-# INLINE foldSequence #-}
foldSequence
::
t m (Fold m a b)
-> t m a
-> t m b
foldSequence :: t m (Fold m a b) -> t m a -> t m b
foldSequence t m (Fold m a b)
_f t m a
_m = t m b
forall a. HasCallStack => a
undefined
{-# INLINE foldIterateM #-}
foldIterateM ::
(IsStream t, Monad m) => (b -> m (Fold m a b)) -> m b -> t m a -> t m b
foldIterateM :: (b -> m (Fold m a b)) -> m b -> t m a -> t m b
foldIterateM b -> m (Fold m a b)
f m b
i t m a
m = Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$ (b -> m (Fold m a b)) -> m b -> Stream m a -> Stream m b
forall (m :: * -> *) b a.
Monad m =>
(b -> m (Fold m a b)) -> m b -> Stream m a -> Stream m b
D.foldIterateM b -> m (Fold m a b)
f m b
i (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m)
{-# INLINE refoldIterateM #-}
refoldIterateM :: (IsStream t, Monad m) =>
Refold m b a b -> m b -> t m a -> t m b
refoldIterateM :: Refold m b a b -> m b -> t m a -> t m b
refoldIterateM Refold m b a b
c m b
i t m a
m = Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$ Refold m b a b -> m b -> Stream m a -> Stream m b
forall (m :: * -> *) b a.
Monad m =>
Refold m b a b -> m b -> Stream m a -> Stream m b
D.refoldIterateM Refold m b a b
c m b
i (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m)
{-# INLINE parseMany #-}
parseMany
:: (IsStream t, MonadThrow m)
=> Parser m a b
-> t m a
-> t m b
parseMany :: Parser m a b -> t m a -> t m b
parseMany Parser m a b
p t m a
m =
Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$ Parser m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
MonadThrow m =>
Parser m a b -> Stream m a -> Stream m b
D.parseMany (Parser m a b -> Parser m a b
forall (m :: * -> *) a b.
MonadThrow m =>
Parser m a b -> Parser m a b
PRK.fromParserK Parser m a b
p) (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m)
{-# INLINE parseManyD #-}
parseManyD
:: (IsStream t, MonadThrow m)
=> PRD.Parser m a b
-> t m a
-> t m b
parseManyD :: Parser m a b -> t m a -> t m b
parseManyD Parser m a b
p t m a
m =
Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$ Parser m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
MonadThrow m =>
Parser m a b -> Stream m a -> Stream m b
D.parseMany Parser m a b
p (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m)
{-# INLINE parseSequence #-}
parseSequence
::
t m (Parser m a b)
-> t m a
-> t m b
parseSequence :: t m (Parser m a b) -> t m a -> t m b
parseSequence t m (Parser m a b)
_f t m a
_m = t m b
forall a. HasCallStack => a
undefined
{-# INLINE parseManyTill #-}
parseManyTill ::
Parser m a b
-> Parser m a x
-> t m a
-> t m b
parseManyTill :: Parser m a b -> Parser m a x -> t m a -> t m b
parseManyTill = Parser m a b -> Parser m a x -> t m a -> t m b
forall a. HasCallStack => a
undefined
{-# INLINE parseIterate #-}
parseIterate
:: (IsStream t, MonadThrow m)
=> (b -> Parser m a b)
-> b
-> t m a
-> t m b
parseIterate :: (b -> Parser m a b) -> b -> t m a -> t m b
parseIterate b -> Parser m a b
f b
i t m a
m = Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$
(b -> Parser m a b) -> b -> Stream m a -> Stream m b
forall (m :: * -> *) b a.
MonadThrow m =>
(b -> Parser m a b) -> b -> Stream m a -> Stream m b
D.parseIterate (Parser m a b -> Parser m a b
forall (m :: * -> *) a b.
MonadThrow m =>
Parser m a b -> Parser m a b
PRK.fromParserK (Parser m a b -> Parser m a b)
-> (b -> Parser m a b) -> b -> Parser m a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Parser m a b
f) b
i (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m)
{-# INLINE groupsBy #-}
groupsBy
:: (IsStream t, Monad m)
=> (a -> a -> Bool)
-> Fold m a b
-> t m a
-> t m b
groupsBy :: (a -> a -> Bool) -> Fold m a b -> t m a -> t m b
groupsBy a -> a -> Bool
cmp Fold m a b
f t m a
m = Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$ (a -> a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
(a -> a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
D.groupsBy a -> a -> Bool
cmp Fold m a b
f (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m)
{-# INLINE groupsByRolling #-}
groupsByRolling
:: (IsStream t, Monad m)
=> (a -> a -> Bool)
-> Fold m a b
-> t m a
-> t m b
groupsByRolling :: (a -> a -> Bool) -> Fold m a b -> t m a -> t m b
groupsByRolling a -> a -> Bool
cmp Fold m a b
f t m a
m = Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$ (a -> a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
(a -> a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
D.groupsRollingBy a -> a -> Bool
cmp Fold m a b
f (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m)
{-# INLINE groups #-}
groups :: (IsStream t, Monad m, Eq a) => Fold m a b -> t m a -> t m b
groups :: Fold m a b -> t m a -> t m b
groups = (a -> a -> Bool) -> Fold m a b -> t m a -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> a -> Bool) -> Fold m a b -> t m a -> t m b
groupsBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)
{-# INLINE splitOn #-}
splitOn
:: (IsStream t, Monad m)
=> (a -> Bool) -> Fold m a b -> t m a -> t m b
splitOn :: (a -> Bool) -> Fold m a b -> t m a -> t m b
splitOn a -> Bool
predicate Fold m a b
f =
Fold m a b -> t m a -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
Fold m a b -> t m a -> t m b
foldManyPost ((a -> Bool) -> Fold m a b -> Fold m a b
forall (m :: * -> *) a b.
Monad m =>
(a -> Bool) -> Fold m a b -> Fold m a b
FL.takeEndBy_ a -> Bool
predicate Fold m a b
f)
{-# INLINE splitOnSuffix #-}
splitOnSuffix
:: (IsStream t, Monad m)
=> (a -> Bool) -> Fold m a b -> t m a -> t m b
splitOnSuffix :: (a -> Bool) -> Fold m a b -> t m a -> t m b
splitOnSuffix a -> Bool
predicate Fold m a b
f = Fold m a b -> t m a -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
Fold m a b -> t m a -> t m b
foldMany ((a -> Bool) -> Fold m a b -> Fold m a b
forall (m :: * -> *) a b.
Monad m =>
(a -> Bool) -> Fold m a b -> Fold m a b
FL.takeEndBy_ a -> Bool
predicate Fold m a b
f)
{-# INLINE splitOnPrefix #-}
splitOnPrefix ::
(a -> Bool) -> Fold m a b -> t m a -> t m b
splitOnPrefix :: (a -> Bool) -> Fold m a b -> t m a -> t m b
splitOnPrefix a -> Bool
_predicate Fold m a b
_f = t m a -> t m b
forall a. HasCallStack => a
undefined
{-# INLINE wordsBy #-}
wordsBy
:: (IsStream t, Monad m)
=> (a -> Bool) -> Fold m a b -> t m a -> t m b
wordsBy :: (a -> Bool) -> Fold m a b -> t m a -> t m b
wordsBy a -> Bool
predicate Fold m a b
f t m a
m =
Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
(a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
D.wordsBy a -> Bool
predicate Fold m a b
f (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m)
{-# INLINE splitWithSuffix #-}
splitWithSuffix
:: (IsStream t, Monad m)
=> (a -> Bool) -> Fold m a b -> t m a -> t m b
splitWithSuffix :: (a -> Bool) -> Fold m a b -> t m a -> t m b
splitWithSuffix a -> Bool
predicate Fold m a b
f = Fold m a b -> t m a -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
Fold m a b -> t m a -> t m b
foldMany ((a -> Bool) -> Fold m a b -> Fold m a b
forall (m :: * -> *) a b.
Monad m =>
(a -> Bool) -> Fold m a b -> Fold m a b
FL.takeEndBy a -> Bool
predicate Fold m a b
f)
{-# INLINE splitBySeq #-}
splitBySeq
:: (IsStream t, MonadAsync m, Storable a, Enum a, Eq a)
=> Array a -> Fold m a b -> t m a -> t m b
splitBySeq :: Array a -> Fold m a b -> t m a -> t m b
splitBySeq Array a
patt Fold m a b
f t m a
m =
m b -> t m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
m a -> t m a -> t m a
intersperseM (Fold m a b -> SerialT m a -> m b
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> SerialT m a -> m b
fold Fold m a b
f (Array a -> SerialT m a
forall (m :: * -> *) a.
(Monad m, Storable a) =>
Array a -> SerialT m a
A.toStream Array a
patt)) (t m b -> t m b) -> t m b -> t m b
forall a b. (a -> b) -> a -> b
$ Array a -> Fold m a b -> t m a -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, MonadIO m, Storable a, Enum a, Eq a) =>
Array a -> Fold m a b -> t m a -> t m b
splitOnSeq Array a
patt Fold m a b
f t m a
m
{-# INLINE splitOnSuffixSeq #-}
splitOnSuffixSeq
:: (IsStream t, MonadIO m, Storable a, Enum a, Eq a)
=> Array a -> Fold m a b -> t m a -> t m b
splitOnSuffixSeq :: Array a -> Fold m a b -> t m a -> t m b
splitOnSuffixSeq Array a
patt Fold m a b
f t m a
m =
Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$ Bool -> Array a -> Fold m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
(MonadIO m, Storable a, Enum a, Eq a) =>
Bool -> Array a -> Fold m a b -> Stream m a -> Stream m b
D.splitOnSuffixSeq Bool
False Array a
patt Fold m a b
f (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m)
{-# INLINE splitWithSuffixSeq #-}
splitWithSuffixSeq
:: (IsStream t, MonadIO m, Storable a, Enum a, Eq a)
=> Array a -> Fold m a b -> t m a -> t m b
splitWithSuffixSeq :: Array a -> Fold m a b -> t m a -> t m b
splitWithSuffixSeq Array a
patt Fold m a b
f t m a
m =
Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$ Bool -> Array a -> Fold m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
(MonadIO m, Storable a, Enum a, Eq a) =>
Bool -> Array a -> Fold m a b -> Stream m a -> Stream m b
D.splitOnSuffixSeq Bool
True Array a
patt Fold m a b
f (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m)
{-# INLINE chunksOf #-}
chunksOf
:: (IsStream t, Monad m)
=> Int -> Fold m a b -> t m a -> t m b
chunksOf :: Int -> Fold m a b -> t m a -> t m b
chunksOf Int
n Fold m a b
f = Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m b -> t m b) -> (t m a -> Stream m b) -> t m a -> t m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Fold m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
Int -> Fold m a b -> Stream m a -> Stream m b
D.chunksOf Int
n Fold m a b
f (Stream m a -> Stream m b)
-> (t m a -> Stream m a) -> t m a -> Stream m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD
{-# INLINE arraysOf #-}
arraysOf :: (IsStream t, MonadIO m, Storable a)
=> Int -> t m a -> t m (Array a)
arraysOf :: Int -> t m a -> t m (Array a)
arraysOf Int
n = Stream m (Array a) -> t m (Array a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m (Array a) -> t m (Array a))
-> (t m a -> Stream m (Array a)) -> t m a -> t m (Array a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Stream m a -> Stream m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Stream m a -> Stream m (Array a)
A.arraysOf Int
n (Stream m a -> Stream m (Array a))
-> (t m a -> Stream m a) -> t m a -> Stream m (Array a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD
{-# INLINE intervalsOf #-}
intervalsOf
:: (IsStream t, MonadAsync m)
=> Double -> Fold m a b -> t m a -> t m b
intervalsOf :: Double -> Fold m a b -> t m a -> t m b
intervalsOf Double
n Fold m a b
f t m a
xs =
(Maybe a -> Bool) -> Fold m (Maybe a) b -> t m (Maybe a) -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> Bool) -> Fold m a b -> t m a -> t m b
splitWithSuffix Maybe a -> Bool
forall a. Maybe a -> Bool
isNothing (Fold m a b -> Fold m (Maybe a) b
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Fold m (Maybe a) b
FL.catMaybes Fold m a b
f)
(Double -> m (Maybe a) -> t m (Maybe a) -> t m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
Double -> m a -> t m a -> t m a
interjectSuffix Double
n (Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing) ((a -> Maybe a) -> t m a -> t m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> b) -> t m a -> t m b
map a -> Maybe a
forall a. a -> Maybe a
Just t m a
xs))
{-# INLINE chunksOfTimeout #-}
chunksOfTimeout :: (IsStream t, MonadAsync m, Functor (t m))
=> Int -> Double -> FL.Fold m a b -> t m a -> t m b
chunksOfTimeout :: Int -> Double -> Fold m a b -> t m a -> t m b
chunksOfTimeout Int
n Double
timeout Fold m a b
f =
(((), b) -> b) -> t m ((), b) -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> b) -> t m a -> t m b
map ((), b) -> b
forall a b. (a, b) -> b
snd
(t m ((), b) -> t m b) -> (t m a -> t m ((), b)) -> t m a -> t m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double
-> Bool
-> (Int -> m Bool)
-> Double
-> Fold m a b
-> t m (AbsTime, ((), a))
-> t m ((), b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
(IsStream t, MonadAsync m, Ord k) =>
Double
-> Bool
-> (Int -> m Bool)
-> Double
-> Fold m a b
-> t m (AbsTime, (k, a))
-> t m (k, b)
classifySessionsBy
Double
timeout Bool
False (m Bool -> Int -> m Bool
forall a b. a -> b -> a
const (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False)) Double
timeout (Int -> Fold m a b -> Fold m a b
forall (m :: * -> *) a b.
Monad m =>
Int -> Fold m a b -> Fold m a b
FL.take Int
n Fold m a b
f)
(t m (AbsTime, ((), a)) -> t m ((), b))
-> (t m a -> t m (AbsTime, ((), a))) -> t m a -> t m ((), b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t m ((), a) -> t m (AbsTime, ((), a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m, Functor (t m)) =>
t m a -> t m (AbsTime, a)
Transform.timestamped
(t m ((), a) -> t m (AbsTime, ((), a)))
-> (t m a -> t m ((), a)) -> t m a -> t m (AbsTime, ((), a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> ((), a)) -> t m a -> t m ((), a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> b) -> t m a -> t m b
map ((),)
data SessionState t m k a b = SessionState
{ SessionState t m k a b -> AbsTime
sessionCurTime :: !AbsTime
, SessionState t m k a b -> AbsTime
sessionEventTime :: !AbsTime
, SessionState t m k a b -> Int
sessionCount :: !Int
, SessionState t m k a b -> Heap (Entry AbsTime k)
sessionTimerHeap :: H.Heap (H.Entry AbsTime k)
, SessionState t m k a b -> Map k a
sessionKeyValueMap :: Map.Map k a
, SessionState t m k a b -> t m (k, b)
sessionOutputStream :: t (m :: Type -> Type) (k, b)
}
data SessionEntry a b = LiveSession !a !b | ZombieSession
{-# INLINABLE classifySessionsBy #-}
classifySessionsBy
:: (IsStream t, MonadAsync m, Ord k)
=> Double
-> Bool
-> (Int -> m Bool)
-> Double
-> Fold m a b
-> t m (AbsTime, (k, a))
-> t m (k, b)
classifySessionsBy :: Double
-> Bool
-> (Int -> m Bool)
-> Double
-> Fold m a b
-> t m (AbsTime, (k, a))
-> t m (k, b)
classifySessionsBy Double
tick Bool
reset Int -> m Bool
ejectPred Double
tmout
(Fold s -> a -> m (Step s b)
step m (Step s b)
initial s -> m b
extract) t m (AbsTime, (k, a))
str =
(SessionState t m k (SessionEntry AbsTime s) b -> t m (k, b))
-> t m (SessionState t m k (SessionEntry AbsTime s) b)
-> t m (k, b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> t m b) -> t m a -> t m b
concatMap SessionState t m k (SessionEntry AbsTime s) b -> t m (k, b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> t m (k, b)
sessionOutputStream (t m (SessionState t m k (SessionEntry AbsTime s) b) -> t m (k, b))
-> t m (SessionState t m k (SessionEntry AbsTime s) b)
-> t m (k, b)
forall a b. (a -> b) -> a -> b
$
(SessionState t m k (SessionEntry AbsTime s) b
-> Maybe (AbsTime, (k, a))
-> m (SessionState t m k (SessionEntry AbsTime s) b))
-> m (SessionState t m k (SessionEntry AbsTime s) b)
-> (SessionState t m k (SessionEntry AbsTime s) b
-> m (SessionState t m k (SessionEntry AbsTime s) b))
-> t m (Maybe (AbsTime, (k, a)))
-> t m (SessionState t m k (SessionEntry AbsTime s) b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) b a.
(IsStream t, Monad m) =>
(b -> a -> m b) -> m b -> (b -> m b) -> t m a -> t m b
scanlMAfter' SessionState t m k (SessionEntry AbsTime s) b
-> Maybe (AbsTime, (k, a))
-> m (SessionState t m k (SessionEntry AbsTime s) b)
forall (t :: (* -> *) -> * -> *) k (t :: (* -> *) -> * -> *)
(m :: * -> *) b (m :: * -> *).
(IsStream t, Ord k) =>
SessionState t m k (SessionEntry AbsTime s) b
-> Maybe (AbsTime, (k, a))
-> m (SessionState t m k (SessionEntry AbsTime s) b)
sstep (SessionState t m k (SessionEntry AbsTime s) b
-> m (SessionState t m k (SessionEntry AbsTime s) b)
forall (m :: * -> *) a. Monad m => a -> m a
return SessionState t m k (SessionEntry AbsTime s) b
forall (m :: * -> *) k a b. SessionState t m k a b
szero) SessionState t m k (SessionEntry AbsTime s) b
-> m (SessionState t m k (SessionEntry AbsTime s) b)
forall k (t :: (* -> *) -> * -> *) (t :: (* -> *) -> * -> *)
(m :: * -> *) a b (m :: * -> *).
(Ord k, IsStream t) =>
SessionState t m k (SessionEntry a s) b
-> m (SessionState t m k (SessionEntry a s) b)
flush t m (Maybe (AbsTime, (k, a)))
stream
where
timeoutMs :: RelTime
timeoutMs = MilliSecond64 -> RelTime
forall a. TimeUnit a => a -> RelTime
toRelTime (Double -> MilliSecond64
forall a b. (RealFrac a, Integral b) => a -> b
round (Double
tmout Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1000) :: MilliSecond64)
tickMs :: RelTime
tickMs = MilliSecond64 -> RelTime
forall a. TimeUnit a => a -> RelTime
toRelTime (Double -> MilliSecond64
forall a b. (RealFrac a, Integral b) => a -> b
round (Double
tick Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1000) :: MilliSecond64)
szero :: SessionState t m k a b
szero = SessionState :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
AbsTime
-> AbsTime
-> Int
-> Heap (Entry AbsTime k)
-> Map k a
-> t m (k, b)
-> SessionState t m k a b
SessionState
{ sessionCurTime :: AbsTime
sessionCurTime = MilliSecond64 -> AbsTime
forall a. TimeUnit a => a -> AbsTime
toAbsTime (MilliSecond64
0 :: MilliSecond64)
, sessionEventTime :: AbsTime
sessionEventTime = MilliSecond64 -> AbsTime
forall a. TimeUnit a => a -> AbsTime
toAbsTime (MilliSecond64
0 :: MilliSecond64)
, sessionCount :: Int
sessionCount = Int
0
, sessionTimerHeap :: Heap (Entry AbsTime k)
sessionTimerHeap = Heap (Entry AbsTime k)
forall a. Heap a
H.empty
, sessionKeyValueMap :: Map k a
sessionKeyValueMap = Map k a
forall k a. Map k a
Map.empty
, sessionOutputStream :: t m (k, b)
sessionOutputStream = t m (k, b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
IsStream.nil
}
sstep :: SessionState t m k (SessionEntry AbsTime s) b
-> Maybe (AbsTime, (k, a))
-> m (SessionState t m k (SessionEntry AbsTime s) b)
sstep session :: SessionState t m k (SessionEntry AbsTime s) b
session@SessionState{t m (k, b)
Int
Map k (SessionEntry AbsTime s)
Heap (Entry AbsTime k)
AbsTime
sessionOutputStream :: t m (k, b)
sessionKeyValueMap :: Map k (SessionEntry AbsTime s)
sessionTimerHeap :: Heap (Entry AbsTime k)
sessionCount :: Int
sessionEventTime :: AbsTime
sessionCurTime :: AbsTime
sessionOutputStream :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> t m (k, b)
sessionKeyValueMap :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> Map k a
sessionTimerHeap :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> Heap (Entry AbsTime k)
sessionCount :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> Int
sessionEventTime :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> AbsTime
sessionCurTime :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> AbsTime
..} (Just (AbsTime
timestamp, (k
key, a
value))) = do
let curTime :: AbsTime
curTime = AbsTime -> AbsTime -> AbsTime
forall a. Ord a => a -> a -> a
max AbsTime
sessionEventTime AbsTime
timestamp
mOld :: Maybe (SessionEntry AbsTime s)
mOld = k
-> Map k (SessionEntry AbsTime s) -> Maybe (SessionEntry AbsTime s)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup k
key Map k (SessionEntry AbsTime s)
sessionKeyValueMap
let done :: b -> m (SessionState t m k (SessionEntry AbsTime s) b)
done b
fb = do
let (Map k (SessionEntry AbsTime s)
mp, Int
cnt) = case Maybe (SessionEntry AbsTime s)
mOld of
Just (LiveSession _ _) ->
( k
-> SessionEntry AbsTime s
-> Map k (SessionEntry AbsTime s)
-> Map k (SessionEntry AbsTime s)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert k
key SessionEntry AbsTime s
forall a b. SessionEntry a b
ZombieSession Map k (SessionEntry AbsTime s)
sessionKeyValueMap
, Int
sessionCount Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
)
Maybe (SessionEntry AbsTime s)
_ -> (Map k (SessionEntry AbsTime s)
sessionKeyValueMap, Int
sessionCount)
SessionState t m k (SessionEntry AbsTime s) b
-> m (SessionState t m k (SessionEntry AbsTime s) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (SessionState t m k (SessionEntry AbsTime s) b
-> m (SessionState t m k (SessionEntry AbsTime s) b))
-> SessionState t m k (SessionEntry AbsTime s) b
-> m (SessionState t m k (SessionEntry AbsTime s) b)
forall a b. (a -> b) -> a -> b
$ SessionState t m k (SessionEntry AbsTime s) b
session
{ sessionCurTime :: AbsTime
sessionCurTime = AbsTime
curTime
, sessionEventTime :: AbsTime
sessionEventTime = AbsTime
curTime
, sessionCount :: Int
sessionCount = Int
cnt
, sessionKeyValueMap :: Map k (SessionEntry AbsTime s)
sessionKeyValueMap = Map k (SessionEntry AbsTime s)
mp
, sessionOutputStream :: t m (k, b)
sessionOutputStream = (k, b) -> t m (k, b)
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a
fromPure (k
key, b
fb)
}
partial :: s -> m (SessionState t m k (SessionEntry AbsTime s) b)
partial s
fs1 = do
let expiry :: AbsTime
expiry = AbsTime -> RelTime -> AbsTime
addToAbsTime AbsTime
timestamp RelTime
timeoutMs
(Heap (Entry AbsTime k)
hp1, Map k (SessionEntry AbsTime s)
mp1, t m (k, b)
out1, Int
cnt1) <- do
let vars :: (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s), t m a,
Int)
vars = (Heap (Entry AbsTime k)
sessionTimerHeap, Map k (SessionEntry AbsTime s)
sessionKeyValueMap,
t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
IsStream.nil, Int
sessionCount)
case Maybe (SessionEntry AbsTime s)
mOld of
Maybe (SessionEntry AbsTime s)
Nothing -> do
Bool
eject <- Int -> m Bool
ejectPred Int
sessionCount
(Heap (Entry AbsTime k)
hp, Map k (SessionEntry AbsTime s)
mp, t m (k, b)
out, Int
cnt) <-
if Bool
eject
then (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
-> m (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
forall k a d (t :: (* -> *) -> * -> *) (m :: * -> *).
(Ord k, Num d, IsStream t, Ord a) =>
(Heap (Entry a k), Map k (SessionEntry a s), t m (k, b), d)
-> m (Heap (Entry a k), Map k (SessionEntry a s), t m (k, b), d)
ejectOne (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
forall (m :: * -> *) a.
(Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s), t m a,
Int)
vars
else (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
-> m (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
forall (m :: * -> *) a.
(Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s), t m a,
Int)
vars
let hp' :: Heap (Entry AbsTime k)
hp' = Entry AbsTime k -> Heap (Entry AbsTime k) -> Heap (Entry AbsTime k)
forall a. Ord a => a -> Heap a -> Heap a
H.insert (AbsTime -> k -> Entry AbsTime k
forall p a. p -> a -> Entry p a
Entry AbsTime
expiry k
key) Heap (Entry AbsTime k)
hp
in (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
-> m (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Heap (Entry AbsTime k)
hp', Map k (SessionEntry AbsTime s)
mp, t m (k, b)
out, Int
cnt Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Just SessionEntry AbsTime s
_ -> (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
-> m (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
forall (m :: * -> *) a.
(Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s), t m a,
Int)
vars
let acc :: SessionEntry AbsTime s
acc = AbsTime -> s -> SessionEntry AbsTime s
forall a b. a -> b -> SessionEntry a b
LiveSession AbsTime
expiry s
fs1
mp2 :: Map k (SessionEntry AbsTime s)
mp2 = k
-> SessionEntry AbsTime s
-> Map k (SessionEntry AbsTime s)
-> Map k (SessionEntry AbsTime s)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert k
key SessionEntry AbsTime s
acc Map k (SessionEntry AbsTime s)
mp1
SessionState t m k (SessionEntry AbsTime s) b
-> m (SessionState t m k (SessionEntry AbsTime s) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (SessionState t m k (SessionEntry AbsTime s) b
-> m (SessionState t m k (SessionEntry AbsTime s) b))
-> SessionState t m k (SessionEntry AbsTime s) b
-> m (SessionState t m k (SessionEntry AbsTime s) b)
forall a b. (a -> b) -> a -> b
$ SessionState :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
AbsTime
-> AbsTime
-> Int
-> Heap (Entry AbsTime k)
-> Map k a
-> t m (k, b)
-> SessionState t m k a b
SessionState
{ sessionCurTime :: AbsTime
sessionCurTime = AbsTime
curTime
, sessionEventTime :: AbsTime
sessionEventTime = AbsTime
curTime
, sessionCount :: Int
sessionCount = Int
cnt1
, sessionTimerHeap :: Heap (Entry AbsTime k)
sessionTimerHeap = Heap (Entry AbsTime k)
hp1
, sessionKeyValueMap :: Map k (SessionEntry AbsTime s)
sessionKeyValueMap = Map k (SessionEntry AbsTime s)
mp2
, sessionOutputStream :: t m (k, b)
sessionOutputStream = t m (k, b)
out1
}
Step s b
res0 <- do
case Maybe (SessionEntry AbsTime s)
mOld of
Just (LiveSession AbsTime
_ s
acc) -> Step s b -> m (Step s b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ s -> Step s b
forall s b. s -> Step s b
FL.Partial s
acc
Maybe (SessionEntry AbsTime s)
_ -> m (Step s b)
initial
case Step s b
res0 of
FL.Done b
_ ->
[Char] -> m (SessionState t m k (SessionEntry AbsTime s) b)
forall a. HasCallStack => [Char] -> a
error ([Char] -> m (SessionState t m k (SessionEntry AbsTime s) b))
-> [Char] -> m (SessionState t m k (SessionEntry AbsTime s) b)
forall a b. (a -> b) -> a -> b
$ [Char]
"classifySessionsBy: "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"The supplied fold must consume at least one input"
FL.Partial s
fs -> do
Step s b
res <- s -> a -> m (Step s b)
step s
fs a
value
case Step s b
res of
FL.Done b
fb -> b -> m (SessionState t m k (SessionEntry AbsTime s) b)
forall (m :: * -> *) (t :: (* -> *) -> * -> *) b (m :: * -> *).
(Monad m, IsStream t) =>
b -> m (SessionState t m k (SessionEntry AbsTime s) b)
done b
fb
FL.Partial s
fs1 -> s -> m (SessionState t m k (SessionEntry AbsTime s) b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
IsStream t =>
s -> m (SessionState t m k (SessionEntry AbsTime s) b)
partial s
fs1
sstep sessionState :: SessionState t m k (SessionEntry AbsTime s) b
sessionState@SessionState{t m (k, b)
Int
Map k (SessionEntry AbsTime s)
Heap (Entry AbsTime k)
AbsTime
sessionOutputStream :: t m (k, b)
sessionKeyValueMap :: Map k (SessionEntry AbsTime s)
sessionTimerHeap :: Heap (Entry AbsTime k)
sessionCount :: Int
sessionEventTime :: AbsTime
sessionCurTime :: AbsTime
sessionOutputStream :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> t m (k, b)
sessionKeyValueMap :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> Map k a
sessionTimerHeap :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> Heap (Entry AbsTime k)
sessionCount :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> Int
sessionEventTime :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> AbsTime
sessionCurTime :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> AbsTime
..} Maybe (AbsTime, (k, a))
Nothing =
let curTime :: AbsTime
curTime = AbsTime -> RelTime -> AbsTime
addToAbsTime AbsTime
sessionCurTime RelTime
tickMs
in SessionState t m k (SessionEntry AbsTime s) b
-> AbsTime -> m (SessionState t m k (SessionEntry AbsTime s) b)
forall k (t :: (* -> *) -> * -> *) (t :: (* -> *) -> * -> *)
(m :: * -> *) b (m :: * -> *).
(Ord k, IsStream t) =>
SessionState t m k (SessionEntry AbsTime s) b
-> AbsTime -> m (SessionState t m k (SessionEntry AbsTime s) b)
ejectExpired SessionState t m k (SessionEntry AbsTime s) b
sessionState AbsTime
curTime
flush :: SessionState t m k (SessionEntry a s) b
-> m (SessionState t m k (SessionEntry a s) b)
flush session :: SessionState t m k (SessionEntry a s) b
session@SessionState{t m (k, b)
Int
Map k (SessionEntry a s)
Heap (Entry AbsTime k)
AbsTime
sessionOutputStream :: t m (k, b)
sessionKeyValueMap :: Map k (SessionEntry a s)
sessionTimerHeap :: Heap (Entry AbsTime k)
sessionCount :: Int
sessionEventTime :: AbsTime
sessionCurTime :: AbsTime
sessionOutputStream :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> t m (k, b)
sessionKeyValueMap :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> Map k a
sessionTimerHeap :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> Heap (Entry AbsTime k)
sessionCount :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> Int
sessionEventTime :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> AbsTime
sessionCurTime :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> AbsTime
..} = do
(Heap (Entry AbsTime k)
hp', Map k (SessionEntry a s)
mp', t m (k, b)
out, Int
count) <-
(Heap (Entry AbsTime k), Map k (SessionEntry a s), t m (k, b), Int)
-> m (Heap (Entry AbsTime k), Map k (SessionEntry a s), t m (k, b),
Int)
forall k d (t :: (* -> *) -> * -> *) p a (m :: * -> *).
(Ord k, Num d, IsStream t) =>
(Heap (Entry p k), Map k (SessionEntry a s), t m (k, b), d)
-> m (Heap (Entry p k), Map k (SessionEntry a s), t m (k, b), d)
ejectAll
( Heap (Entry AbsTime k)
sessionTimerHeap
, Map k (SessionEntry a s)
sessionKeyValueMap
, t m (k, b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
IsStream.nil
, Int
sessionCount
)
SessionState t m k (SessionEntry a s) b
-> m (SessionState t m k (SessionEntry a s) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (SessionState t m k (SessionEntry a s) b
-> m (SessionState t m k (SessionEntry a s) b))
-> SessionState t m k (SessionEntry a s) b
-> m (SessionState t m k (SessionEntry a s) b)
forall a b. (a -> b) -> a -> b
$ SessionState t m k (SessionEntry a s) b
session
{ sessionCount :: Int
sessionCount = Int
count
, sessionTimerHeap :: Heap (Entry AbsTime k)
sessionTimerHeap = Heap (Entry AbsTime k)
hp'
, sessionKeyValueMap :: Map k (SessionEntry a s)
sessionKeyValueMap = Map k (SessionEntry a s)
mp'
, sessionOutputStream :: t m (k, b)
sessionOutputStream = t m (k, b)
out
}
ejectEntry :: a
-> Map k a
-> t m (k, b)
-> d
-> s
-> k
-> m (a, Map k a, t m (k, b), d)
ejectEntry a
hp Map k a
mp t m (k, b)
out d
cnt s
acc k
key = do
b
sess <- s -> m b
extract s
acc
let out1 :: t m (k, b)
out1 = (k
key, b
sess) (k, b) -> t m (k, b) -> t m (k, b)
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
`cons` t m (k, b)
out
let mp1 :: Map k a
mp1 = k -> Map k a -> Map k a
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete k
key Map k a
mp
(a, Map k a, t m (k, b), d) -> m (a, Map k a, t m (k, b), d)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
hp, Map k a
mp1, t m (k, b)
out1, d
cnt d -> d -> d
forall a. Num a => a -> a -> a
- d
1)
ejectAll :: (Heap (Entry p k), Map k (SessionEntry a s), t m (k, b), d)
-> m (Heap (Entry p k), Map k (SessionEntry a s), t m (k, b), d)
ejectAll (Heap (Entry p k)
hp, Map k (SessionEntry a s)
mp, t m (k, b)
out, !d
cnt) = do
let hres :: Maybe (Entry p k, Heap (Entry p k))
hres = Heap (Entry p k) -> Maybe (Entry p k, Heap (Entry p k))
forall a. Heap a -> Maybe (a, Heap a)
H.uncons Heap (Entry p k)
hp
case Maybe (Entry p k, Heap (Entry p k))
hres of
Just (Entry p
_ k
key, Heap (Entry p k)
hp1) -> do
(Heap (Entry p k), Map k (SessionEntry a s), t m (k, b), d)
r <- case k -> Map k (SessionEntry a s) -> Maybe (SessionEntry a s)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup k
key Map k (SessionEntry a s)
mp of
Maybe (SessionEntry a s)
Nothing -> (Heap (Entry p k), Map k (SessionEntry a s), t m (k, b), d)
-> m (Heap (Entry p k), Map k (SessionEntry a s), t m (k, b), d)
forall (m :: * -> *) a. Monad m => a -> m a
return (Heap (Entry p k)
hp1, Map k (SessionEntry a s)
mp, t m (k, b)
out, d
cnt)
Just SessionEntry a s
ZombieSession ->
(Heap (Entry p k), Map k (SessionEntry a s), t m (k, b), d)
-> m (Heap (Entry p k), Map k (SessionEntry a s), t m (k, b), d)
forall (m :: * -> *) a. Monad m => a -> m a
return (Heap (Entry p k)
hp1, k -> Map k (SessionEntry a s) -> Map k (SessionEntry a s)
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete k
key Map k (SessionEntry a s)
mp, t m (k, b)
out, d
cnt)
Just (LiveSession a
_ s
acc) ->
Heap (Entry p k)
-> Map k (SessionEntry a s)
-> t m (k, b)
-> d
-> s
-> k
-> m (Heap (Entry p k), Map k (SessionEntry a s), t m (k, b), d)
forall d (t :: (* -> *) -> * -> *) k a a (m :: * -> *).
(Num d, IsStream t, Ord k) =>
a
-> Map k a
-> t m (k, b)
-> d
-> s
-> k
-> m (a, Map k a, t m (k, b), d)
ejectEntry Heap (Entry p k)
hp1 Map k (SessionEntry a s)
mp t m (k, b)
out d
cnt s
acc k
key
(Heap (Entry p k), Map k (SessionEntry a s), t m (k, b), d)
-> m (Heap (Entry p k), Map k (SessionEntry a s), t m (k, b), d)
ejectAll (Heap (Entry p k), Map k (SessionEntry a s), t m (k, b), d)
r
Maybe (Entry p k, Heap (Entry p k))
Nothing -> do
Bool -> m () -> m ()
forall a. HasCallStack => Bool -> a -> a
assert (Map k (SessionEntry a s) -> Bool
forall k a. Map k a -> Bool
Map.null Map k (SessionEntry a s)
mp) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
(Heap (Entry p k), Map k (SessionEntry a s), t m (k, b), d)
-> m (Heap (Entry p k), Map k (SessionEntry a s), t m (k, b), d)
forall (m :: * -> *) a. Monad m => a -> m a
return (Heap (Entry p k)
hp, Map k (SessionEntry a s)
mp, t m (k, b)
out, d
cnt)
ejectOne :: (Heap (Entry a k), Map k (SessionEntry a s), t m (k, b), d)
-> m (Heap (Entry a k), Map k (SessionEntry a s), t m (k, b), d)
ejectOne (Heap (Entry a k)
hp, Map k (SessionEntry a s)
mp, t m (k, b)
out, !d
cnt) = do
let hres :: Maybe (Entry a k, Heap (Entry a k))
hres = Heap (Entry a k) -> Maybe (Entry a k, Heap (Entry a k))
forall a. Heap a -> Maybe (a, Heap a)
H.uncons Heap (Entry a k)
hp
case Maybe (Entry a k, Heap (Entry a k))
hres of
Just (Entry a
expiry k
key, Heap (Entry a k)
hp1) ->
case k -> Map k (SessionEntry a s) -> Maybe (SessionEntry a s)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup k
key Map k (SessionEntry a s)
mp of
Maybe (SessionEntry a s)
Nothing -> (Heap (Entry a k), Map k (SessionEntry a s), t m (k, b), d)
-> m (Heap (Entry a k), Map k (SessionEntry a s), t m (k, b), d)
ejectOne (Heap (Entry a k)
hp1, Map k (SessionEntry a s)
mp, t m (k, b)
out, d
cnt)
Just SessionEntry a s
ZombieSession ->
(Heap (Entry a k), Map k (SessionEntry a s), t m (k, b), d)
-> m (Heap (Entry a k), Map k (SessionEntry a s), t m (k, b), d)
ejectOne (Heap (Entry a k)
hp1, k -> Map k (SessionEntry a s) -> Map k (SessionEntry a s)
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete k
key Map k (SessionEntry a s)
mp, t m (k, b)
out, d
cnt)
Just (LiveSession a
expiry1 s
acc) -> do
if Bool -> Bool
not Bool
reset Bool -> Bool -> Bool
|| a
expiry1 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
expiry
then Heap (Entry a k)
-> Map k (SessionEntry a s)
-> t m (k, b)
-> d
-> s
-> k
-> m (Heap (Entry a k), Map k (SessionEntry a s), t m (k, b), d)
forall d (t :: (* -> *) -> * -> *) k a a (m :: * -> *).
(Num d, IsStream t, Ord k) =>
a
-> Map k a
-> t m (k, b)
-> d
-> s
-> k
-> m (a, Map k a, t m (k, b), d)
ejectEntry Heap (Entry a k)
hp1 Map k (SessionEntry a s)
mp t m (k, b)
out d
cnt s
acc k
key
else
let hp2 :: Heap (Entry a k)
hp2 = Entry a k -> Heap (Entry a k) -> Heap (Entry a k)
forall a. Ord a => a -> Heap a -> Heap a
H.insert (a -> k -> Entry a k
forall p a. p -> a -> Entry p a
Entry a
expiry1 k
key) Heap (Entry a k)
hp1
in (Heap (Entry a k), Map k (SessionEntry a s), t m (k, b), d)
-> m (Heap (Entry a k), Map k (SessionEntry a s), t m (k, b), d)
ejectOne (Heap (Entry a k)
hp2, Map k (SessionEntry a s)
mp, t m (k, b)
out, d
cnt)
Maybe (Entry a k, Heap (Entry a k))
Nothing -> do
Bool -> m () -> m ()
forall a. HasCallStack => Bool -> a -> a
assert (Map k (SessionEntry a s) -> Bool
forall k a. Map k a -> Bool
Map.null Map k (SessionEntry a s)
mp) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
(Heap (Entry a k), Map k (SessionEntry a s), t m (k, b), d)
-> m (Heap (Entry a k), Map k (SessionEntry a s), t m (k, b), d)
forall (m :: * -> *) a. Monad m => a -> m a
return (Heap (Entry a k)
hp, Map k (SessionEntry a s)
mp, t m (k, b)
out, d
cnt)
ejectExpired :: SessionState t m k (SessionEntry AbsTime s) b
-> AbsTime -> m (SessionState t m k (SessionEntry AbsTime s) b)
ejectExpired session :: SessionState t m k (SessionEntry AbsTime s) b
session@SessionState{t m (k, b)
Int
Map k (SessionEntry AbsTime s)
Heap (Entry AbsTime k)
AbsTime
sessionOutputStream :: t m (k, b)
sessionKeyValueMap :: Map k (SessionEntry AbsTime s)
sessionTimerHeap :: Heap (Entry AbsTime k)
sessionCount :: Int
sessionEventTime :: AbsTime
sessionCurTime :: AbsTime
sessionOutputStream :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> t m (k, b)
sessionKeyValueMap :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> Map k a
sessionTimerHeap :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> Heap (Entry AbsTime k)
sessionCount :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> Int
sessionEventTime :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> AbsTime
sessionCurTime :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
SessionState t m k a b -> AbsTime
..} AbsTime
curTime = do
(Heap (Entry AbsTime k)
hp', Map k (SessionEntry AbsTime s)
mp', t m (k, b)
out, Int
count) <-
Heap (Entry AbsTime k)
-> Map k (SessionEntry AbsTime s)
-> t m (k, b)
-> Int
-> m (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
forall k (t :: (* -> *) -> * -> *) (m :: * -> *).
(Ord k, IsStream t) =>
Heap (Entry AbsTime k)
-> Map k (SessionEntry AbsTime s)
-> t m (k, b)
-> Int
-> m (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
ejectLoop
Heap (Entry AbsTime k)
sessionTimerHeap Map k (SessionEntry AbsTime s)
sessionKeyValueMap t m (k, b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
IsStream.nil Int
sessionCount
SessionState t m k (SessionEntry AbsTime s) b
-> m (SessionState t m k (SessionEntry AbsTime s) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (SessionState t m k (SessionEntry AbsTime s) b
-> m (SessionState t m k (SessionEntry AbsTime s) b))
-> SessionState t m k (SessionEntry AbsTime s) b
-> m (SessionState t m k (SessionEntry AbsTime s) b)
forall a b. (a -> b) -> a -> b
$ SessionState t m k (SessionEntry AbsTime s) b
session
{ sessionCurTime :: AbsTime
sessionCurTime = AbsTime
curTime
, sessionCount :: Int
sessionCount = Int
count
, sessionTimerHeap :: Heap (Entry AbsTime k)
sessionTimerHeap = Heap (Entry AbsTime k)
hp'
, sessionKeyValueMap :: Map k (SessionEntry AbsTime s)
sessionKeyValueMap = Map k (SessionEntry AbsTime s)
mp'
, sessionOutputStream :: t m (k, b)
sessionOutputStream = t m (k, b)
out
}
where
ejectLoop :: Heap (Entry AbsTime k)
-> Map k (SessionEntry AbsTime s)
-> t m (k, b)
-> Int
-> m (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
ejectLoop Heap (Entry AbsTime k)
hp Map k (SessionEntry AbsTime s)
mp t m (k, b)
out !Int
cnt = do
let hres :: Maybe (Entry AbsTime k, Heap (Entry AbsTime k))
hres = Heap (Entry AbsTime k)
-> Maybe (Entry AbsTime k, Heap (Entry AbsTime k))
forall a. Heap a -> Maybe (a, Heap a)
H.uncons Heap (Entry AbsTime k)
hp
case Maybe (Entry AbsTime k, Heap (Entry AbsTime k))
hres of
Just (Entry AbsTime
expiry k
key, Heap (Entry AbsTime k)
hp1) -> do
(Bool
eject, Bool
force) <-
if AbsTime
curTime AbsTime -> AbsTime -> Bool
forall a. Ord a => a -> a -> Bool
>= AbsTime
expiry
then (Bool, Bool) -> m (Bool, Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
True, Bool
False)
else do
Bool
r <- Int -> m Bool
ejectPred Int
cnt
(Bool, Bool) -> m (Bool, Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
r, Bool
r)
if Bool
eject
then
case k
-> Map k (SessionEntry AbsTime s) -> Maybe (SessionEntry AbsTime s)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup k
key Map k (SessionEntry AbsTime s)
mp of
Maybe (SessionEntry AbsTime s)
Nothing -> Heap (Entry AbsTime k)
-> Map k (SessionEntry AbsTime s)
-> t m (k, b)
-> Int
-> m (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
ejectLoop Heap (Entry AbsTime k)
hp1 Map k (SessionEntry AbsTime s)
mp t m (k, b)
out Int
cnt
Just SessionEntry AbsTime s
ZombieSession ->
Heap (Entry AbsTime k)
-> Map k (SessionEntry AbsTime s)
-> t m (k, b)
-> Int
-> m (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
ejectLoop Heap (Entry AbsTime k)
hp1 (k
-> Map k (SessionEntry AbsTime s) -> Map k (SessionEntry AbsTime s)
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete k
key Map k (SessionEntry AbsTime s)
mp) t m (k, b)
out Int
cnt
Just (LiveSession AbsTime
expiry1 s
acc) -> do
if AbsTime
expiry1 AbsTime -> AbsTime -> Bool
forall a. Ord a => a -> a -> Bool
<= AbsTime
curTime Bool -> Bool -> Bool
|| Bool -> Bool
not Bool
reset Bool -> Bool -> Bool
|| Bool
force
then do
(Heap (Entry AbsTime k)
hp2,Map k (SessionEntry AbsTime s)
mp1,t m (k, b)
out1,Int
cnt1) <-
Heap (Entry AbsTime k)
-> Map k (SessionEntry AbsTime s)
-> t m (k, b)
-> Int
-> s
-> k
-> m (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
forall d (t :: (* -> *) -> * -> *) k a a (m :: * -> *).
(Num d, IsStream t, Ord k) =>
a
-> Map k a
-> t m (k, b)
-> d
-> s
-> k
-> m (a, Map k a, t m (k, b), d)
ejectEntry Heap (Entry AbsTime k)
hp1 Map k (SessionEntry AbsTime s)
mp t m (k, b)
out Int
cnt s
acc k
key
Heap (Entry AbsTime k)
-> Map k (SessionEntry AbsTime s)
-> t m (k, b)
-> Int
-> m (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
ejectLoop Heap (Entry AbsTime k)
hp2 Map k (SessionEntry AbsTime s)
mp1 t m (k, b)
out1 Int
cnt1
else
let hp2 :: Heap (Entry AbsTime k)
hp2 = Entry AbsTime k -> Heap (Entry AbsTime k) -> Heap (Entry AbsTime k)
forall a. Ord a => a -> Heap a -> Heap a
H.insert (AbsTime -> k -> Entry AbsTime k
forall p a. p -> a -> Entry p a
Entry AbsTime
expiry1 k
key) Heap (Entry AbsTime k)
hp1
in Heap (Entry AbsTime k)
-> Map k (SessionEntry AbsTime s)
-> t m (k, b)
-> Int
-> m (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
ejectLoop Heap (Entry AbsTime k)
hp2 Map k (SessionEntry AbsTime s)
mp t m (k, b)
out Int
cnt
else (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
-> m (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Heap (Entry AbsTime k)
hp, Map k (SessionEntry AbsTime s)
mp, t m (k, b)
out, Int
cnt)
Maybe (Entry AbsTime k, Heap (Entry AbsTime k))
Nothing -> do
Bool -> m () -> m ()
forall a. HasCallStack => Bool -> a -> a
assert (Map k (SessionEntry AbsTime s) -> Bool
forall k a. Map k a -> Bool
Map.null Map k (SessionEntry AbsTime s)
mp) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
(Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
-> m (Heap (Entry AbsTime k), Map k (SessionEntry AbsTime s),
t m (k, b), Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Heap (Entry AbsTime k)
hp, Map k (SessionEntry AbsTime s)
mp, t m (k, b)
out, Int
cnt)
stream :: t m (Maybe (AbsTime, (k, a)))
stream = ((AbsTime, (k, a)) -> Maybe (AbsTime, (k, a)))
-> t m (AbsTime, (k, a)) -> t m (Maybe (AbsTime, (k, a)))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> b) -> t m a -> t m b
map (AbsTime, (k, a)) -> Maybe (AbsTime, (k, a))
forall a. a -> Maybe a
Just t m (AbsTime, (k, a))
str t m (Maybe (AbsTime, (k, a)))
-> t m (Maybe (AbsTime, (k, a))) -> t m (Maybe (AbsTime, (k, a)))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
t m a -> t m a -> t m a
`parallelFst` m (Maybe (AbsTime, (k, a))) -> t m (Maybe (AbsTime, (k, a)))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
m a -> t m a
repeatM m (Maybe (AbsTime, (k, a)))
forall a. m (Maybe a)
timer
timer :: m (Maybe a)
timer = do
IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> IO ()
threadDelay (Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round (Double -> Int) -> Double -> Int
forall a b. (a -> b) -> a -> b
$ Double
tick Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1000000)
Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
{-# INLINE classifyKeepAliveSessions #-}
classifyKeepAliveSessions ::
(IsStream t, MonadAsync m, Ord k)
=> (Int -> m Bool)
-> Double
-> Fold m a b
-> t m (AbsTime, (k, a))
-> t m (k, b)
classifyKeepAliveSessions :: (Int -> m Bool)
-> Double -> Fold m a b -> t m (AbsTime, (k, a)) -> t m (k, b)
classifyKeepAliveSessions = Double
-> Bool
-> (Int -> m Bool)
-> Double
-> Fold m a b
-> t m (AbsTime, (k, a))
-> t m (k, b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
(IsStream t, MonadAsync m, Ord k) =>
Double
-> Bool
-> (Int -> m Bool)
-> Double
-> Fold m a b
-> t m (AbsTime, (k, a))
-> t m (k, b)
classifySessionsBy Double
1 Bool
True
{-# INLINE classifySessionsOf #-}
classifySessionsOf ::
(IsStream t, MonadAsync m, Ord k)
=> (Int -> m Bool)
-> Double
-> Fold m a b
-> t m (AbsTime, (k, a))
-> t m (k, b)
classifySessionsOf :: (Int -> m Bool)
-> Double -> Fold m a b -> t m (AbsTime, (k, a)) -> t m (k, b)
classifySessionsOf = Double
-> Bool
-> (Int -> m Bool)
-> Double
-> Fold m a b
-> t m (AbsTime, (k, a))
-> t m (k, b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) k a b.
(IsStream t, MonadAsync m, Ord k) =>
Double
-> Bool
-> (Int -> m Bool)
-> Double
-> Fold m a b
-> t m (AbsTime, (k, a))
-> t m (k, b)
classifySessionsBy Double
1 Bool
False
{-# INLINE splitInnerBy #-}
splitInnerBy
:: (IsStream t, Monad m)
=> (f a -> m (f a, Maybe (f a)))
-> (f a -> f a -> m (f a))
-> t m (f a)
-> t m (f a)
splitInnerBy :: (f a -> m (f a, Maybe (f a)))
-> (f a -> f a -> m (f a)) -> t m (f a) -> t m (f a)
splitInnerBy f a -> m (f a, Maybe (f a))
splitter f a -> f a -> m (f a)
joiner t m (f a)
xs =
Stream m (f a) -> t m (f a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m (f a) -> t m (f a)) -> Stream m (f a) -> t m (f a)
forall a b. (a -> b) -> a -> b
$ (f a -> m (f a, Maybe (f a)))
-> (f a -> f a -> m (f a)) -> Stream m (f a) -> Stream m (f a)
forall (m :: * -> *) (f :: * -> *) a.
Monad m =>
(f a -> m (f a, Maybe (f a)))
-> (f a -> f a -> m (f a)) -> Stream m (f a) -> Stream m (f a)
D.splitInnerBy f a -> m (f a, Maybe (f a))
splitter f a -> f a -> m (f a)
joiner (Stream m (f a) -> Stream m (f a))
-> Stream m (f a) -> Stream m (f a)
forall a b. (a -> b) -> a -> b
$ t m (f a) -> Stream m (f a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m (f a)
xs
{-# INLINE splitInnerBySuffix #-}
splitInnerBySuffix
:: (IsStream t, Monad m, Eq (f a), Monoid (f a))
=> (f a -> m (f a, Maybe (f a)))
-> (f a -> f a -> m (f a))
-> t m (f a)
-> t m (f a)
splitInnerBySuffix :: (f a -> m (f a, Maybe (f a)))
-> (f a -> f a -> m (f a)) -> t m (f a) -> t m (f a)
splitInnerBySuffix f a -> m (f a, Maybe (f a))
splitter f a -> f a -> m (f a)
joiner t m (f a)
xs =
Stream m (f a) -> t m (f a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Stream m (f a) -> t m (f a)) -> Stream m (f a) -> t m (f a)
forall a b. (a -> b) -> a -> b
$ (f a -> m (f a, Maybe (f a)))
-> (f a -> f a -> m (f a)) -> Stream m (f a) -> Stream m (f a)
forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Eq (f a), Monoid (f a)) =>
(f a -> m (f a, Maybe (f a)))
-> (f a -> f a -> m (f a)) -> Stream m (f a) -> Stream m (f a)
D.splitInnerBySuffix f a -> m (f a, Maybe (f a))
splitter f a -> f a -> m (f a)
joiner (Stream m (f a) -> Stream m (f a))
-> Stream m (f a) -> Stream m (f a)
forall a b. (a -> b) -> a -> b
$ t m (f a) -> Stream m (f a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m (f a)
xs