module Streamly.Internal.Data.Fold.Container
(
toSet
, toIntSet
, countDistinct
, countDistinctInt
, nub
, nubInt
, frequency
, demuxToContainer
, demuxToContainerIO
, demuxToMap
, demuxToMapIO
, demuxKvToContainer
, demuxKvToMap
, demuxGeneric
, demux
, demuxGenericIO
, demuxIO
, kvToMap
, toContainer
, toContainerIO
, toMap
, toMapIO
, classifyGeneric
, classify
, classifyGenericIO
, classifyIO
)
where
#include "inline.hs"
#include "ArrayMacros.h"
import Control.Monad.IO.Class (MonadIO(..))
import Data.IORef (newIORef, readIORef, writeIORef)
import Data.Map.Strict (Map)
import Data.IntSet (IntSet)
import Data.Set (Set)
import Streamly.Internal.Data.IsMap (IsMap(..))
import Streamly.Internal.Data.Tuple.Strict (Tuple'(..), Tuple3'(..))
import qualified Data.IntSet as IntSet
import qualified Data.Set as Set
import qualified Streamly.Internal.Data.IsMap as IsMap
import Prelude hiding (Foldable(..))
import Streamly.Internal.Data.Fold.Type
import Streamly.Internal.Data.Fold.Combinators
{-# INLINE toSet #-}
toSet :: (Monad m, Ord a) => Fold m a (Set a)
toSet :: forall (m :: * -> *) a. (Monad m, Ord a) => Fold m a (Set a)
toSet = forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Ord a => a -> Set a -> Set a
Set.insert) forall a. Set a
Set.empty
{-# INLINE toIntSet #-}
toIntSet :: Monad m => Fold m Int IntSet
toIntSet :: forall (m :: * -> *). Monad m => Fold m Int IntSet
toIntSet = forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' (forall a b c. (a -> b -> c) -> b -> a -> c
flip Int -> IntSet -> IntSet
IntSet.insert) IntSet
IntSet.empty
{-# INLINE nub #-}
nub :: (Monad m, Ord a) => Fold m a (Maybe a)
nub :: forall (m :: * -> *) a. (Monad m, Ord a) => Fold m a (Maybe a)
nub = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Tuple' Set a
_ Maybe a
x) -> Maybe a
x) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' forall {a} {b}.
Ord a =>
Tuple' (Set a) b -> a -> Tuple' (Set a) (Maybe a)
step forall {a} {a}. Tuple' (Set a) (Maybe a)
initial
where
initial :: Tuple' (Set a) (Maybe a)
initial = forall a b. a -> b -> Tuple' a b
Tuple' forall a. Set a
Set.empty forall a. Maybe a
Nothing
step :: Tuple' (Set a) b -> a -> Tuple' (Set a) (Maybe a)
step (Tuple' Set a
set b
_) a
x =
if forall a. Ord a => a -> Set a -> Bool
Set.member a
x Set a
set
then forall a b. a -> b -> Tuple' a b
Tuple' Set a
set forall a. Maybe a
Nothing
else forall a b. a -> b -> Tuple' a b
Tuple' (forall a. Ord a => a -> Set a -> Set a
Set.insert a
x Set a
set) (forall a. a -> Maybe a
Just a
x)
{-# INLINE nubInt #-}
nubInt :: Monad m => Fold m Int (Maybe Int)
nubInt :: forall (m :: * -> *). Monad m => Fold m Int (Maybe Int)
nubInt = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Tuple' IntSet
_ Maybe Int
x) -> Maybe Int
x) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' forall {b}. Tuple' IntSet b -> Int -> Tuple' IntSet (Maybe Int)
step forall {a}. Tuple' IntSet (Maybe a)
initial
where
initial :: Tuple' IntSet (Maybe a)
initial = forall a b. a -> b -> Tuple' a b
Tuple' IntSet
IntSet.empty forall a. Maybe a
Nothing
step :: Tuple' IntSet b -> Int -> Tuple' IntSet (Maybe Int)
step (Tuple' IntSet
set b
_) Int
x =
if Int -> IntSet -> Bool
IntSet.member Int
x IntSet
set
then forall a b. a -> b -> Tuple' a b
Tuple' IntSet
set forall a. Maybe a
Nothing
else forall a b. a -> b -> Tuple' a b
Tuple' (Int -> IntSet -> IntSet
IntSet.insert Int
x IntSet
set) (forall a. a -> Maybe a
Just Int
x)
{-# INLINE countDistinct #-}
countDistinct :: (Monad m, Ord a) => Fold m a Int
countDistinct :: forall (m :: * -> *) a. (Monad m, Ord a) => Fold m a Int
countDistinct = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Set a -> Int
Set.size forall (m :: * -> *) a. (Monad m, Ord a) => Fold m a (Set a)
toSet
{-# INLINE countDistinctInt #-}
countDistinctInt :: Monad m => Fold m Int Int
countDistinctInt :: forall (m :: * -> *). Monad m => Fold m Int Int
countDistinctInt = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap IntSet -> Int
IntSet.size forall (m :: * -> *). Monad m => Fold m Int IntSet
toIntSet
{-# INLINE demuxGeneric #-}
demuxGeneric :: (Monad m, IsMap f, Traversable f) =>
(a -> Key f)
-> (a -> m (Fold m a b))
-> Fold m a (m (f b), Maybe (Key f, b))
demuxGeneric :: forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, IsMap f, Traversable f) =>
(a -> Key f)
-> (a -> m (Fold m a b)) -> Fold m a (m (f b), Maybe (Key f, b))
demuxGeneric a -> Key f
getKey a -> m (Fold m a b)
getFold =
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Fold m a b
Fold (\Tuple' (f (Fold m a b)) (Maybe (Key f, b))
s a
a -> forall s b. s -> Step s b
Partial forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {b}.
Tuple' (f (Fold m a b)) b
-> a -> m (Tuple' (f (Fold m a b)) (Maybe (Key f, b)))
step Tuple' (f (Fold m a b)) (Maybe (Key f, b))
s a
a) (forall s b. s -> Step s b
Partial forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {a} {a}. m (Tuple' (f a) (Maybe a))
initial) forall {t :: * -> *} {m :: * -> *} {m :: * -> *} {a} {b} {b}.
(Traversable t, Monad m, Monad m) =>
Tuple' (t (Fold m a b)) b -> m (m (t b), b)
extract forall {t :: * -> *} {m :: * -> *} {m :: * -> *} {a} {b} {b}.
(Traversable t, Monad m, Monad m) =>
Tuple' (t (Fold m a b)) b -> m (m (t b), b)
final
where
initial :: m (Tuple' (f a) (Maybe a))
initial = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> Tuple' a b
Tuple' forall (f :: * -> *) a. IsMap f => f a
IsMap.mapEmpty forall a. Maybe a
Nothing
{-# INLINE runFold #-}
runFold :: f (Fold m a b)
-> Fold m a b
-> (Key f, a)
-> m (Tuple' (f (Fold m a b)) (Maybe (Key f, b)))
runFold f (Fold m a b)
kv (Fold s -> a -> m (Step s b)
step1 m (Step s b)
initial1 s -> m b
extract1 s -> m b
final1) (Key f
k, a
a) = do
Step s b
res <- m (Step s b)
initial1
case Step s b
res of
Partial s
s -> do
Step s b
res1 <- s -> a -> m (Step s b)
step1 s
s a
a
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ case Step s b
res1 of
Partial s
_ ->
let fld :: Fold m a b
fld = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step1 (forall (m :: * -> *) a. Monad m => a -> m a
return Step s b
res1) s -> m b
extract1 s -> m b
final1
in forall a b. a -> b -> Tuple' a b
Tuple' (forall (f :: * -> *) a. IsMap f => Key f -> a -> f a -> f a
IsMap.mapInsert Key f
k Fold m a b
fld f (Fold m a b)
kv) forall a. Maybe a
Nothing
Done b
b -> forall a b. a -> b -> Tuple' a b
Tuple' (forall (f :: * -> *) a. IsMap f => Key f -> f a -> f a
IsMap.mapDelete Key f
k f (Fold m a b)
kv) (forall a. a -> Maybe a
Just (Key f
k, b
b))
Done b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> Tuple' a b
Tuple' f (Fold m a b)
kv (forall a. a -> Maybe a
Just (Key f
k, b
b))
step :: Tuple' (f (Fold m a b)) b
-> a -> m (Tuple' (f (Fold m a b)) (Maybe (Key f, b)))
step (Tuple' f (Fold m a b)
kv b
_) a
a = do
let k :: Key f
k = a -> Key f
getKey a
a
case forall (f :: * -> *) a. IsMap f => Key f -> f a -> Maybe a
IsMap.mapLookup Key f
k f (Fold m a b)
kv of
Maybe (Fold m a b)
Nothing -> do
Fold m a b
fld <- a -> m (Fold m a b)
getFold a
a
forall {m :: * -> *} {f :: * -> *} {a} {b}.
(IsMap f, Monad m) =>
f (Fold m a b)
-> Fold m a b
-> (Key f, a)
-> m (Tuple' (f (Fold m a b)) (Maybe (Key f, b)))
runFold f (Fold m a b)
kv Fold m a b
fld (Key f
k, a
a)
Just Fold m a b
f -> forall {m :: * -> *} {f :: * -> *} {a} {b}.
(IsMap f, Monad m) =>
f (Fold m a b)
-> Fold m a b
-> (Key f, a)
-> m (Tuple' (f (Fold m a b)) (Maybe (Key f, b)))
runFold f (Fold m a b)
kv Fold m a b
f (Key f
k, a
a)
extract :: Tuple' (t (Fold m a b)) b -> m (m (t b), b)
extract (Tuple' t (Fold m a b)
kv b
x) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
Prelude.mapM forall {m :: * -> *} {a} {b}. Monad m => Fold m a b -> m b
f t (Fold m a b)
kv, b
x)
where
f :: Fold m a b -> m b
f (Fold s -> a -> m (Step s b)
_ m (Step s b)
i s -> m b
e s -> m b
_) = do
Step s b
r <- m (Step s b)
i
case Step s b
r of
Partial s
s -> s -> m b
e s
s
Step s b
_ -> forall a. HasCallStack => [Char] -> a
error [Char]
"demuxGeneric: unreachable code"
final :: Tuple' (t (Fold m a b)) b -> m (m (t b), b)
final (Tuple' t (Fold m a b)
kv b
x) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
Prelude.mapM forall {m :: * -> *} {a} {b}. Monad m => Fold m a b -> m b
f t (Fold m a b)
kv, b
x)
where
f :: Fold m a b -> m b
f (Fold s -> a -> m (Step s b)
_ m (Step s b)
i s -> m b
_ s -> m b
fin) = do
Step s b
r <- m (Step s b)
i
case Step s b
r of
Partial s
s -> s -> m b
fin s
s
Step s b
_ -> forall a. HasCallStack => [Char] -> a
error [Char]
"demuxGeneric: unreachable code"
{-# INLINE demux #-}
demux :: (Monad m, Ord k) =>
(a -> k)
-> (a -> m (Fold m a b))
-> Fold m a (m (Map k b), Maybe (k, b))
demux :: forall (m :: * -> *) k a b.
(Monad m, Ord k) =>
(a -> k)
-> (a -> m (Fold m a b)) -> Fold m a (m (Map k b), Maybe (k, b))
demux = forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, IsMap f, Traversable f) =>
(a -> Key f)
-> (a -> m (Fold m a b)) -> Fold m a (m (f b), Maybe (Key f, b))
demuxGeneric
{-# INLINE demuxGenericIO #-}
demuxGenericIO :: (MonadIO m, IsMap f, Traversable f) =>
(a -> Key f)
-> (a -> m (Fold m a b))
-> Fold m a (m (f b), Maybe (Key f, b))
demuxGenericIO :: forall (m :: * -> *) (f :: * -> *) a b.
(MonadIO m, IsMap f, Traversable f) =>
(a -> Key f)
-> (a -> m (Fold m a b)) -> Fold m a (m (f b), Maybe (Key f, b))
demuxGenericIO a -> Key f
getKey a -> m (Fold m a b)
getFold =
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Fold m a b
Fold (\Tuple' (f (IORef (Fold m a b))) (Maybe (Key f, b))
s a
a -> forall s b. s -> Step s b
Partial forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {b}.
Tuple' (f (IORef (Fold m a b))) b
-> a -> m (Tuple' (f (IORef (Fold m a b))) (Maybe (Key f, b)))
step Tuple' (f (IORef (Fold m a b))) (Maybe (Key f, b))
s a
a) (forall s b. s -> Step s b
Partial forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {a} {a}. m (Tuple' (f a) (Maybe a))
initial) forall {t :: * -> *} {m :: * -> *} {m :: * -> *} {a} {b} {b}.
(Traversable t, Monad m, MonadIO m) =>
Tuple' (t (IORef (Fold m a b))) b -> m (m (t b), b)
extract forall {t :: * -> *} {m :: * -> *} {m :: * -> *} {a} {b} {b}.
(Traversable t, Monad m, MonadIO m) =>
Tuple' (t (IORef (Fold m a b))) b -> m (m (t b), b)
final
where
initial :: m (Tuple' (f a) (Maybe a))
initial = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> Tuple' a b
Tuple' forall (f :: * -> *) a. IsMap f => f a
IsMap.mapEmpty forall a. Maybe a
Nothing
{-# INLINE initFold #-}
initFold :: f (IORef (Fold m a b))
-> Fold m a b
-> (Key f, a)
-> m (Tuple' (f (IORef (Fold m a b))) (Maybe (Key f, b)))
initFold f (IORef (Fold m a b))
kv (Fold s -> a -> m (Step s b)
step1 m (Step s b)
initial1 s -> m b
extract1 s -> m b
final1) (Key f
k, a
a) = do
Step s b
res <- m (Step s b)
initial1
case Step s b
res of
Partial s
s -> do
Step s b
res1 <- s -> a -> m (Step s b)
step1 s
s a
a
case Step s b
res1 of
Partial s
_ -> do
let fld :: Fold m a b
fld = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step1 (forall (m :: * -> *) a. Monad m => a -> m a
return Step s b
res1) s -> m b
extract1 s -> m b
final1
IORef (Fold m a b)
ref <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. a -> IO (IORef a)
newIORef Fold m a b
fld
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> Tuple' a b
Tuple' (forall (f :: * -> *) a. IsMap f => Key f -> a -> f a -> f a
IsMap.mapInsert Key f
k IORef (Fold m a b)
ref f (IORef (Fold m a b))
kv) forall a. Maybe a
Nothing
Done b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> Tuple' a b
Tuple' f (IORef (Fold m a b))
kv (forall a. a -> Maybe a
Just (Key f
k, b
b))
Done b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> Tuple' a b
Tuple' f (IORef (Fold m a b))
kv (forall a. a -> Maybe a
Just (Key f
k, b
b))
{-# INLINE runFold #-}
runFold :: f a
-> IORef (Fold m a b)
-> Fold m a b
-> (Key f, a)
-> m (Tuple' (f a) (Maybe (Key f, b)))
runFold f a
kv IORef (Fold m a b)
ref (Fold s -> a -> m (Step s b)
step1 m (Step s b)
initial1 s -> m b
extract1 s -> m b
final1) (Key f
k, a
a) = do
Step s b
res <- m (Step s b)
initial1
case Step s b
res of
Partial s
s -> do
Step s b
res1 <- s -> a -> m (Step s b)
step1 s
s a
a
case Step s b
res1 of
Partial s
_ -> do
let fld :: Fold m a b
fld = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step1 (forall (m :: * -> *) a. Monad m => a -> m a
return Step s b
res1) s -> m b
extract1 s -> m b
final1
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> a -> IO ()
writeIORef IORef (Fold m a b)
ref Fold m a b
fld
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> Tuple' a b
Tuple' f a
kv forall a. Maybe a
Nothing
Done b
b ->
let kv1 :: f a
kv1 = forall (f :: * -> *) a. IsMap f => Key f -> f a -> f a
IsMap.mapDelete Key f
k f a
kv
in forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> Tuple' a b
Tuple' f a
kv1 (forall a. a -> Maybe a
Just (Key f
k, b
b))
Done b
_ -> forall a. HasCallStack => [Char] -> a
error [Char]
"demuxGenericIO: unreachable"
step :: Tuple' (f (IORef (Fold m a b))) b
-> a -> m (Tuple' (f (IORef (Fold m a b))) (Maybe (Key f, b)))
step (Tuple' f (IORef (Fold m a b))
kv b
_) a
a = do
let k :: Key f
k = a -> Key f
getKey a
a
case forall (f :: * -> *) a. IsMap f => Key f -> f a -> Maybe a
IsMap.mapLookup Key f
k f (IORef (Fold m a b))
kv of
Maybe (IORef (Fold m a b))
Nothing -> do
Fold m a b
f <- a -> m (Fold m a b)
getFold a
a
forall {m :: * -> *} {f :: * -> *} {a} {b}.
(MonadIO m, IsMap f) =>
f (IORef (Fold m a b))
-> Fold m a b
-> (Key f, a)
-> m (Tuple' (f (IORef (Fold m a b))) (Maybe (Key f, b)))
initFold f (IORef (Fold m a b))
kv Fold m a b
f (Key f
k, a
a)
Just IORef (Fold m a b)
ref -> do
Fold m a b
f <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> IO a
readIORef IORef (Fold m a b)
ref
forall {m :: * -> *} {f :: * -> *} {a} {a} {b}.
(MonadIO m, IsMap f) =>
f a
-> IORef (Fold m a b)
-> Fold m a b
-> (Key f, a)
-> m (Tuple' (f a) (Maybe (Key f, b)))
runFold f (IORef (Fold m a b))
kv IORef (Fold m a b)
ref Fold m a b
f (Key f
k, a
a)
extract :: Tuple' (t (IORef (Fold m a b))) b -> m (m (t b), b)
extract (Tuple' t (IORef (Fold m a b))
kv b
x) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
Prelude.mapM forall {m :: * -> *} {a} {b}.
MonadIO m =>
IORef (Fold m a b) -> m b
f t (IORef (Fold m a b))
kv, b
x)
where
f :: IORef (Fold m a b) -> m b
f IORef (Fold m a b)
ref = do
Fold s -> a -> m (Step s b)
_ m (Step s b)
i s -> m b
e s -> m b
_ <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> IO a
readIORef IORef (Fold m a b)
ref
Step s b
r <- m (Step s b)
i
case Step s b
r of
Partial s
s -> s -> m b
e s
s
Step s b
_ -> forall a. HasCallStack => [Char] -> a
error [Char]
"demuxGenericIO: unreachable code"
final :: Tuple' (t (IORef (Fold m a b))) b -> m (m (t b), b)
final (Tuple' t (IORef (Fold m a b))
kv b
x) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
Prelude.mapM forall {m :: * -> *} {a} {b}.
MonadIO m =>
IORef (Fold m a b) -> m b
f t (IORef (Fold m a b))
kv, b
x)
where
f :: IORef (Fold m a b) -> m b
f IORef (Fold m a b)
ref = do
Fold s -> a -> m (Step s b)
_ m (Step s b)
i s -> m b
_ s -> m b
fin <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> IO a
readIORef IORef (Fold m a b)
ref
Step s b
r <- m (Step s b)
i
case Step s b
r of
Partial s
s -> s -> m b
fin s
s
Step s b
_ -> forall a. HasCallStack => [Char] -> a
error [Char]
"demuxGenericIO: unreachable code"
{-# INLINE demuxIO #-}
demuxIO :: (MonadIO m, Ord k) =>
(a -> k)
-> (a -> m (Fold m a b))
-> Fold m a (m (Map k b), Maybe (k, b))
demuxIO :: forall (m :: * -> *) k a b.
(MonadIO m, Ord k) =>
(a -> k)
-> (a -> m (Fold m a b)) -> Fold m a (m (Map k b), Maybe (k, b))
demuxIO = forall (m :: * -> *) (f :: * -> *) a b.
(MonadIO m, IsMap f, Traversable f) =>
(a -> Key f)
-> (a -> m (Fold m a b)) -> Fold m a (m (f b), Maybe (Key f, b))
demuxGenericIO
{-# INLINE kvToMapOverwriteGeneric #-}
kvToMapOverwriteGeneric :: (Monad m, IsMap f) => Fold m (Key f, a) (f a)
kvToMapOverwriteGeneric :: forall (m :: * -> *) (f :: * -> *) a.
(Monad m, IsMap f) =>
Fold m (Key f, a) (f a)
kvToMapOverwriteGeneric =
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' (\f a
kv (Key f
k, a
v) -> forall (f :: * -> *) a. IsMap f => Key f -> a -> f a -> f a
IsMap.mapInsert Key f
k a
v f a
kv) forall (f :: * -> *) a. IsMap f => f a
IsMap.mapEmpty
{-# INLINE demuxToContainer #-}
demuxToContainer :: (Monad m, IsMap f, Traversable f) =>
(a -> Key f) -> (a -> m (Fold m a b)) -> Fold m a (f b)
demuxToContainer :: forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, IsMap f, Traversable f) =>
(a -> Key f) -> (a -> m (Fold m a b)) -> Fold m a (f b)
demuxToContainer a -> Key f
getKey a -> m (Fold m a b)
getFold =
let
classifier :: Fold m a (m (f b), Maybe (Key f, b))
classifier = forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, IsMap f, Traversable f) =>
(a -> Key f)
-> (a -> m (Fold m a b)) -> Fold m a (m (f b), Maybe (Key f, b))
demuxGeneric a -> Key f
getKey a -> m (Fold m a b)
getFold
getMap :: Maybe (f (f a)) -> f (f a)
getMap Maybe (f (f a))
Nothing = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (f :: * -> *) a. IsMap f => f a
IsMap.mapEmpty
getMap (Just f (f a)
action) = f (f a)
action
aggregator :: Fold m (m (f a), Maybe (Key f, a)) (f a)
aggregator =
forall (m :: * -> *) a b c x.
Monad m =>
(a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
teeWith forall (f :: * -> *) a. IsMap f => f a -> f a -> f a
IsMap.mapUnion
(forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> Fold m a b -> Fold m a c
rmapM forall {f :: * -> *} {f :: * -> *} {a}.
(Applicative f, IsMap f) =>
Maybe (f (f a)) -> f (f a)
getMap forall a b. (a -> b) -> a -> b
$ forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
lmap forall a b. (a, b) -> a
fst forall (m :: * -> *) a. Monad m => Fold m a (Maybe a)
latest)
(forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
lmap forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Fold m (Maybe a) b
catMaybes forall (m :: * -> *) (f :: * -> *) a.
(Monad m, IsMap f) =>
Fold m (Key f, a) (f a)
kvToMapOverwriteGeneric)
in forall (m :: * -> *) a b c.
Monad m =>
Fold m a b -> Fold m b c -> Fold m a c
postscan Fold m a (m (f b), Maybe (Key f, b))
classifier forall {a}. Fold m (m (f a), Maybe (Key f, a)) (f a)
aggregator
{-# INLINE demuxToMap #-}
demuxToMap :: (Monad m, Ord k) =>
(a -> k) -> (a -> m (Fold m a b)) -> Fold m a (Map k b)
demuxToMap :: forall (m :: * -> *) k a b.
(Monad m, Ord k) =>
(a -> k) -> (a -> m (Fold m a b)) -> Fold m a (Map k b)
demuxToMap = forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, IsMap f, Traversable f) =>
(a -> Key f) -> (a -> m (Fold m a b)) -> Fold m a (f b)
demuxToContainer
{-# INLINE demuxToContainerIO #-}
demuxToContainerIO :: (MonadIO m, IsMap f, Traversable f) =>
(a -> Key f) -> (a -> m (Fold m a b)) -> Fold m a (f b)
demuxToContainerIO :: forall (m :: * -> *) (f :: * -> *) a b.
(MonadIO m, IsMap f, Traversable f) =>
(a -> Key f) -> (a -> m (Fold m a b)) -> Fold m a (f b)
demuxToContainerIO a -> Key f
getKey a -> m (Fold m a b)
getFold =
let
classifier :: Fold m a (m (f b), Maybe (Key f, b))
classifier = forall (m :: * -> *) (f :: * -> *) a b.
(MonadIO m, IsMap f, Traversable f) =>
(a -> Key f)
-> (a -> m (Fold m a b)) -> Fold m a (m (f b), Maybe (Key f, b))
demuxGenericIO a -> Key f
getKey a -> m (Fold m a b)
getFold
getMap :: Maybe (f (f a)) -> f (f a)
getMap Maybe (f (f a))
Nothing = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (f :: * -> *) a. IsMap f => f a
IsMap.mapEmpty
getMap (Just f (f a)
action) = f (f a)
action
aggregator :: Fold m (m (f a), Maybe (Key f, a)) (f a)
aggregator =
forall (m :: * -> *) a b c x.
Monad m =>
(a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
teeWith forall (f :: * -> *) a. IsMap f => f a -> f a -> f a
IsMap.mapUnion
(forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> Fold m a b -> Fold m a c
rmapM forall {f :: * -> *} {f :: * -> *} {a}.
(Applicative f, IsMap f) =>
Maybe (f (f a)) -> f (f a)
getMap forall a b. (a -> b) -> a -> b
$ forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
lmap forall a b. (a, b) -> a
fst forall (m :: * -> *) a. Monad m => Fold m a (Maybe a)
latest)
(forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
lmap forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Fold m (Maybe a) b
catMaybes forall (m :: * -> *) (f :: * -> *) a.
(Monad m, IsMap f) =>
Fold m (Key f, a) (f a)
kvToMapOverwriteGeneric)
in forall (m :: * -> *) a b c.
Monad m =>
Fold m a b -> Fold m b c -> Fold m a c
postscan Fold m a (m (f b), Maybe (Key f, b))
classifier forall {a}. Fold m (m (f a), Maybe (Key f, a)) (f a)
aggregator
{-# INLINE demuxToMapIO #-}
demuxToMapIO :: (MonadIO m, Ord k) =>
(a -> k) -> (a -> m (Fold m a b)) -> Fold m a (Map k b)
demuxToMapIO :: forall (m :: * -> *) k a b.
(MonadIO m, Ord k) =>
(a -> k) -> (a -> m (Fold m a b)) -> Fold m a (Map k b)
demuxToMapIO = forall (m :: * -> *) (f :: * -> *) a b.
(MonadIO m, IsMap f, Traversable f) =>
(a -> Key f) -> (a -> m (Fold m a b)) -> Fold m a (f b)
demuxToContainerIO
{-# INLINE demuxKvToContainer #-}
demuxKvToContainer :: (Monad m, IsMap f, Traversable f) =>
(Key f -> m (Fold m a b)) -> Fold m (Key f, a) (f b)
demuxKvToContainer :: forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, IsMap f, Traversable f) =>
(Key f -> m (Fold m a b)) -> Fold m (Key f, a) (f b)
demuxKvToContainer Key f -> m (Fold m a b)
f = forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, IsMap f, Traversable f) =>
(a -> Key f) -> (a -> m (Fold m a b)) -> Fold m a (f b)
demuxToContainer forall a b. (a, b) -> a
fst (\(Key f
k, a
_) -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
lmap forall a b. (a, b) -> b
snd) (Key f -> m (Fold m a b)
f Key f
k))
{-# INLINE demuxKvToMap #-}
demuxKvToMap :: (Monad m, Ord k) =>
(k -> m (Fold m a b)) -> Fold m (k, a) (Map k b)
demuxKvToMap :: forall (m :: * -> *) k a b.
(Monad m, Ord k) =>
(k -> m (Fold m a b)) -> Fold m (k, a) (Map k b)
demuxKvToMap = forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, IsMap f, Traversable f) =>
(Key f -> m (Fold m a b)) -> Fold m (Key f, a) (f b)
demuxKvToContainer
{-# INLINE classifyGeneric #-}
classifyGeneric :: (Monad m, IsMap f, Traversable f, Ord (Key f)) =>
(a -> Key f) -> Fold m a b -> Fold m a (m (f b), Maybe (Key f, b))
classifyGeneric :: forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, IsMap f, Traversable f, Ord (Key f)) =>
(a -> Key f) -> Fold m a b -> Fold m a (m (f b), Maybe (Key f, b))
classifyGeneric a -> Key f
f (Fold s -> a -> m (Step s b)
step1 m (Step s b)
initial1 s -> m b
extract1 s -> m b
final1) =
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Fold m a b
Fold (\Tuple3' (f s) (Set (Key f)) (Maybe (Key f, b))
s a
a -> forall s b. s -> Step s b
Partial forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {c}.
Tuple3' (f s) (Set (Key f)) c
-> a -> m (Tuple3' (f s) (Set (Key f)) (Maybe (Key f, b)))
step Tuple3' (f s) (Set (Key f)) (Maybe (Key f, b))
s a
a) (forall s b. s -> Step s b
Partial forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {a} {a} {a}. m (Tuple3' (f a) (Set a) (Maybe a))
initial) forall {t :: * -> *} {m :: * -> *} {b} {b}.
(Traversable t, Monad m) =>
Tuple3' (t s) b b -> m (m (t b), b)
extract forall {m :: * -> *} {f :: * -> *} {b}.
(Monad m, IsMap f, Ord (Key f)) =>
Tuple3' (f s) (Set (Key f)) b -> m (m (f b), b)
final
where
initial :: m (Tuple3' (f a) (Set a) (Maybe a))
initial = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' forall (f :: * -> *) a. IsMap f => f a
IsMap.mapEmpty forall a. Set a
Set.empty forall a. Maybe a
Nothing
{-# INLINE initFold #-}
initFold :: f s
-> Set (Key f)
-> Key f
-> a
-> m (Tuple3' (f s) (Set (Key f)) (Maybe (Key f, b)))
initFold f s
kv Set (Key f)
set Key f
k a
a = do
Step s b
x <- m (Step s b)
initial1
case Step s b
x of
Partial s
s -> do
Step s b
r <- s -> a -> m (Step s b)
step1 s
s a
a
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Partial s
s1 ->
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' (forall (f :: * -> *) a. IsMap f => Key f -> a -> f a -> f a
IsMap.mapInsert Key f
k s
s1 f s
kv) Set (Key f)
set forall a. Maybe a
Nothing
Done b
b ->
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' f s
kv Set (Key f)
set (forall a. a -> Maybe a
Just (Key f
k, b
b))
Done b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' f s
kv (forall a. Ord a => a -> Set a -> Set a
Set.insert Key f
k Set (Key f)
set) (forall a. a -> Maybe a
Just (Key f
k, b
b)))
step :: Tuple3' (f s) (Set (Key f)) c
-> a -> m (Tuple3' (f s) (Set (Key f)) (Maybe (Key f, b)))
step (Tuple3' f s
kv Set (Key f)
set c
_) a
a = do
let k :: Key f
k = a -> Key f
f a
a
case forall (f :: * -> *) a. IsMap f => Key f -> f a -> Maybe a
IsMap.mapLookup Key f
k f s
kv of
Maybe s
Nothing -> do
if forall a. Ord a => a -> Set a -> Bool
Set.member Key f
k Set (Key f)
set
then forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' f s
kv Set (Key f)
set forall a. Maybe a
Nothing)
else forall {f :: * -> *}.
(IsMap f, Ord (Key f)) =>
f s
-> Set (Key f)
-> Key f
-> a
-> m (Tuple3' (f s) (Set (Key f)) (Maybe (Key f, b)))
initFold f s
kv Set (Key f)
set Key f
k a
a
Just s
s -> do
Step s b
r <- s -> a -> m (Step s b)
step1 s
s a
a
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Partial s
s1 ->
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' (forall (f :: * -> *) a. IsMap f => Key f -> a -> f a -> f a
IsMap.mapInsert Key f
k s
s1 f s
kv) Set (Key f)
set forall a. Maybe a
Nothing
Done b
b ->
let kv1 :: f s
kv1 = forall (f :: * -> *) a. IsMap f => Key f -> f a -> f a
IsMap.mapDelete Key f
k f s
kv
in forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' f s
kv1 (forall a. Ord a => a -> Set a -> Set a
Set.insert Key f
k Set (Key f)
set) (forall a. a -> Maybe a
Just (Key f
k, b
b))
extract :: Tuple3' (t s) b b -> m (m (t b), b)
extract (Tuple3' t s
kv b
_ b
x) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
Prelude.mapM s -> m b
extract1 t s
kv, b
x)
final :: Tuple3' (f s) (Set (Key f)) b -> m (m (f b), b)
final (Tuple3' f s
kv Set (Key f)
set b
x) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (t :: * -> *) a b.
(IsMap f, Applicative t) =>
(Key f -> a -> t b) -> f a -> t (f b)
IsMap.mapTraverseWithKey Key f -> s -> m b
f1 f s
kv, b
x)
where
f1 :: Key f -> s -> m b
f1 Key f
k s
s = do
if forall a. Ord a => a -> Set a -> Bool
Set.member Key f
k Set (Key f)
set
then s -> m b
extract1 s
s
else s -> m b
final1 s
s
{-# INLINE classify #-}
classify :: (Monad m, Ord k) =>
(a -> k) -> Fold m a b -> Fold m a (m (Map k b), Maybe (k, b))
classify :: forall (m :: * -> *) k a b.
(Monad m, Ord k) =>
(a -> k) -> Fold m a b -> Fold m a (m (Map k b), Maybe (k, b))
classify = forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, IsMap f, Traversable f, Ord (Key f)) =>
(a -> Key f) -> Fold m a b -> Fold m a (m (f b), Maybe (Key f, b))
classifyGeneric
{-# INLINE classifyGenericIO #-}
classifyGenericIO :: (MonadIO m, IsMap f, Traversable f, Ord (Key f)) =>
(a -> Key f) -> Fold m a b -> Fold m a (m (f b), Maybe (Key f, b))
classifyGenericIO :: forall (m :: * -> *) (f :: * -> *) a b.
(MonadIO m, IsMap f, Traversable f, Ord (Key f)) =>
(a -> Key f) -> Fold m a b -> Fold m a (m (f b), Maybe (Key f, b))
classifyGenericIO a -> Key f
f (Fold s -> a -> m (Step s b)
step1 m (Step s b)
initial1 s -> m b
extract1 s -> m b
final1) =
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Fold m a b
Fold (\Tuple3' (f (IORef s)) (Set (Key f)) (Maybe (Key f, b))
s a
a -> forall s b. s -> Step s b
Partial forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {c}.
Tuple3' (f (IORef s)) (Set (Key f)) c
-> a -> m (Tuple3' (f (IORef s)) (Set (Key f)) (Maybe (Key f, b)))
step Tuple3' (f (IORef s)) (Set (Key f)) (Maybe (Key f, b))
s a
a) (forall s b. s -> Step s b
Partial forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {a} {a} {a}. m (Tuple3' (f a) (Set a) (Maybe a))
initial) forall {t :: * -> *} {m :: * -> *} {b} {b}.
(Traversable t, Monad m) =>
Tuple3' (t (IORef s)) b b -> m (m (t b), b)
extract forall {m :: * -> *} {f :: * -> *} {b}.
(Monad m, IsMap f, Ord (Key f)) =>
Tuple3' (f (IORef s)) (Set (Key f)) b -> m (m (f b), b)
final
where
initial :: m (Tuple3' (f a) (Set a) (Maybe a))
initial = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' forall (f :: * -> *) a. IsMap f => f a
IsMap.mapEmpty forall a. Set a
Set.empty forall a. Maybe a
Nothing
{-# INLINE initFold #-}
initFold :: f (IORef s)
-> Set (Key f)
-> Key f
-> a
-> m (Tuple3' (f (IORef s)) (Set (Key f)) (Maybe (Key f, b)))
initFold f (IORef s)
kv Set (Key f)
set Key f
k a
a = do
Step s b
x <- m (Step s b)
initial1
case Step s b
x of
Partial s
s -> do
Step s b
r <- s -> a -> m (Step s b)
step1 s
s a
a
case Step s b
r of
Partial s
s1 -> do
IORef s
ref <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. a -> IO (IORef a)
newIORef s
s1
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' (forall (f :: * -> *) a. IsMap f => Key f -> a -> f a -> f a
IsMap.mapInsert Key f
k IORef s
ref f (IORef s)
kv) Set (Key f)
set forall a. Maybe a
Nothing
Done b
b ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' f (IORef s)
kv Set (Key f)
set (forall a. a -> Maybe a
Just (Key f
k, b
b))
Done b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' f (IORef s)
kv (forall a. Ord a => a -> Set a -> Set a
Set.insert Key f
k Set (Key f)
set) (forall a. a -> Maybe a
Just (Key f
k, b
b)))
step :: Tuple3' (f (IORef s)) (Set (Key f)) c
-> a -> m (Tuple3' (f (IORef s)) (Set (Key f)) (Maybe (Key f, b)))
step (Tuple3' f (IORef s)
kv Set (Key f)
set c
_) a
a = do
let k :: Key f
k = a -> Key f
f a
a
case forall (f :: * -> *) a. IsMap f => Key f -> f a -> Maybe a
IsMap.mapLookup Key f
k f (IORef s)
kv of
Maybe (IORef s)
Nothing -> do
if forall a. Ord a => a -> Set a -> Bool
Set.member Key f
k Set (Key f)
set
then forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' f (IORef s)
kv Set (Key f)
set forall a. Maybe a
Nothing)
else forall {f :: * -> *}.
(IsMap f, Ord (Key f)) =>
f (IORef s)
-> Set (Key f)
-> Key f
-> a
-> m (Tuple3' (f (IORef s)) (Set (Key f)) (Maybe (Key f, b)))
initFold f (IORef s)
kv Set (Key f)
set Key f
k a
a
Just IORef s
ref -> do
s
s <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> IO a
readIORef IORef s
ref
Step s b
r <- s -> a -> m (Step s b)
step1 s
s a
a
case Step s b
r of
Partial s
s1 -> do
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> a -> IO ()
writeIORef IORef s
ref s
s1
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' f (IORef s)
kv Set (Key f)
set forall a. Maybe a
Nothing
Done b
b ->
let kv1 :: f (IORef s)
kv1 = forall (f :: * -> *) a. IsMap f => Key f -> f a -> f a
IsMap.mapDelete Key f
k f (IORef s)
kv
in forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' f (IORef s)
kv1 (forall a. Ord a => a -> Set a -> Set a
Set.insert Key f
k Set (Key f)
set) (forall a. a -> Maybe a
Just (Key f
k, b
b))
extract :: Tuple3' (t (IORef s)) b b -> m (m (t b), b)
extract (Tuple3' t (IORef s)
kv b
_ b
x) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
Prelude.mapM IORef s -> m b
g t (IORef s)
kv, b
x)
where
g :: IORef s -> m b
g IORef s
ref = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a. IORef a -> IO a
readIORef IORef s
ref) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> m b
extract1
final :: Tuple3' (f (IORef s)) (Set (Key f)) b -> m (m (f b), b)
final (Tuple3' f (IORef s)
kv Set (Key f)
set b
x) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (t :: * -> *) a b.
(IsMap f, Applicative t) =>
(Key f -> a -> t b) -> f a -> t (f b)
IsMap.mapTraverseWithKey Key f -> IORef s -> m b
g f (IORef s)
kv, b
x)
where
g :: Key f -> IORef s -> m b
g Key f
k IORef s
ref = do
s
s <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> IO a
readIORef IORef s
ref
if forall a. Ord a => a -> Set a -> Bool
Set.member Key f
k Set (Key f)
set
then s -> m b
extract1 s
s
else s -> m b
final1 s
s
{-# INLINE classifyIO #-}
classifyIO :: (MonadIO m, Ord k) =>
(a -> k) -> Fold m a b -> Fold m a (m (Map k b), Maybe (k, b))
classifyIO :: forall (m :: * -> *) k a b.
(MonadIO m, Ord k) =>
(a -> k) -> Fold m a b -> Fold m a (m (Map k b), Maybe (k, b))
classifyIO = forall (m :: * -> *) (f :: * -> *) a b.
(MonadIO m, IsMap f, Traversable f, Ord (Key f)) =>
(a -> Key f) -> Fold m a b -> Fold m a (m (f b), Maybe (Key f, b))
classifyGenericIO
{-# INLINE toContainer #-}
toContainer :: (Monad m, IsMap f, Traversable f, Ord (Key f)) =>
(a -> Key f) -> Fold m a b -> Fold m a (f b)
toContainer :: forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, IsMap f, Traversable f, Ord (Key f)) =>
(a -> Key f) -> Fold m a b -> Fold m a (f b)
toContainer a -> Key f
f Fold m a b
fld =
let
classifier :: Fold m a (m (f b), Maybe (Key f, b))
classifier = forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, IsMap f, Traversable f, Ord (Key f)) =>
(a -> Key f) -> Fold m a b -> Fold m a (m (f b), Maybe (Key f, b))
classifyGeneric a -> Key f
f Fold m a b
fld
getMap :: Maybe (f (f a)) -> f (f a)
getMap Maybe (f (f a))
Nothing = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (f :: * -> *) a. IsMap f => f a
IsMap.mapEmpty
getMap (Just f (f a)
action) = f (f a)
action
aggregator :: Fold m (m (f a), Maybe (Key f, a)) (f a)
aggregator =
forall (m :: * -> *) a b c x.
Monad m =>
(a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
teeWith forall (f :: * -> *) a. IsMap f => f a -> f a -> f a
IsMap.mapUnion
(forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> Fold m a b -> Fold m a c
rmapM forall {f :: * -> *} {f :: * -> *} {a}.
(Applicative f, IsMap f) =>
Maybe (f (f a)) -> f (f a)
getMap forall a b. (a -> b) -> a -> b
$ forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
lmap forall a b. (a, b) -> a
fst forall (m :: * -> *) a. Monad m => Fold m a (Maybe a)
latest)
(forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
lmap forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Fold m (Maybe a) b
catMaybes forall (m :: * -> *) (f :: * -> *) a.
(Monad m, IsMap f) =>
Fold m (Key f, a) (f a)
kvToMapOverwriteGeneric)
in forall (m :: * -> *) a b c.
Monad m =>
Fold m a b -> Fold m b c -> Fold m a c
postscan Fold m a (m (f b), Maybe (Key f, b))
classifier forall {a}. Fold m (m (f a), Maybe (Key f, a)) (f a)
aggregator
{-# INLINE toMap #-}
toMap :: (Monad m, Ord k) =>
(a -> k) -> Fold m a b -> Fold m a (Map k b)
toMap :: forall (m :: * -> *) k a b.
(Monad m, Ord k) =>
(a -> k) -> Fold m a b -> Fold m a (Map k b)
toMap = forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, IsMap f, Traversable f, Ord (Key f)) =>
(a -> Key f) -> Fold m a b -> Fold m a (f b)
toContainer
{-# INLINE toContainerIO #-}
toContainerIO :: (MonadIO m, IsMap f, Traversable f, Ord (Key f)) =>
(a -> Key f) -> Fold m a b -> Fold m a (f b)
toContainerIO :: forall (m :: * -> *) (f :: * -> *) a b.
(MonadIO m, IsMap f, Traversable f, Ord (Key f)) =>
(a -> Key f) -> Fold m a b -> Fold m a (f b)
toContainerIO a -> Key f
f Fold m a b
fld =
let
classifier :: Fold m a (m (f b), Maybe (Key f, b))
classifier = forall (m :: * -> *) (f :: * -> *) a b.
(MonadIO m, IsMap f, Traversable f, Ord (Key f)) =>
(a -> Key f) -> Fold m a b -> Fold m a (m (f b), Maybe (Key f, b))
classifyGenericIO a -> Key f
f Fold m a b
fld
getMap :: Maybe (f (f a)) -> f (f a)
getMap Maybe (f (f a))
Nothing = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (f :: * -> *) a. IsMap f => f a
IsMap.mapEmpty
getMap (Just f (f a)
action) = f (f a)
action
aggregator :: Fold m (m (f a), Maybe (Key f, a)) (f a)
aggregator =
forall (m :: * -> *) a b c x.
Monad m =>
(a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
teeWith forall (f :: * -> *) a. IsMap f => f a -> f a -> f a
IsMap.mapUnion
(forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> Fold m a b -> Fold m a c
rmapM forall {f :: * -> *} {f :: * -> *} {a}.
(Applicative f, IsMap f) =>
Maybe (f (f a)) -> f (f a)
getMap forall a b. (a -> b) -> a -> b
$ forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
lmap forall a b. (a, b) -> a
fst forall (m :: * -> *) a. Monad m => Fold m a (Maybe a)
latest)
(forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
lmap forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Fold m (Maybe a) b
catMaybes forall (m :: * -> *) (f :: * -> *) a.
(Monad m, IsMap f) =>
Fold m (Key f, a) (f a)
kvToMapOverwriteGeneric)
in forall (m :: * -> *) a b c.
Monad m =>
Fold m a b -> Fold m b c -> Fold m a c
postscan Fold m a (m (f b), Maybe (Key f, b))
classifier forall {a}. Fold m (m (f a), Maybe (Key f, a)) (f a)
aggregator
{-# INLINE toMapIO #-}
toMapIO :: (MonadIO m, Ord k) =>
(a -> k) -> Fold m a b -> Fold m a (Map k b)
toMapIO :: forall (m :: * -> *) k a b.
(MonadIO m, Ord k) =>
(a -> k) -> Fold m a b -> Fold m a (Map k b)
toMapIO = forall (m :: * -> *) (f :: * -> *) a b.
(MonadIO m, IsMap f, Traversable f, Ord (Key f)) =>
(a -> Key f) -> Fold m a b -> Fold m a (f b)
toContainerIO
{-# INLINE kvToMap #-}
kvToMap :: (Monad m, Ord k) => Fold m a b -> Fold m (k, a) (Map k b)
kvToMap :: forall (m :: * -> *) k a b.
(Monad m, Ord k) =>
Fold m a b -> Fold m (k, a) (Map k b)
kvToMap = forall (m :: * -> *) k a b.
(Monad m, Ord k) =>
(a -> k) -> Fold m a b -> Fold m a (Map k b)
toMap forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
lmap forall a b. (a, b) -> b
snd
{-# INLINE frequency #-}
frequency :: (Monad m, Ord a) => Fold m a (Map a Int)
frequency :: forall (m :: * -> *) a. (Monad m, Ord a) => Fold m a (Map a Int)
frequency = forall (m :: * -> *) k a b.
(Monad m, Ord k) =>
(a -> k) -> Fold m a b -> Fold m a (Map k b)
toMap forall a. a -> a
id forall (m :: * -> *) a. Monad m => Fold m a Int
length