module Streamly.Internal.Data.Fold
(
Step (..)
, Fold (..)
, foldl'
, foldlM'
, foldl1'
, foldr
, foldrM
, mkFold
, mkFold_
, mkFoldM
, mkFoldM_
, fromPure
, fromEffect
, sconcat
, mconcat
, foldMap
, foldMapM
, drain
, drainBy
, last
, length
, mean
, variance
, stdDev
, rollingHash
, rollingHashWithSalt
, rollingHashFirstN
, sum
, product
, maximumBy
, maximum
, minimumBy
, minimum
, toList
, toListRev
, toStream
, toStreamRev
, drainN
, genericIndex
, index
, head
, find
, lookup
, findIndex
, elemIndex
, null
, elem
, notElem
, all
, any
, and
, or
, with
, hoist
, generally
, rmapM
, transform
, map
, lmap
, lmapM
, indexed
, filter
, filterM
, sampleFromthen
, catMaybes
, mapMaybe
, take
, takeInterval
, takeEndBy
, takeEndBy_
, serialWith
, splitAt
, teeWith
, tee
, teeWithFst
, teeWithMin
, distribute
, shortest
, longest
, partitionByM
, partitionByFstM
, partitionByMinM
, partitionBy
, partition
, demux
, demuxWith
, demuxDefault
, demuxDefaultWith
, classify
, classifyWith
, unzip
, unzipWith
, unzipWithM
, unzipWithFstM
, unzipWithMinM
, zipWithM
, zip
, many
, intervalsOf
, chunksOf
, chunksBetween
, concatSequence
, concatMap
, initialize
, runStep
, duplicate
, drainBy2
, sequence
, mapM
)
where
import Control.Monad (void)
import Data.Bifunctor (first)
import Data.Functor.Identity (Identity(..))
import Data.Int (Int64)
import Data.Map.Strict (Map)
import Data.Maybe (isJust, fromJust)
#if __GLASGOW_HASKELL__ < 804
import Data.Semigroup (Semigroup((<>)))
#endif
import Streamly.Internal.Data.Either.Strict
(Either'(..), fromLeft', fromRight', isLeft', isRight')
import Streamly.Internal.Data.Pipe.Type (Pipe (..), PipeState(..))
import Streamly.Internal.Data.Tuple.Strict (Tuple'(..), Tuple3'(..))
import Streamly.Internal.Data.Stream.Serial (SerialT)
import qualified Data.Map.Strict as Map
import qualified Streamly.Internal.Data.Pipe.Type as Pipe
import qualified Streamly.Internal.Data.Stream.IsStream.Enumeration as Stream
import qualified Streamly.Internal.Data.Stream.StreamK as K
import qualified Prelude
import Prelude hiding
( filter, foldl1, drop, dropWhile, take, takeWhile, zipWith
, foldl, foldr, map, mapM_, sequence, all, any, sum, product, elem
, notElem, maximum, minimum, head, last, tail, length, null
, reverse, iterate, init, and, or, lookup, (!!)
, scanl, scanl1, replicate, concatMap, mconcat, foldMap, unzip
, span, splitAt, break, mapM, zip)
import Streamly.Internal.Data.Fold.Type
hoist :: (forall x. m x -> n x) -> Fold m a b -> Fold n a b
hoist :: (forall x. m x -> n x) -> Fold m a b -> Fold n a b
hoist forall x. m x -> n x
f (Fold s -> a -> m (Step s b)
step m (Step s b)
initial s -> m b
extract) =
(s -> a -> n (Step s b))
-> n (Step s b) -> (s -> n b) -> Fold n a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
Fold (\s
x a
a -> m (Step s b) -> n (Step s b)
forall x. m x -> n x
f (m (Step s b) -> n (Step s b)) -> m (Step s b) -> n (Step s b)
forall a b. (a -> b) -> a -> b
$ s -> a -> m (Step s b)
step s
x a
a) (m (Step s b) -> n (Step s b)
forall x. m x -> n x
f m (Step s b)
initial) (m b -> n b
forall x. m x -> n x
f (m b -> n b) -> (s -> m b) -> s -> n b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> m b
extract)
generally :: Monad m => Fold Identity a b -> Fold m a b
generally :: Fold Identity a b -> Fold m a b
generally = (forall x. Identity x -> m x) -> Fold Identity a b -> Fold m a b
forall (m :: * -> *) (n :: * -> *) a b.
(forall x. m x -> n x) -> Fold m a b -> Fold n a b
hoist (x -> m x
forall (m :: * -> *) a. Monad m => a -> m a
return (x -> m x) -> (Identity x -> x) -> Identity x -> m x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity x -> x
forall a. Identity a -> a
runIdentity)
{-# DEPRECATED sequence "Use \"rmapM id\" instead" #-}
{-# INLINE sequence #-}
sequence :: Monad m => Fold m a (m b) -> Fold m a b
sequence :: Fold m a (m b) -> Fold m a b
sequence = (m b -> m b) -> Fold m a (m b) -> Fold m a b
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> Fold m a b -> Fold m a c
rmapM m b -> m b
forall a. a -> a
id
{-# DEPRECATED mapM "Use rmapM instead" #-}
{-# INLINE mapM #-}
mapM :: Monad m => (b -> m c) -> Fold m a b -> Fold m a c
mapM :: (b -> m c) -> Fold m a b -> Fold m a c
mapM = (b -> m c) -> Fold m a b -> Fold m a c
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> Fold m a b -> Fold m a c
rmapM
{-# INLINE mapMaybe #-}
mapMaybe :: (Monad m) => (a -> Maybe b) -> Fold m b r -> Fold m a r
mapMaybe :: (a -> Maybe b) -> Fold m b r -> Fold m a r
mapMaybe a -> Maybe b
f = (a -> Maybe b) -> Fold m (Maybe b) r -> Fold m a r
forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
map a -> Maybe b
f (Fold m (Maybe b) r -> Fold m a r)
-> (Fold m b r -> Fold m (Maybe b) r) -> Fold m b r -> Fold m a r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe b -> Bool) -> Fold m (Maybe b) r -> Fold m (Maybe b) r
forall (m :: * -> *) a r.
Monad m =>
(a -> Bool) -> Fold m a r -> Fold m a r
filter Maybe b -> Bool
forall a. Maybe a -> Bool
isJust (Fold m (Maybe b) r -> Fold m (Maybe b) r)
-> (Fold m b r -> Fold m (Maybe b) r)
-> Fold m b r
-> Fold m (Maybe b) r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe b -> b) -> Fold m b r -> Fold m (Maybe b) r
forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
map Maybe b -> b
forall a. HasCallStack => Maybe a -> a
fromJust
{-# INLINE transform #-}
transform :: Monad m => Pipe m a b -> Fold m b c -> Fold m a c
transform :: Pipe m a b -> Fold m b c -> Fold m a c
transform (Pipe s1 -> a -> m (Step (PipeState s1 s2) b)
pstep1 s2 -> m (Step (PipeState s1 s2) b)
pstep2 s1
pinitial) (Fold s -> b -> m (Step s c)
fstep m (Step s c)
finitial s -> m c
fextract) =
(Tuple' s1 s -> a -> m (Step (Tuple' s1 s) c))
-> m (Step (Tuple' s1 s) c) -> (Tuple' s1 s -> m c) -> Fold m a c
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
Fold Tuple' s1 s -> a -> m (Step (Tuple' s1 s) c)
step m (Step (Tuple' s1 s) c)
initial Tuple' s1 s -> m c
forall a. Tuple' a s -> m c
extract
where
initial :: m (Step (Tuple' s1 s) c)
initial = (s -> Tuple' s1 s) -> Step s c -> Step (Tuple' s1 s) c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (s1 -> s -> Tuple' s1 s
forall a b. a -> b -> Tuple' a b
Tuple' s1
pinitial) (Step s c -> Step (Tuple' s1 s) c)
-> m (Step s c) -> m (Step (Tuple' s1 s) c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (Step s c)
finitial
step :: Tuple' s1 s -> a -> m (Step (Tuple' s1 s) c)
step (Tuple' s1
ps s
fs) a
x = do
Step (PipeState s1 s2) b
r <- s1 -> a -> m (Step (PipeState s1 s2) b)
pstep1 s1
ps a
x
s -> Step (PipeState s1 s2) b -> m (Step (Tuple' s1 s) c)
go s
fs Step (PipeState s1 s2) b
r
where
go :: s -> Step (PipeState s1 s2) b -> m (Step (Tuple' s1 s) c)
go s
acc (Pipe.Yield b
b (Consume s1
ps')) = do
Step s c
acc' <- s -> b -> m (Step s c)
fstep s
acc b
b
Step (Tuple' s1 s) c -> m (Step (Tuple' s1 s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (Tuple' s1 s) c -> m (Step (Tuple' s1 s) c))
-> Step (Tuple' s1 s) c -> m (Step (Tuple' s1 s) c)
forall a b. (a -> b) -> a -> b
$ case Step s c
acc' of
Partial s
s -> Tuple' s1 s -> Step (Tuple' s1 s) c
forall s b. s -> Step s b
Partial (Tuple' s1 s -> Step (Tuple' s1 s) c)
-> Tuple' s1 s -> Step (Tuple' s1 s) c
forall a b. (a -> b) -> a -> b
$ s1 -> s -> Tuple' s1 s
forall a b. a -> b -> Tuple' a b
Tuple' s1
ps' s
s
Done c
b2 -> c -> Step (Tuple' s1 s) c
forall s b. b -> Step s b
Done c
b2
go s
acc (Pipe.Yield b
b (Produce s2
ps')) = do
Step s c
acc' <- s -> b -> m (Step s c)
fstep s
acc b
b
Step (PipeState s1 s2) b
r <- s2 -> m (Step (PipeState s1 s2) b)
pstep2 s2
ps'
case Step s c
acc' of
Partial s
s -> s -> Step (PipeState s1 s2) b -> m (Step (Tuple' s1 s) c)
go s
s Step (PipeState s1 s2) b
r
Done c
b2 -> Step (Tuple' s1 s) c -> m (Step (Tuple' s1 s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple' s1 s) c -> m (Step (Tuple' s1 s) c))
-> Step (Tuple' s1 s) c -> m (Step (Tuple' s1 s) c)
forall a b. (a -> b) -> a -> b
$ c -> Step (Tuple' s1 s) c
forall s b. b -> Step s b
Done c
b2
go s
acc (Pipe.Continue (Consume s1
ps')) =
Step (Tuple' s1 s) c -> m (Step (Tuple' s1 s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple' s1 s) c -> m (Step (Tuple' s1 s) c))
-> Step (Tuple' s1 s) c -> m (Step (Tuple' s1 s) c)
forall a b. (a -> b) -> a -> b
$ Tuple' s1 s -> Step (Tuple' s1 s) c
forall s b. s -> Step s b
Partial (Tuple' s1 s -> Step (Tuple' s1 s) c)
-> Tuple' s1 s -> Step (Tuple' s1 s) c
forall a b. (a -> b) -> a -> b
$ s1 -> s -> Tuple' s1 s
forall a b. a -> b -> Tuple' a b
Tuple' s1
ps' s
acc
go s
acc (Pipe.Continue (Produce s2
ps')) = do
Step (PipeState s1 s2) b
r <- s2 -> m (Step (PipeState s1 s2) b)
pstep2 s2
ps'
s -> Step (PipeState s1 s2) b -> m (Step (Tuple' s1 s) c)
go s
acc Step (PipeState s1 s2) b
r
extract :: Tuple' a s -> m c
extract (Tuple' a
_ s
fs) = s -> m c
fextract s
fs
{-# INLINABLE drainBy #-}
drainBy :: Monad m => (a -> m b) -> Fold m a ()
drainBy :: (a -> m b) -> Fold m a ()
drainBy a -> m b
f = (a -> m b) -> Fold m b () -> Fold m a ()
forall (m :: * -> *) a b r.
Monad m =>
(a -> m b) -> Fold m b r -> Fold m a r
lmapM a -> m b
f Fold m b ()
forall (m :: * -> *) a. Monad m => Fold m a ()
drain
{-# INLINABLE drainBy2 #-}
drainBy2 :: Monad m => (a -> m b) -> Fold2 m c a ()
drainBy2 :: (a -> m b) -> Fold2 m c a ()
drainBy2 a -> m b
f = (() -> a -> m ()) -> (c -> m ()) -> (() -> m ()) -> Fold2 m c a ()
forall (m :: * -> *) c a b s.
(s -> a -> m s) -> (c -> m s) -> (s -> m b) -> Fold2 m c a b
Fold2 ((a -> m ()) -> () -> a -> m ()
forall a b. a -> b -> a
const (m b -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m b -> m ()) -> (a -> m b) -> a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m b
f)) (\c
_ -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return
{-# INLINABLE last #-}
last :: Monad m => Fold m a (Maybe a)
last :: Fold m a (Maybe a)
last = (a -> a -> a) -> Fold m a (Maybe a)
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Fold m a (Maybe a)
foldl1' (\a
_ a
x -> a
x)
{-# INLINE genericLength #-}
genericLength :: (Monad m, Num b) => Fold m a b
genericLength :: Fold m a b
genericLength = (b -> a -> b) -> b -> Fold m a b
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' (\b
n a
_ -> b
n b -> b -> b
forall a. Num a => a -> a -> a
+ b
1) b
0
{-# INLINE length #-}
length :: Monad m => Fold m a Int
length :: Fold m a Int
length = Fold m a Int
forall (m :: * -> *) b a. (Monad m, Num b) => Fold m a b
genericLength
{-# INLINE sum #-}
sum :: (Monad m, Num a) => Fold m a a
sum :: Fold m a a
sum = (a -> a -> a) -> a -> Fold m a a
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' a -> a -> a
forall a. Num a => a -> a -> a
(+) a
0
{-# INLINE product #-}
product :: (Monad m, Num a, Eq a) => Fold m a a
product :: Fold m a a
product = (a -> a -> Step a a) -> Step a a -> Fold m a a
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> Step b b) -> Step b b -> Fold m a b
mkFold_ a -> a -> Step a a
forall s b. (Eq s, Num s, Num b) => s -> s -> Step s b
step (a -> Step a a
forall s b. s -> Step s b
Partial a
1)
where
step :: s -> s -> Step s b
step s
x s
a =
if s
a s -> s -> Bool
forall a. Eq a => a -> a -> Bool
== s
0
then b -> Step s b
forall s b. b -> Step s b
Done b
0
else s -> Step s b
forall s b. s -> Step s b
Partial (s -> Step s b) -> s -> Step s b
forall a b. (a -> b) -> a -> b
$ s
x s -> s -> s
forall a. Num a => a -> a -> a
* s
a
{-# INLINE maximumBy #-}
maximumBy :: Monad m => (a -> a -> Ordering) -> Fold m a (Maybe a)
maximumBy :: (a -> a -> Ordering) -> Fold m a (Maybe a)
maximumBy a -> a -> Ordering
cmp = (a -> a -> a) -> Fold m a (Maybe a)
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Fold m a (Maybe a)
foldl1' a -> a -> a
max'
where
max' :: a -> a -> a
max' a
x a
y =
case a -> a -> Ordering
cmp a
x a
y of
Ordering
GT -> a
x
Ordering
_ -> a
y
{-# INLINE maximum #-}
maximum :: (Monad m, Ord a) => Fold m a (Maybe a)
maximum :: Fold m a (Maybe a)
maximum = (a -> a -> a) -> Fold m a (Maybe a)
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Fold m a (Maybe a)
foldl1' a -> a -> a
forall a. Ord a => a -> a -> a
max
{-# INLINE minimumBy #-}
minimumBy :: Monad m => (a -> a -> Ordering) -> Fold m a (Maybe a)
minimumBy :: (a -> a -> Ordering) -> Fold m a (Maybe a)
minimumBy a -> a -> Ordering
cmp = (a -> a -> a) -> Fold m a (Maybe a)
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Fold m a (Maybe a)
foldl1' a -> a -> a
min'
where
min' :: a -> a -> a
min' a
x a
y =
case a -> a -> Ordering
cmp a
x a
y of
Ordering
GT -> a
y
Ordering
_ -> a
x
{-# INLINE minimum #-}
minimum :: (Monad m, Ord a) => Fold m a (Maybe a)
minimum :: Fold m a (Maybe a)
minimum = (a -> a -> a) -> Fold m a (Maybe a)
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Fold m a (Maybe a)
foldl1' a -> a -> a
forall a. Ord a => a -> a -> a
min
{-# INLINABLE mean #-}
mean :: (Monad m, Fractional a) => Fold m a a
mean :: Fold m a a
mean = (Tuple' a a -> a) -> Fold m a (Tuple' a a) -> Fold m a a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Tuple' a a -> a
forall a b. Tuple' a b -> a
done (Fold m a (Tuple' a a) -> Fold m a a)
-> Fold m a (Tuple' a a) -> Fold m a a
forall a b. (a -> b) -> a -> b
$ (Tuple' a a -> a -> Tuple' a a)
-> Tuple' a a -> Fold m a (Tuple' a a)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' Tuple' a a -> a -> Tuple' a a
forall b. Fractional b => Tuple' b b -> b -> Tuple' b b
step Tuple' a a
begin
where
begin :: Tuple' a a
begin = a -> a -> Tuple' a a
forall a b. a -> b -> Tuple' a b
Tuple' a
0 a
0
step :: Tuple' b b -> b -> Tuple' b b
step (Tuple' b
x b
n) b
y =
let n1 :: b
n1 = b
n b -> b -> b
forall a. Num a => a -> a -> a
+ b
1
in b -> b -> Tuple' b b
forall a b. a -> b -> Tuple' a b
Tuple' (b
x b -> b -> b
forall a. Num a => a -> a -> a
+ (b
y b -> b -> b
forall a. Num a => a -> a -> a
- b
x) b -> b -> b
forall a. Fractional a => a -> a -> a
/ b
n1) b
n1
done :: Tuple' a b -> a
done (Tuple' a
x b
_) = a
x
{-# INLINABLE variance #-}
variance :: (Monad m, Fractional a) => Fold m a a
variance :: Fold m a a
variance = (Tuple3' a a a -> a) -> Fold m a (Tuple3' a a a) -> Fold m a a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Tuple3' a a a -> a
forall a b. Fractional a => Tuple3' a b a -> a
done (Fold m a (Tuple3' a a a) -> Fold m a a)
-> Fold m a (Tuple3' a a a) -> Fold m a a
forall a b. (a -> b) -> a -> b
$ (Tuple3' a a a -> a -> Tuple3' a a a)
-> Tuple3' a a a -> Fold m a (Tuple3' a a a)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' Tuple3' a a a -> a -> Tuple3' a a a
forall b. Fractional b => Tuple3' b b b -> b -> Tuple3' b b b
step Tuple3' a a a
begin
where
begin :: Tuple3' a a a
begin = a -> a -> a -> Tuple3' a a a
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' a
0 a
0 a
0
step :: Tuple3' b b b -> b -> Tuple3' b b b
step (Tuple3' b
n b
mean_ b
m2) b
x = b -> b -> b -> Tuple3' b b b
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' b
n' b
mean' b
m2'
where
n' :: b
n' = b
n b -> b -> b
forall a. Num a => a -> a -> a
+ b
1
mean' :: b
mean' = (b
n b -> b -> b
forall a. Num a => a -> a -> a
* b
mean_ b -> b -> b
forall a. Num a => a -> a -> a
+ b
x) b -> b -> b
forall a. Fractional a => a -> a -> a
/ (b
n b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
delta :: b
delta = b
x b -> b -> b
forall a. Num a => a -> a -> a
- b
mean_
m2' :: b
m2' = b
m2 b -> b -> b
forall a. Num a => a -> a -> a
+ b
delta b -> b -> b
forall a. Num a => a -> a -> a
* b
delta b -> b -> b
forall a. Num a => a -> a -> a
* b
n b -> b -> b
forall a. Fractional a => a -> a -> a
/ (b
n b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
done :: Tuple3' a b a -> a
done (Tuple3' a
n b
_ a
m2) = a
m2 a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
n
{-# INLINABLE stdDev #-}
stdDev :: (Monad m, Floating a) => Fold m a a
stdDev :: Fold m a a
stdDev = a -> a
forall a. Floating a => a -> a
sqrt (a -> a) -> Fold m a a -> Fold m a a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Fold m a a
forall (m :: * -> *) a. (Monad m, Fractional a) => Fold m a a
variance
{-# INLINABLE rollingHashWithSalt #-}
rollingHashWithSalt :: (Monad m, Enum a) => Int64 -> Fold m a Int64
rollingHashWithSalt :: Int64 -> Fold m a Int64
rollingHashWithSalt = (Int64 -> a -> Int64) -> Int64 -> Fold m a Int64
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' Int64 -> a -> Int64
forall a. Enum a => Int64 -> a -> Int64
step
where
k :: Int64
k = Int64
2891336453 :: Int64
step :: Int64 -> a -> Int64
step Int64
cksum a
a = Int64
cksum Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Int64
k Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a -> Int
forall a. Enum a => a -> Int
fromEnum a
a)
{-# INLINE defaultSalt #-}
defaultSalt :: Int64
defaultSalt :: Int64
defaultSalt = -Int64
2578643520546668380
{-# INLINABLE rollingHash #-}
rollingHash :: (Monad m, Enum a) => Fold m a Int64
rollingHash :: Fold m a Int64
rollingHash = Int64 -> Fold m a Int64
forall (m :: * -> *) a.
(Monad m, Enum a) =>
Int64 -> Fold m a Int64
rollingHashWithSalt Int64
defaultSalt
{-# INLINABLE rollingHashFirstN #-}
rollingHashFirstN :: (Monad m, Enum a) => Int -> Fold m a Int64
rollingHashFirstN :: Int -> Fold m a Int64
rollingHashFirstN Int
n = Int -> Fold m a Int64 -> Fold m a Int64
forall (m :: * -> *) a b.
Monad m =>
Int -> Fold m a b -> Fold m a b
take Int
n Fold m a Int64
forall (m :: * -> *) a. (Monad m, Enum a) => Fold m a Int64
rollingHash
{-# INLINE sconcat #-}
sconcat :: (Monad m, Semigroup a) => a -> Fold m a a
sconcat :: a -> Fold m a a
sconcat = (a -> a -> a) -> a -> Fold m a a
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>)
{-# INLINE mconcat #-}
mconcat ::
( Monad m
#if !MIN_VERSION_base(4,11,0)
, Semigroup a
#endif
, Monoid a) => Fold m a a
mconcat :: Fold m a a
mconcat = a -> Fold m a a
forall (m :: * -> *) a. (Monad m, Semigroup a) => a -> Fold m a a
sconcat a
forall a. Monoid a => a
mempty
{-# INLINABLE foldMap #-}
foldMap :: (Monad m, Monoid b
#if !MIN_VERSION_base(4,11,0)
, Semigroup b
#endif
) => (a -> b) -> Fold m a b
foldMap :: (a -> b) -> Fold m a b
foldMap a -> b
f = (a -> b) -> Fold m b b -> Fold m a b
forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
lmap a -> b
f Fold m b b
forall (m :: * -> *) a. (Monad m, Monoid a) => Fold m a a
mconcat
{-# INLINABLE foldMapM #-}
foldMapM :: (Monad m, Monoid b) => (a -> m b) -> Fold m a b
foldMapM :: (a -> m b) -> Fold m a b
foldMapM a -> m b
act = (b -> a -> m b) -> m b -> Fold m a b
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
foldlM' b -> a -> m b
step (b -> m b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
forall a. Monoid a => a
mempty)
where
step :: b -> a -> m b
step b
m a
a = do
b
m' <- a -> m b
act a
a
b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> m b) -> b -> m b
forall a b. (a -> b) -> a -> b
$! b -> b -> b
forall a. Monoid a => a -> a -> a
mappend b
m b
m'
{-# INLINABLE toListRev #-}
toListRev :: Monad m => Fold m a [a]
toListRev :: Fold m a [a]
toListRev = ([a] -> a -> [a]) -> [a] -> Fold m a [a]
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' ((a -> [a] -> [a]) -> [a] -> a -> [a]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (:)) []
{-# INLINABLE drainN #-}
drainN :: Monad m => Int -> Fold m a ()
drainN :: Int -> Fold m a ()
drainN Int
n = Int -> Fold m a () -> Fold m a ()
forall (m :: * -> *) a b.
Monad m =>
Int -> Fold m a b -> Fold m a b
take Int
n Fold m a ()
forall (m :: * -> *) a. Monad m => Fold m a ()
drain
{-# INLINABLE genericIndex #-}
genericIndex :: (Integral i, Monad m) => i -> Fold m a (Maybe a)
genericIndex :: i -> Fold m a (Maybe a)
genericIndex i
i = (i -> a -> Step i (Maybe a))
-> Step i (Maybe a) -> (i -> Maybe a) -> Fold m a (Maybe a)
forall (m :: * -> *) s a b.
Monad m =>
(s -> a -> Step s b) -> Step s b -> (s -> b) -> Fold m a b
mkFold i -> a -> Step i (Maybe a)
forall a. i -> a -> Step i (Maybe a)
step (i -> Step i (Maybe a)
forall s b. s -> Step s b
Partial i
0) (Maybe a -> i -> Maybe a
forall a b. a -> b -> a
const Maybe a
forall a. Maybe a
Nothing)
where
step :: i -> a -> Step i (Maybe a)
step i
j a
a =
if i
i i -> i -> Bool
forall a. Eq a => a -> a -> Bool
== i
j
then Maybe a -> Step i (Maybe a)
forall s b. b -> Step s b
Done (Maybe a -> Step i (Maybe a)) -> Maybe a -> Step i (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
a
else i -> Step i (Maybe a)
forall s b. s -> Step s b
Partial (i
j i -> i -> i
forall a. Num a => a -> a -> a
+ i
1)
{-# INLINABLE index #-}
index :: Monad m => Int -> Fold m a (Maybe a)
index :: Int -> Fold m a (Maybe a)
index = Int -> Fold m a (Maybe a)
forall i (m :: * -> *) a.
(Integral i, Monad m) =>
i -> Fold m a (Maybe a)
genericIndex
{-# INLINABLE head #-}
head :: Monad m => Fold m a (Maybe a)
head :: Fold m a (Maybe a)
head = (Maybe a -> a -> Step (Maybe a) (Maybe a))
-> Step (Maybe a) (Maybe a) -> Fold m a (Maybe a)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> Step b b) -> Step b b -> Fold m a b
mkFold_ ((a -> Step (Maybe a) (Maybe a))
-> Maybe a -> a -> Step (Maybe a) (Maybe a)
forall a b. a -> b -> a
const (Maybe a -> Step (Maybe a) (Maybe a)
forall s b. b -> Step s b
Done (Maybe a -> Step (Maybe a) (Maybe a))
-> (a -> Maybe a) -> a -> Step (Maybe a) (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a
forall a. a -> Maybe a
Just)) (Maybe a -> Step (Maybe a) (Maybe a)
forall s b. s -> Step s b
Partial Maybe a
forall a. Maybe a
Nothing)
{-# INLINABLE find #-}
find :: Monad m => (a -> Bool) -> Fold m a (Maybe a)
find :: (a -> Bool) -> Fold m a (Maybe a)
find a -> Bool
predicate = (() -> a -> Step () (Maybe a))
-> Step () (Maybe a) -> (() -> Maybe a) -> Fold m a (Maybe a)
forall (m :: * -> *) s a b.
Monad m =>
(s -> a -> Step s b) -> Step s b -> (s -> b) -> Fold m a b
mkFold () -> a -> Step () (Maybe a)
step (() -> Step () (Maybe a)
forall s b. s -> Step s b
Partial ()) (Maybe a -> () -> Maybe a
forall a b. a -> b -> a
const Maybe a
forall a. Maybe a
Nothing)
where
step :: () -> a -> Step () (Maybe a)
step () a
a =
if a -> Bool
predicate a
a
then Maybe a -> Step () (Maybe a)
forall s b. b -> Step s b
Done (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
else () -> Step () (Maybe a)
forall s b. s -> Step s b
Partial ()
{-# INLINABLE lookup #-}
lookup :: (Eq a, Monad m) => a -> Fold m (a,b) (Maybe b)
lookup :: a -> Fold m (a, b) (Maybe b)
lookup a
a0 = (() -> (a, b) -> Step () (Maybe b))
-> Step () (Maybe b) -> (() -> Maybe b) -> Fold m (a, b) (Maybe b)
forall (m :: * -> *) s a b.
Monad m =>
(s -> a -> Step s b) -> Step s b -> (s -> b) -> Fold m a b
mkFold () -> (a, b) -> Step () (Maybe b)
forall a. () -> (a, a) -> Step () (Maybe a)
step (() -> Step () (Maybe b)
forall s b. s -> Step s b
Partial ()) (Maybe b -> () -> Maybe b
forall a b. a -> b -> a
const Maybe b
forall a. Maybe a
Nothing)
where
step :: () -> (a, a) -> Step () (Maybe a)
step () (a
a, a
b) =
if a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
a0
then Maybe a -> Step () (Maybe a)
forall s b. b -> Step s b
Done (Maybe a -> Step () (Maybe a)) -> Maybe a -> Step () (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
b
else () -> Step () (Maybe a)
forall s b. s -> Step s b
Partial ()
{-# INLINABLE findIndex #-}
findIndex :: Monad m => (a -> Bool) -> Fold m a (Maybe Int)
findIndex :: (a -> Bool) -> Fold m a (Maybe Int)
findIndex a -> Bool
predicate = (Int -> a -> Step Int (Maybe Int))
-> Step Int (Maybe Int)
-> (Int -> Maybe Int)
-> Fold m a (Maybe Int)
forall (m :: * -> *) s a b.
Monad m =>
(s -> a -> Step s b) -> Step s b -> (s -> b) -> Fold m a b
mkFold Int -> a -> Step Int (Maybe Int)
forall s. Num s => s -> a -> Step s (Maybe s)
step (Int -> Step Int (Maybe Int)
forall s b. s -> Step s b
Partial Int
0) (Maybe Int -> Int -> Maybe Int
forall a b. a -> b -> a
const Maybe Int
forall a. Maybe a
Nothing)
where
step :: s -> a -> Step s (Maybe s)
step s
i a
a =
if a -> Bool
predicate a
a
then Maybe s -> Step s (Maybe s)
forall s b. b -> Step s b
Done (Maybe s -> Step s (Maybe s)) -> Maybe s -> Step s (Maybe s)
forall a b. (a -> b) -> a -> b
$ s -> Maybe s
forall a. a -> Maybe a
Just s
i
else s -> Step s (Maybe s)
forall s b. s -> Step s b
Partial (s
i s -> s -> s
forall a. Num a => a -> a -> a
+ s
1)
{-# INLINABLE elemIndex #-}
elemIndex :: (Eq a, Monad m) => a -> Fold m a (Maybe Int)
elemIndex :: a -> Fold m a (Maybe Int)
elemIndex a
a = (a -> Bool) -> Fold m a (Maybe Int)
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Fold m a (Maybe Int)
findIndex (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==)
{-# INLINABLE null #-}
null :: Monad m => Fold m a Bool
null :: Fold m a Bool
null = (() -> a -> Step () Bool)
-> Step () Bool -> (() -> Bool) -> Fold m a Bool
forall (m :: * -> *) s a b.
Monad m =>
(s -> a -> Step s b) -> Step s b -> (s -> b) -> Fold m a b
mkFold (\() a
_ -> Bool -> Step () Bool
forall s b. b -> Step s b
Done Bool
False) (() -> Step () Bool
forall s b. s -> Step s b
Partial ()) (Bool -> () -> Bool
forall a b. a -> b -> a
const Bool
True)
{-# INLINE any #-}
any :: Monad m => (a -> Bool) -> Fold m a Bool
any :: (a -> Bool) -> Fold m a Bool
any a -> Bool
predicate = (Bool -> a -> Step Bool Bool) -> Step Bool Bool -> Fold m a Bool
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> Step b b) -> Step b b -> Fold m a b
mkFold_ Bool -> a -> Step Bool Bool
forall p. p -> a -> Step Bool Bool
step Step Bool Bool
forall b. Step Bool b
initial
where
initial :: Step Bool b
initial = Bool -> Step Bool b
forall s b. s -> Step s b
Partial Bool
False
step :: p -> a -> Step Bool Bool
step p
_ a
a =
if a -> Bool
predicate a
a
then Bool -> Step Bool Bool
forall s b. b -> Step s b
Done Bool
True
else Bool -> Step Bool Bool
forall s b. s -> Step s b
Partial Bool
False
{-# INLINABLE elem #-}
elem :: (Eq a, Monad m) => a -> Fold m a Bool
elem :: a -> Fold m a Bool
elem a
a = (a -> Bool) -> Fold m a Bool
forall (m :: * -> *) a. Monad m => (a -> Bool) -> Fold m a Bool
any (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==)
{-# INLINABLE all #-}
all :: Monad m => (a -> Bool) -> Fold m a Bool
all :: (a -> Bool) -> Fold m a Bool
all a -> Bool
predicate = (Bool -> a -> Step Bool Bool) -> Step Bool Bool -> Fold m a Bool
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> Step b b) -> Step b b -> Fold m a b
mkFold_ Bool -> a -> Step Bool Bool
forall p. p -> a -> Step Bool Bool
step Step Bool Bool
forall b. Step Bool b
initial
where
initial :: Step Bool b
initial = Bool -> Step Bool b
forall s b. s -> Step s b
Partial Bool
True
step :: p -> a -> Step Bool Bool
step p
_ a
a =
if a -> Bool
predicate a
a
then Bool -> Step Bool Bool
forall s b. s -> Step s b
Partial Bool
True
else Bool -> Step Bool Bool
forall s b. b -> Step s b
Done Bool
False
{-# INLINABLE notElem #-}
notElem :: (Eq a, Monad m) => a -> Fold m a Bool
notElem :: a -> Fold m a Bool
notElem a
a = (a -> Bool) -> Fold m a Bool
forall (m :: * -> *) a. Monad m => (a -> Bool) -> Fold m a Bool
all (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/=)
{-# INLINE and #-}
and :: Monad m => Fold m Bool Bool
and :: Fold m Bool Bool
and = (Bool -> Bool) -> Fold m Bool Bool
forall (m :: * -> *) a. Monad m => (a -> Bool) -> Fold m a Bool
all (Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool
True)
{-# INLINE or #-}
or :: Monad m => Fold m Bool Bool
or :: Fold m Bool Bool
or = (Bool -> Bool) -> Fold m Bool Bool
forall (m :: * -> *) a. Monad m => (a -> Bool) -> Fold m a Bool
any (Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool
True)
{-# INLINE splitAt #-}
splitAt
:: Monad m
=> Int
-> Fold m a b
-> Fold m a c
-> Fold m a (b, c)
splitAt :: Int -> Fold m a b -> Fold m a c -> Fold m a (b, c)
splitAt Int
n Fold m a b
fld = (b -> c -> (b, c)) -> Fold m a b -> Fold m a c -> Fold m a (b, c)
forall (m :: * -> *) a b c x.
Monad m =>
(a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
serialWith (,) (Int -> Fold m a b -> Fold m a b
forall (m :: * -> *) a b.
Monad m =>
Int -> Fold m a b -> Fold m a b
take Int
n Fold m a b
fld)
{-# INLINE takeEndBy_ #-}
takeEndBy_ :: Monad m => (a -> Bool) -> Fold m a b -> Fold m a b
takeEndBy_ :: (a -> Bool) -> Fold m a b -> Fold m a b
takeEndBy_ a -> Bool
predicate (Fold s -> a -> m (Step s b)
fstep m (Step s b)
finitial s -> m b
fextract) =
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step m (Step s b)
finitial s -> m b
fextract
where
step :: s -> a -> m (Step s b)
step s
s a
a =
if Bool -> Bool
not (a -> Bool
predicate a
a)
then s -> a -> m (Step s b)
fstep s
s a
a
else b -> Step s b
forall s b. b -> Step s b
Done (b -> Step s b) -> m b -> m (Step s b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
fextract s
s
{-# INLINE takeEndBy #-}
takeEndBy :: Monad m => (a -> Bool) -> Fold m a b -> Fold m a b
takeEndBy :: (a -> Bool) -> Fold m a b -> Fold m a b
takeEndBy a -> Bool
predicate (Fold s -> a -> m (Step s b)
fstep m (Step s b)
finitial s -> m b
fextract) =
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step m (Step s b)
finitial s -> m b
fextract
where
step :: s -> a -> m (Step s b)
step s
s a
a = do
Step s b
res <- s -> a -> m (Step s b)
fstep s
s a
a
if Bool -> Bool
not (a -> Bool
predicate a
a)
then Step s b -> m (Step s b)
forall (m :: * -> *) a. Monad m => a -> m a
return Step s b
res
else do
case Step s b
res of
Partial s
s1 -> b -> Step s b
forall s b. b -> Step s b
Done (b -> Step s b) -> m b -> m (Step s b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
fextract s
s1
Done b
b -> 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
$ b -> Step s b
forall s b. b -> Step s b
Done b
b
{-# INLINE tee #-}
tee :: Monad m => Fold m a b -> Fold m a c -> Fold m a (b,c)
tee :: Fold m a b -> Fold m a c -> Fold m a (b, c)
tee = (b -> c -> (b, c)) -> Fold m a b -> Fold m a c -> Fold m a (b, c)
forall (m :: * -> *) a b c x.
Monad m =>
(a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
teeWith (,)
{-# INLINE distribute #-}
distribute :: Monad m => [Fold m a b] -> Fold m a [b]
distribute :: [Fold m a b] -> Fold m a [b]
distribute = (Fold m a b -> Fold m a [b] -> Fold m a [b])
-> Fold m a [b] -> [Fold m a b] -> Fold m a [b]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Prelude.foldr ((b -> [b] -> [b]) -> Fold m a b -> Fold m a [b] -> Fold m a [b]
forall (m :: * -> *) a b c x.
Monad m =>
(a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
teeWith (:)) ([b] -> Fold m a [b]
forall (m :: * -> *) b a. Applicative m => b -> Fold m a b
fromPure [])
{-# INLINE partitionByM #-}
partitionByM :: Monad m
=> (a -> m (Either b c)) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
partitionByM :: (a -> m (Either b c))
-> Fold m b x -> Fold m c y -> Fold m a (x, y)
partitionByM a -> m (Either b c)
f (Fold s -> b -> m (Step s x)
stepL m (Step s x)
beginL s -> m x
doneL) (Fold s -> c -> m (Step s y)
stepR m (Step s y)
beginR s -> m y
doneR) =
(GenericRunner s s x y
-> a -> m (Step (GenericRunner s s x y) (x, y)))
-> m (Step (GenericRunner s s x y) (x, y))
-> (GenericRunner s s x y -> m (x, y))
-> Fold m a (x, y)
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
Fold GenericRunner s s x y
-> a -> m (Step (GenericRunner s s x y) (x, y))
step m (Step (GenericRunner s s x y) (x, y))
begin GenericRunner s s x y -> m (x, y)
done
where
begin :: m (Step (GenericRunner s s x y) (x, y))
begin = do
Step s x
resL <- m (Step s x)
beginL
Step s y
resR <- m (Step s y)
beginR
Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y)))
-> Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall a b. (a -> b) -> a -> b
$ case Step s x
resL of
Partial s
sL ->
GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall s b. s -> Step s b
Partial
(GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y))
-> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall a b. (a -> b) -> a -> b
$ case Step s y
resR of
Partial s
sR -> s -> s -> GenericRunner s s x y
forall sL sR bL bR. sL -> sR -> GenericRunner sL sR bL bR
RunBoth s
sL s
sR
Done y
bR -> s -> y -> GenericRunner s s x y
forall sL sR bL bR. sL -> bR -> GenericRunner sL sR bL bR
RunLeft s
sL y
bR
Done x
bL ->
case Step s y
resR of
Partial s
sR -> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall s b. s -> Step s b
Partial (GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y))
-> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall a b. (a -> b) -> a -> b
$ x -> s -> GenericRunner s s x y
forall sL sR bL bR. bL -> sR -> GenericRunner sL sR bL bR
RunRight x
bL s
sR
Done y
bR -> (x, y) -> Step (GenericRunner s s x y) (x, y)
forall s b. b -> Step s b
Done (x
bL, y
bR)
step :: GenericRunner s s x y
-> a -> m (Step (GenericRunner s s x y) (x, y))
step (RunBoth s
sL s
sR) a
a = do
Either b c
r <- a -> m (Either b c)
f a
a
case Either b c
r of
Left b
b -> do
Step s x
res <- s -> b -> m (Step s x)
stepL s
sL b
b
Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y)))
-> Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall a b. (a -> b) -> a -> b
$ GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall s b. s -> Step s b
Partial
(GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y))
-> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall a b. (a -> b) -> a -> b
$ case Step s x
res of
Partial s
sres -> s -> s -> GenericRunner s s x y
forall sL sR bL bR. sL -> sR -> GenericRunner sL sR bL bR
RunBoth s
sres s
sR
Done x
bres -> x -> s -> GenericRunner s s x y
forall sL sR bL bR. bL -> sR -> GenericRunner sL sR bL bR
RunRight x
bres s
sR
Right c
c -> do
Step s y
res <- s -> c -> m (Step s y)
stepR s
sR c
c
Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y)))
-> Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall a b. (a -> b) -> a -> b
$ GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall s b. s -> Step s b
Partial
(GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y))
-> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall a b. (a -> b) -> a -> b
$ case Step s y
res of
Partial s
sres -> s -> s -> GenericRunner s s x y
forall sL sR bL bR. sL -> sR -> GenericRunner sL sR bL bR
RunBoth s
sL s
sres
Done y
bres -> s -> y -> GenericRunner s s x y
forall sL sR bL bR. sL -> bR -> GenericRunner sL sR bL bR
RunLeft s
sL y
bres
step (RunLeft s
sL y
bR) a
a = do
Either b c
r <- a -> m (Either b c)
f a
a
case Either b c
r of
Left b
b -> do
Step s x
res <- s -> b -> m (Step s x)
stepL s
sL b
b
Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y)))
-> Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall a b. (a -> b) -> a -> b
$ case Step s x
res of
Partial s
sres -> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall s b. s -> Step s b
Partial (GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y))
-> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall a b. (a -> b) -> a -> b
$ s -> y -> GenericRunner s s x y
forall sL sR bL bR. sL -> bR -> GenericRunner sL sR bL bR
RunLeft s
sres y
bR
Done x
bres -> (x, y) -> Step (GenericRunner s s x y) (x, y)
forall s b. b -> Step s b
Done (x
bres, y
bR)
Right c
_ -> Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y)))
-> Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall a b. (a -> b) -> a -> b
$ GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall s b. s -> Step s b
Partial (GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y))
-> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall a b. (a -> b) -> a -> b
$ s -> y -> GenericRunner s s x y
forall sL sR bL bR. sL -> bR -> GenericRunner sL sR bL bR
RunLeft s
sL y
bR
step (RunRight x
bL s
sR) a
a = do
Either b c
r <- a -> m (Either b c)
f a
a
case Either b c
r of
Left b
_ -> Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y)))
-> Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall a b. (a -> b) -> a -> b
$ GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall s b. s -> Step s b
Partial (GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y))
-> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall a b. (a -> b) -> a -> b
$ x -> s -> GenericRunner s s x y
forall sL sR bL bR. bL -> sR -> GenericRunner sL sR bL bR
RunRight x
bL s
sR
Right c
c -> do
Step s y
res <- s -> c -> m (Step s y)
stepR s
sR c
c
Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y)))
-> Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall a b. (a -> b) -> a -> b
$ case Step s y
res of
Partial s
sres -> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall s b. s -> Step s b
Partial (GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y))
-> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall a b. (a -> b) -> a -> b
$ x -> s -> GenericRunner s s x y
forall sL sR bL bR. bL -> sR -> GenericRunner sL sR bL bR
RunRight x
bL s
sres
Done y
bres -> (x, y) -> Step (GenericRunner s s x y) (x, y)
forall s b. b -> Step s b
Done (x
bL, y
bres)
done :: GenericRunner s s x y -> m (x, y)
done (RunBoth s
sL s
sR) = (,) (x -> y -> (x, y)) -> m x -> m (y -> (x, y))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m x
doneL s
sL m (y -> (x, y)) -> m y -> m (x, y)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> s -> m y
doneR s
sR
done (RunLeft s
sL y
bR) = (,y
bR) (x -> (x, y)) -> m x -> m (x, y)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m x
doneL s
sL
done (RunRight x
bL s
sR) = (x
bL,) (y -> (x, y)) -> m y -> m (x, y)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m y
doneR s
sR
{-# INLINE partitionByFstM #-}
partitionByFstM ::
(a -> m (Either b c)) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
partitionByFstM :: (a -> m (Either b c))
-> Fold m b x -> Fold m c y -> Fold m a (x, y)
partitionByFstM = (a -> m (Either b c))
-> Fold m b x -> Fold m c y -> Fold m a (x, y)
forall a. HasCallStack => a
undefined
{-# INLINE partitionByMinM #-}
partitionByMinM ::
(a -> m (Either b c)) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
partitionByMinM :: (a -> m (Either b c))
-> Fold m b x -> Fold m c y -> Fold m a (x, y)
partitionByMinM = (a -> m (Either b c))
-> Fold m b x -> Fold m c y -> Fold m a (x, y)
forall a. HasCallStack => a
undefined
{-# INLINE partitionBy #-}
partitionBy :: Monad m
=> (a -> Either b c) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
partitionBy :: (a -> Either b c) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
partitionBy a -> Either b c
f = (a -> m (Either b c))
-> Fold m b x -> Fold m c y -> Fold m a (x, y)
forall (m :: * -> *) a b c x y.
Monad m =>
(a -> m (Either b c))
-> Fold m b x -> Fold m c y -> Fold m a (x, y)
partitionByM (Either b c -> m (Either b c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either b c -> m (Either b c))
-> (a -> Either b c) -> a -> m (Either b c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either b c
f)
{-# INLINE partition #-}
partition :: Monad m
=> Fold m b x -> Fold m c y -> Fold m (Either b c) (x, y)
partition :: Fold m b x -> Fold m c y -> Fold m (Either b c) (x, y)
partition = (Either b c -> Either b c)
-> Fold m b x -> Fold m c y -> Fold m (Either b c) (x, y)
forall (m :: * -> *) a b c x y.
Monad m =>
(a -> Either b c) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
partitionBy Either b c -> Either b c
forall a. a -> a
id
{-# INLINE demuxWith #-}
demuxWith :: (Monad m, Ord k)
=> (a -> (k, a')) -> Map k (Fold m a' b) -> Fold m a (Map k b)
demuxWith :: (a -> (k, a')) -> Map k (Fold m a' b) -> Fold m a (Map k b)
demuxWith a -> (k, a')
f Map k (Fold m a' b)
kv = ((Map k b, ()) -> Map k b)
-> Fold m a (Map k b, ()) -> Fold m a (Map k b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Map k b, ()) -> Map k b
forall a b. (a, b) -> a
fst (Fold m a (Map k b, ()) -> Fold m a (Map k b))
-> Fold m a (Map k b, ()) -> Fold m a (Map k b)
forall a b. (a -> b) -> a -> b
$ (a -> (k, a'))
-> Map k (Fold m a' b)
-> Fold m (k, a') ()
-> Fold m a (Map k b, ())
forall (m :: * -> *) k a a' b c.
(Monad m, Ord k) =>
(a -> (k, a'))
-> Map k (Fold m a' b) -> Fold m (k, a') c -> Fold m a (Map k b, c)
demuxDefaultWith a -> (k, a')
f Map k (Fold m a' b)
kv Fold m (k, a') ()
forall (m :: * -> *) a. Monad m => Fold m a ()
drain
{-# INLINE demux #-}
demux :: (Monad m, Ord k)
=> Map k (Fold m a b) -> Fold m (k, a) (Map k b)
demux :: Map k (Fold m a b) -> Fold m (k, a) (Map k b)
demux = ((k, a) -> (k, a)) -> Map k (Fold m a b) -> Fold m (k, a) (Map k b)
forall (m :: * -> *) k a a' b.
(Monad m, Ord k) =>
(a -> (k, a')) -> Map k (Fold m a' b) -> Fold m a (Map k b)
demuxWith (k, a) -> (k, a)
forall a. a -> a
id
data DemuxState s b doneMap runMap =
DemuxMapAndDefault !s !doneMap !runMap
| DemuxOnlyMap b !doneMap !runMap
| DemuxOnlyDefault s !doneMap
{-# INLINE demuxDefaultWith #-}
demuxDefaultWith :: (Monad m, Ord k)
=> (a -> (k, a'))
-> Map k (Fold m a' b)
-> Fold m (k, a') c
-> Fold m a (Map k b, c)
demuxDefaultWith :: (a -> (k, a'))
-> Map k (Fold m a' b) -> Fold m (k, a') c -> Fold m a (Map k b, c)
demuxDefaultWith a -> (k, a')
f Map k (Fold m a' b)
kv (Fold s -> (k, a') -> m (Step s c)
dstep m (Step s c)
dinitial s -> m c
dextract) =
(DemuxState s c (Map k b) (Map k (Fold m a' b))
-> a
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)))
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
-> (DemuxState s c (Map k b) (Map k (Fold m a' b))
-> m (Map k b, c))
-> Fold m a (Map k b, c)
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
Fold DemuxState s c (Map k b) (Map k (Fold m a' b))
-> a
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
forall b.
DemuxState s c (Map k b) (Map k (Fold m a' b))
-> a
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
step m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
initial DemuxState s c (Map k b) (Map k (Fold m a' b)) -> m (Map k b, c)
forall k a a.
Ord k =>
DemuxState s c (Map k a) (Map k (Fold m a a)) -> m (Map k a, c)
extract
where
initial :: m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
initial = do
let runInit :: Fold m a b -> m (Either' b (Fold m a b))
runInit (Fold s -> a -> m (Step s b)
step1 m (Step s b)
initial1 s -> m b
done1) = do
Step s b
r <- m (Step s b)
initial1
Either' b (Fold m a b) -> m (Either' b (Fold m a b))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Either' b (Fold m a b) -> m (Either' b (Fold m a b)))
-> Either' b (Fold m a b) -> m (Either' b (Fold m a b))
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Partial s
_ -> Fold m a b -> Either' b (Fold m a b)
forall a b. b -> Either' a b
Right' ((s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step1 (Step s b -> m (Step s b)
forall (m :: * -> *) a. Monad m => a -> m a
return Step s b
r) s -> m b
done1)
Done b
b -> b -> Either' b (Fold m a b)
forall a b. a -> Either' a b
Left' b
b
Map k (Either' b (Fold m a' b))
kv1 <- (Fold m a' b -> m (Either' b (Fold m a' b)))
-> Map k (Fold m a' b) -> m (Map k (Either' b (Fold m a' b)))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
Prelude.mapM Fold m a' b -> m (Either' b (Fold m a' b))
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> m (Either' b (Fold m a b))
runInit Map k (Fold m a' b)
kv
let runMap :: Map k (Fold m a' b)
runMap = (Either' b (Fold m a' b) -> Fold m a' b)
-> Map k (Either' b (Fold m a' b)) -> Map k (Fold m a' b)
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map Either' b (Fold m a' b) -> Fold m a' b
forall a b. Either' a b -> b
fromRight' (Map k (Either' b (Fold m a' b)) -> Map k (Fold m a' b))
-> Map k (Either' b (Fold m a' b)) -> Map k (Fold m a' b)
forall a b. (a -> b) -> a -> b
$ (Either' b (Fold m a' b) -> Bool)
-> Map k (Either' b (Fold m a' b))
-> Map k (Either' b (Fold m a' b))
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter Either' b (Fold m a' b) -> Bool
forall a b. Either' a b -> Bool
isRight' Map k (Either' b (Fold m a' b))
kv1
doneMap :: Map k b
doneMap = (Either' b (Fold m a' b) -> b)
-> Map k (Either' b (Fold m a' b)) -> Map k b
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map Either' b (Fold m a' b) -> b
forall a b. Either' a b -> a
fromLeft' (Map k (Either' b (Fold m a' b)) -> Map k b)
-> Map k (Either' b (Fold m a' b)) -> Map k b
forall a b. (a -> b) -> a -> b
$ (Either' b (Fold m a' b) -> Bool)
-> Map k (Either' b (Fold m a' b))
-> Map k (Either' b (Fold m a' b))
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter Either' b (Fold m a' b) -> Bool
forall a b. Either' a b -> Bool
isLeft' Map k (Either' b (Fold m a' b))
kv1
Step s c
dres <- m (Step s c)
dinitial
Step (DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
forall a b. (a -> b) -> a -> b
$ case Step s c
dres of
Partial s
s ->
DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall s b. s -> Step s b
Partial
(DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
-> DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall a b. (a -> b) -> a -> b
$ if Map k (Fold m a' b) -> Int
forall k a. Map k a -> Int
Map.size Map k (Fold m a' b)
runMap Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0
then s
-> Map k b
-> Map k (Fold m a' b)
-> DemuxState s c (Map k b) (Map k (Fold m a' b))
forall s b doneMap runMap.
s -> doneMap -> runMap -> DemuxState s b doneMap runMap
DemuxMapAndDefault s
s Map k b
doneMap Map k (Fold m a' b)
runMap
else s -> Map k b -> DemuxState s c (Map k b) (Map k (Fold m a' b))
forall s b doneMap runMap.
s -> doneMap -> DemuxState s b doneMap runMap
DemuxOnlyDefault s
s Map k b
doneMap
Done c
b ->
if Map k (Fold m a' b) -> Int
forall k a. Map k a -> Int
Map.size Map k (Fold m a' b)
runMap Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0
then DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall s b. s -> Step s b
Partial (DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
-> DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall a b. (a -> b) -> a -> b
$ c
-> Map k b
-> Map k (Fold m a' b)
-> DemuxState s c (Map k b) (Map k (Fold m a' b))
forall s b doneMap runMap.
b -> doneMap -> runMap -> DemuxState s b doneMap runMap
DemuxOnlyMap c
b Map k b
doneMap Map k (Fold m a' b)
runMap
else (Map k b, c)
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall s b. b -> Step s b
Done (Map k b
doneMap, c
b)
{-# INLINE runFold #-}
runFold :: (Map p b -> Map p (Fold m a b) -> s)
-> (Map p b -> Step s b)
-> Map p b
-> Map p (Fold m a b)
-> Fold m a b
-> p
-> a
-> m (Step s b)
runFold Map p b -> Map p (Fold m a b) -> s
fPartial Map p b -> Step s b
fDone Map p b
doneMap Map p (Fold m a b)
runMap (Fold s -> a -> m (Step s b)
step1 m (Step s b)
initial1 s -> m b
done1) p
k a
a1 = do
Step s b
resi <- m (Step s b)
initial1
case Step s b
resi of
Partial s
st -> do
Step s b
res <- s -> a -> m (Step s b)
step1 s
st a
a1
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
$ case Step s b
res of
Partial s
s ->
let fld :: Fold m a b
fld = (s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step1 (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
Partial s
s) s -> m b
done1
runMap1 :: Map p (Fold m a b)
runMap1 = p -> Fold m a b -> Map p (Fold m a b) -> Map p (Fold m a b)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert p
k Fold m a b
fld Map p (Fold m a b)
runMap
in s -> Step s b
forall s b. s -> Step s b
Partial (s -> Step s b) -> s -> Step s b
forall a b. (a -> b) -> a -> b
$ Map p b -> Map p (Fold m a b) -> s
fPartial Map p b
doneMap Map p (Fold m a b)
runMap1
Done b
b -> do
let runMap1 :: Map p (Fold m a b)
runMap1 = p -> Map p (Fold m a b) -> Map p (Fold m a b)
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete p
k Map p (Fold m a b)
runMap
doneMap1 :: Map p b
doneMap1 = p -> b -> Map p b -> Map p b
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert p
k b
b Map p b
doneMap
if Map p (Fold m a b) -> Int
forall k a. Map k a -> Int
Map.size Map p (Fold m a b)
runMap1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Map p b -> Step s b
fDone Map p b
doneMap1
else s -> Step s b
forall s b. s -> Step s b
Partial (s -> Step s b) -> s -> Step s b
forall a b. (a -> b) -> a -> b
$ Map p b -> Map p (Fold m a b) -> s
fPartial Map p b
doneMap1 Map p (Fold m a b)
runMap1
Done b
_ -> [Char] -> m (Step s b)
forall a. HasCallStack => [Char] -> a
error [Char]
"Bug: demuxDefaultWith: Done fold"
step :: DemuxState s c (Map k b) (Map k (Fold m a' b))
-> a
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
step (DemuxMapAndDefault s
dacc Map k b
doneMap Map k (Fold m a' b)
runMap) a
a = do
let (k
k, a'
a1) = a -> (k, a')
f a
a
case k -> Map k (Fold m a' b) -> Maybe (Fold m a' b)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup k
k Map k (Fold m a' b)
runMap of
Maybe (Fold m a' b)
Nothing -> do
Step s c
res <- s -> (k, a') -> m (Step s c)
dstep s
dacc (k
k, a'
a1)
Step (DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
forall a b. (a -> b) -> a -> b
$ DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall s b. s -> Step s b
Partial
(DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
-> DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall a b. (a -> b) -> a -> b
$ case Step s c
res of
Partial s
s -> s
-> Map k b
-> Map k (Fold m a' b)
-> DemuxState s c (Map k b) (Map k (Fold m a' b))
forall s b doneMap runMap.
s -> doneMap -> runMap -> DemuxState s b doneMap runMap
DemuxMapAndDefault s
s Map k b
doneMap Map k (Fold m a' b)
runMap
Done c
b -> c
-> Map k b
-> Map k (Fold m a' b)
-> DemuxState s c (Map k b) (Map k (Fold m a' b))
forall s b doneMap runMap.
b -> doneMap -> runMap -> DemuxState s b doneMap runMap
DemuxOnlyMap c
b Map k b
doneMap Map k (Fold m a' b)
runMap
Just Fold m a' b
fld ->
(Map k b
-> Map k (Fold m a' b)
-> DemuxState s c (Map k b) (Map k (Fold m a' b)))
-> (Map k b
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
-> Map k b
-> Map k (Fold m a' b)
-> Fold m a' b
-> k
-> a'
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
forall (m :: * -> *) p b a s b.
(Monad m, Ord p) =>
(Map p b -> Map p (Fold m a b) -> s)
-> (Map p b -> Step s b)
-> Map p b
-> Map p (Fold m a b)
-> Fold m a b
-> p
-> a
-> m (Step s b)
runFold
(s
-> Map k b
-> Map k (Fold m a' b)
-> DemuxState s c (Map k b) (Map k (Fold m a' b))
forall s b doneMap runMap.
s -> doneMap -> runMap -> DemuxState s b doneMap runMap
DemuxMapAndDefault s
dacc)
(DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall s b. s -> Step s b
Partial (DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
-> (Map k b -> DemuxState s c (Map k b) (Map k (Fold m a' b)))
-> Map k b
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Map k b -> DemuxState s c (Map k b) (Map k (Fold m a' b))
forall s b doneMap runMap.
s -> doneMap -> DemuxState s b doneMap runMap
DemuxOnlyDefault s
dacc)
Map k b
doneMap Map k (Fold m a' b)
runMap Fold m a' b
fld k
k a'
a1
step (DemuxOnlyMap c
dval Map k b
doneMap Map k (Fold m a' b)
runMap) a
a = do
let (k
k, a'
a1) = a -> (k, a')
f a
a
case k -> Map k (Fold m a' b) -> Maybe (Fold m a' b)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup k
k Map k (Fold m a' b)
runMap of
Maybe (Fold m a' b)
Nothing -> Step (DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
forall a b. (a -> b) -> a -> b
$ DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall s b. s -> Step s b
Partial (DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
-> DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall a b. (a -> b) -> a -> b
$ c
-> Map k b
-> Map k (Fold m a' b)
-> DemuxState s c (Map k b) (Map k (Fold m a' b))
forall s b doneMap runMap.
b -> doneMap -> runMap -> DemuxState s b doneMap runMap
DemuxOnlyMap c
dval Map k b
doneMap Map k (Fold m a' b)
runMap
Just Fold m a' b
fld ->
(Map k b
-> Map k (Fold m a' b)
-> DemuxState s c (Map k b) (Map k (Fold m a' b)))
-> (Map k b
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
-> Map k b
-> Map k (Fold m a' b)
-> Fold m a' b
-> k
-> a'
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
forall (m :: * -> *) p b a s b.
(Monad m, Ord p) =>
(Map p b -> Map p (Fold m a b) -> s)
-> (Map p b -> Step s b)
-> Map p b
-> Map p (Fold m a b)
-> Fold m a b
-> p
-> a
-> m (Step s b)
runFold
(c
-> Map k b
-> Map k (Fold m a' b)
-> DemuxState s c (Map k b) (Map k (Fold m a' b))
forall s b doneMap runMap.
b -> doneMap -> runMap -> DemuxState s b doneMap runMap
DemuxOnlyMap c
dval)
((Map k b, c)
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall s b. b -> Step s b
Done ((Map k b, c)
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
-> (Map k b -> (Map k b, c))
-> Map k b
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (, c
dval))
Map k b
doneMap Map k (Fold m a' b)
runMap Fold m a' b
fld k
k a'
a1
step (DemuxOnlyDefault s
dacc Map k b
doneMap) a
a = do
let (k
k, a'
a1) = a -> (k, a')
f a
a
Step s c
res <- s -> (k, a') -> m (Step s c)
dstep s
dacc (k
k, a'
a1)
Step (DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
-> m (Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
forall a b. (a -> b) -> a -> b
$ case Step s c
res of
Partial s
s -> DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall s b. s -> Step s b
Partial (DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c))
-> DemuxState s c (Map k b) (Map k (Fold m a' b))
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall a b. (a -> b) -> a -> b
$ s -> Map k b -> DemuxState s c (Map k b) (Map k (Fold m a' b))
forall s b doneMap runMap.
s -> doneMap -> DemuxState s b doneMap runMap
DemuxOnlyDefault s
s Map k b
doneMap
Done c
b -> (Map k b, c)
-> Step
(DemuxState s c (Map k b) (Map k (Fold m a' b))) (Map k b, c)
forall s b. b -> Step s b
Done (Map k b
doneMap, c
b)
runExtract :: Fold m a b -> m b
runExtract (Fold s -> a -> m (Step s b)
_ m (Step s b)
initial1 s -> m b
done1) = do
Step s b
res <- m (Step s b)
initial1
case Step s b
res of
Partial s
s -> s -> m b
done1 s
s
Done b
b -> b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
extract :: DemuxState s c (Map k a) (Map k (Fold m a a)) -> m (Map k a, c)
extract (DemuxMapAndDefault s
dacc Map k a
doneMap Map k (Fold m a a)
runMap) = do
c
b <- s -> m c
dextract s
dacc
Map k a
runMap1 <- (Fold m a a -> m a) -> Map k (Fold m a a) -> m (Map k a)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
Prelude.mapM Fold m a a -> m a
forall (m :: * -> *) a b. Monad m => Fold m a b -> m b
runExtract Map k (Fold m a a)
runMap
(Map k a, c) -> m (Map k a, c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Map k a
doneMap Map k a -> Map k a -> Map k a
forall k a. Ord k => Map k a -> Map k a -> Map k a
`Map.union` Map k a
runMap1, c
b)
extract (DemuxOnlyMap c
dval Map k a
doneMap Map k (Fold m a a)
runMap) = do
Map k a
runMap1 <- (Fold m a a -> m a) -> Map k (Fold m a a) -> m (Map k a)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
Prelude.mapM Fold m a a -> m a
forall (m :: * -> *) a b. Monad m => Fold m a b -> m b
runExtract Map k (Fold m a a)
runMap
(Map k a, c) -> m (Map k a, c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Map k a
doneMap Map k a -> Map k a -> Map k a
forall k a. Ord k => Map k a -> Map k a -> Map k a
`Map.union` Map k a
runMap1, c
dval)
extract (DemuxOnlyDefault s
dacc Map k a
doneMap) = do
c
b <- s -> m c
dextract s
dacc
(Map k a, c) -> m (Map k a, c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Map k a
doneMap, c
b)
{-# INLINE demuxDefault #-}
demuxDefault :: (Monad m, Ord k)
=> Map k (Fold m a b) -> Fold m (k, a) b -> Fold m (k, a) (Map k b, b)
demuxDefault :: Map k (Fold m a b) -> Fold m (k, a) b -> Fold m (k, a) (Map k b, b)
demuxDefault = ((k, a) -> (k, a))
-> Map k (Fold m a b)
-> Fold m (k, a) b
-> Fold m (k, a) (Map k b, b)
forall (m :: * -> *) k a a' b c.
(Monad m, Ord k) =>
(a -> (k, a'))
-> Map k (Fold m a' b) -> Fold m (k, a') c -> Fold m a (Map k b, c)
demuxDefaultWith (k, a) -> (k, a)
forall a. a -> a
id
{-# INLINE classifyWith #-}
classifyWith :: (Monad m, Ord k) => (a -> k) -> Fold m a b -> Fold m a (Map k b)
classifyWith :: (a -> k) -> Fold m a b -> Fold m a (Map k b)
classifyWith a -> k
f (Fold s -> a -> m (Step s b)
step1 m (Step s b)
initial1 s -> m b
extract1) =
(Map k (Either' s b) -> m (Map k b))
-> Fold m a (Map k (Either' s b)) -> Fold m a (Map k b)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> Fold m a b -> Fold m a c
rmapM Map k (Either' s b) -> m (Map k b)
extract (Fold m a (Map k (Either' s b)) -> Fold m a (Map k b))
-> Fold m a (Map k (Either' s b)) -> Fold m a (Map k b)
forall a b. (a -> b) -> a -> b
$ (Map k (Either' s b) -> a -> m (Map k (Either' s b)))
-> m (Map k (Either' s b)) -> Fold m a (Map k (Either' s b))
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
foldlM' Map k (Either' s b) -> a -> m (Map k (Either' s b))
step m (Map k (Either' s b))
forall k a. m (Map k a)
initial
where
initial :: m (Map k a)
initial = Map k a -> m (Map k a)
forall (m :: * -> *) a. Monad m => a -> m a
return Map k a
forall k a. Map k a
Map.empty
step :: Map k (Either' s b) -> a -> m (Map k (Either' s b))
step Map k (Either' s b)
kv a
a =
case k -> Map k (Either' s b) -> Maybe (Either' s b)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup k
k Map k (Either' s b)
kv of
Maybe (Either' s b)
Nothing -> 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
Map k (Either' s b) -> m (Map k (Either' s b))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Map k (Either' s b) -> m (Map k (Either' s b)))
-> Map k (Either' s b) -> m (Map k (Either' s b))
forall a b. (a -> b) -> a -> b
$ (Either' s b -> Map k (Either' s b) -> Map k (Either' s b))
-> Map k (Either' s b) -> Either' s b -> Map k (Either' s b)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (k -> Either' s b -> Map k (Either' s b) -> Map k (Either' s b)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert k
k) Map k (Either' s b)
kv
(Either' s b -> Map k (Either' s b))
-> Either' s b -> Map k (Either' s b)
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Partial s
s1 -> s -> Either' s b
forall a b. a -> Either' a b
Left' s
s1
Done b
b -> b -> Either' s b
forall a b. b -> Either' a b
Right' b
b
Done b
b -> Map k (Either' s b) -> m (Map k (Either' s b))
forall (m :: * -> *) a. Monad m => a -> m a
return (Map k (Either' s b) -> m (Map k (Either' s b)))
-> Map k (Either' s b) -> m (Map k (Either' s b))
forall a b. (a -> b) -> a -> b
$ k -> Either' s b -> Map k (Either' s b) -> Map k (Either' s b)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert k
k (b -> Either' s b
forall a b. b -> Either' a b
Right' b
b) Map k (Either' s b)
kv
Just Either' s b
x -> do
case Either' s b
x of
Left' s
s -> do
Step s b
r <- s -> a -> m (Step s b)
step1 s
s a
a
Map k (Either' s b) -> m (Map k (Either' s b))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Map k (Either' s b) -> m (Map k (Either' s b)))
-> Map k (Either' s b) -> m (Map k (Either' s b))
forall a b. (a -> b) -> a -> b
$ (Either' s b -> Map k (Either' s b) -> Map k (Either' s b))
-> Map k (Either' s b) -> Either' s b -> Map k (Either' s b)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (k -> Either' s b -> Map k (Either' s b) -> Map k (Either' s b)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert k
k) Map k (Either' s b)
kv
(Either' s b -> Map k (Either' s b))
-> Either' s b -> Map k (Either' s b)
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Partial s
s1 -> s -> Either' s b
forall a b. a -> Either' a b
Left' s
s1
Done b
b -> b -> Either' s b
forall a b. b -> Either' a b
Right' b
b
Right' b
_ -> Map k (Either' s b) -> m (Map k (Either' s b))
forall (m :: * -> *) a. Monad m => a -> m a
return Map k (Either' s b)
kv
where
k :: k
k = a -> k
f a
a
extract :: Map k (Either' s b) -> m (Map k b)
extract =
(Either' s b -> m b) -> Map k (Either' s b) -> m (Map k b)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
Prelude.mapM
(\case
Left' s
s -> s -> m b
extract1 s
s
Right' b
b -> b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b)
{-# INLINE classify #-}
classify :: (Monad m, Ord k) => Fold m a b -> Fold m (k, a) (Map k b)
classify :: Fold m a b -> Fold m (k, a) (Map k b)
classify Fold m a b
fld = ((k, a) -> k) -> Fold m (k, a) b -> Fold m (k, a) (Map k b)
forall (m :: * -> *) k a b.
(Monad m, Ord k) =>
(a -> k) -> Fold m a b -> Fold m a (Map k b)
classifyWith (k, a) -> k
forall a b. (a, b) -> a
fst (((k, a) -> a) -> Fold m a b -> Fold m (k, a) b
forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
map (k, a) -> a
forall a b. (a, b) -> b
snd Fold m a b
fld)
{-# INLINE unzipWithM #-}
unzipWithM :: Monad m
=> (a -> m (b,c)) -> Fold m b x -> Fold m c y -> Fold m a (x,y)
unzipWithM :: (a -> m (b, c)) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
unzipWithM a -> m (b, c)
f (Fold s -> b -> m (Step s x)
stepL m (Step s x)
beginL s -> m x
doneL) (Fold s -> c -> m (Step s y)
stepR m (Step s y)
beginR s -> m y
doneR) =
(GenericRunner s s x y
-> a -> m (Step (GenericRunner s s x y) (x, y)))
-> m (Step (GenericRunner s s x y) (x, y))
-> (GenericRunner s s x y -> m (x, y))
-> Fold m a (x, y)
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
Fold GenericRunner s s x y
-> a -> m (Step (GenericRunner s s x y) (x, y))
step m (Step (GenericRunner s s x y) (x, y))
begin GenericRunner s s x y -> m (x, y)
done
where
begin :: m (Step (GenericRunner s s x y) (x, y))
begin = do
Step s x
resL <- m (Step s x)
beginL
Step s y
resR <- m (Step s y)
beginR
Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y)))
-> Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall a b. (a -> b) -> a -> b
$ case Step s x
resL of
Partial s
sL ->
GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall s b. s -> Step s b
Partial
(GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y))
-> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall a b. (a -> b) -> a -> b
$ case Step s y
resR of
Partial s
sR -> s -> s -> GenericRunner s s x y
forall sL sR bL bR. sL -> sR -> GenericRunner sL sR bL bR
RunBoth s
sL s
sR
Done y
bR -> s -> y -> GenericRunner s s x y
forall sL sR bL bR. sL -> bR -> GenericRunner sL sR bL bR
RunLeft s
sL y
bR
Done x
bL ->
case Step s y
resR of
Partial s
sR -> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall s b. s -> Step s b
Partial (GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y))
-> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall a b. (a -> b) -> a -> b
$ x -> s -> GenericRunner s s x y
forall sL sR bL bR. bL -> sR -> GenericRunner sL sR bL bR
RunRight x
bL s
sR
Done y
bR -> (x, y) -> Step (GenericRunner s s x y) (x, y)
forall s b. b -> Step s b
Done (x
bL, y
bR)
step :: GenericRunner s s x y
-> a -> m (Step (GenericRunner s s x y) (x, y))
step (RunBoth s
sL s
sR) a
a = do
(b
b, c
c) <- a -> m (b, c)
f a
a
Step s x
resL <- s -> b -> m (Step s x)
stepL s
sL b
b
Step s y
resR <- s -> c -> m (Step s y)
stepR s
sR c
c
case Step s x
resL of
Partial s
sresL ->
Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y)))
-> Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall a b. (a -> b) -> a -> b
$ GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall s b. s -> Step s b
Partial
(GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y))
-> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall a b. (a -> b) -> a -> b
$ case Step s y
resR of
Partial s
sresR -> s -> s -> GenericRunner s s x y
forall sL sR bL bR. sL -> sR -> GenericRunner sL sR bL bR
RunBoth s
sresL s
sresR
Done y
bresR -> s -> y -> GenericRunner s s x y
forall sL sR bL bR. sL -> bR -> GenericRunner sL sR bL bR
RunLeft s
sresL y
bresR
Done x
bresL ->
Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y)))
-> Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall a b. (a -> b) -> a -> b
$ case Step s y
resR of
Partial s
sresR -> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall s b. s -> Step s b
Partial (GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y))
-> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall a b. (a -> b) -> a -> b
$ x -> s -> GenericRunner s s x y
forall sL sR bL bR. bL -> sR -> GenericRunner sL sR bL bR
RunRight x
bresL s
sresR
Done y
bresR -> (x, y) -> Step (GenericRunner s s x y) (x, y)
forall s b. b -> Step s b
Done (x
bresL, y
bresR)
step (RunLeft s
sL y
bR) a
a = do
(b
b, c
_) <- a -> m (b, c)
f a
a
Step s x
resL <- s -> b -> m (Step s x)
stepL s
sL b
b
Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y)))
-> Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall a b. (a -> b) -> a -> b
$ case Step s x
resL of
Partial s
sresL -> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall s b. s -> Step s b
Partial (GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y))
-> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall a b. (a -> b) -> a -> b
$ s -> y -> GenericRunner s s x y
forall sL sR bL bR. sL -> bR -> GenericRunner sL sR bL bR
RunLeft s
sresL y
bR
Done x
bresL -> (x, y) -> Step (GenericRunner s s x y) (x, y)
forall s b. b -> Step s b
Done (x
bresL, y
bR)
step (RunRight x
bL s
sR) a
a = do
(b
_, c
c) <- a -> m (b, c)
f a
a
Step s y
resR <- s -> c -> m (Step s y)
stepR s
sR c
c
Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y)))
-> Step (GenericRunner s s x y) (x, y)
-> m (Step (GenericRunner s s x y) (x, y))
forall a b. (a -> b) -> a -> b
$ case Step s y
resR of
Partial s
sresR -> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall s b. s -> Step s b
Partial (GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y))
-> GenericRunner s s x y -> Step (GenericRunner s s x y) (x, y)
forall a b. (a -> b) -> a -> b
$ x -> s -> GenericRunner s s x y
forall sL sR bL bR. bL -> sR -> GenericRunner sL sR bL bR
RunRight x
bL s
sresR
Done y
bresR -> (x, y) -> Step (GenericRunner s s x y) (x, y)
forall s b. b -> Step s b
Done (x
bL, y
bresR)
done :: GenericRunner s s x y -> m (x, y)
done (RunBoth s
sL s
sR) = (,) (x -> y -> (x, y)) -> m x -> m (y -> (x, y))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m x
doneL s
sL m (y -> (x, y)) -> m y -> m (x, y)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> s -> m y
doneR s
sR
done (RunLeft s
sL y
bR) = (,y
bR) (x -> (x, y)) -> m x -> m (x, y)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m x
doneL s
sL
done (RunRight x
bL s
sR) = (x
bL,) (y -> (x, y)) -> m y -> m (x, y)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m y
doneR s
sR
{-# INLINE unzipWithFstM #-}
unzipWithFstM ::
(a -> m (b,c)) -> Fold m b x -> Fold m c y -> Fold m a (x,y)
unzipWithFstM :: (a -> m (b, c)) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
unzipWithFstM = (a -> m (b, c)) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
forall a. HasCallStack => a
undefined
{-# INLINE unzipWithMinM #-}
unzipWithMinM ::
(a -> m (b,c)) -> Fold m b x -> Fold m c y -> Fold m a (x,y)
unzipWithMinM :: (a -> m (b, c)) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
unzipWithMinM = (a -> m (b, c)) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
forall a. HasCallStack => a
undefined
{-# INLINE unzipWith #-}
unzipWith :: Monad m
=> (a -> (b,c)) -> Fold m b x -> Fold m c y -> Fold m a (x,y)
unzipWith :: (a -> (b, c)) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
unzipWith a -> (b, c)
f = (a -> m (b, c)) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
forall (m :: * -> *) a b c x y.
Monad m =>
(a -> m (b, c)) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
unzipWithM ((b, c) -> m (b, c)
forall (m :: * -> *) a. Monad m => a -> m a
return ((b, c) -> m (b, c)) -> (a -> (b, c)) -> a -> m (b, c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> (b, c)
f)
{-# INLINE unzip #-}
unzip :: Monad m => Fold m a x -> Fold m b y -> Fold m (a,b) (x,y)
unzip :: Fold m a x -> Fold m b y -> Fold m (a, b) (x, y)
unzip = ((a, b) -> (a, b))
-> Fold m a x -> Fold m b y -> Fold m (a, b) (x, y)
forall (m :: * -> *) a b c x y.
Monad m =>
(a -> (b, c)) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
unzipWith (a, b) -> (a, b)
forall a. a -> a
id
{-# INLINE zipWithM #-}
zipWithM ::
(a -> b -> m c) -> t m a -> Fold m c x -> Fold m b x
zipWithM :: (a -> b -> m c) -> t m a -> Fold m c x -> Fold m b x
zipWithM = (a -> b -> m c) -> t m a -> Fold m c x -> Fold m b x
forall a. HasCallStack => a
undefined
{-# INLINE zip #-}
zip :: Monad m => t m a -> Fold m (a, b) x -> Fold m b x
zip :: t m a -> Fold m (a, b) x -> Fold m b x
zip = (a -> b -> m (a, b)) -> t m a -> Fold m (a, b) x -> Fold m b x
forall a b (m :: * -> *) c (t :: (* -> *) -> * -> *) x.
(a -> b -> m c) -> t m a -> Fold m c x -> Fold m b x
zipWithM (((a, b) -> m (a, b)) -> a -> b -> m (a, b)
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (a, b) -> m (a, b)
forall (m :: * -> *) a. Monad m => a -> m a
return)
{-# INLINE indexed #-}
indexed :: forall m a b. Monad m => Fold m (Int, a) b -> Fold m a b
indexed :: Fold m (Int, a) b -> Fold m a b
indexed = SerialT m Int -> Fold m (Int, a) b -> Fold m a b
forall (m :: * -> *) (t :: (* -> *) -> * -> *) a b x.
Monad m =>
t m a -> Fold m (a, b) x -> Fold m b x
zip (Int -> SerialT m Int
forall a (t :: (* -> *) -> * -> *) (m :: * -> *).
(Enumerable a, IsStream t, Monad m) =>
a -> t m a
Stream.enumerateFrom Int
0 :: SerialT m Int)
{-# INLINE with #-}
with ::
(Fold m (s, a) b -> Fold m a b)
-> (((s, a) -> c) -> Fold m (s, a) b -> Fold m (s, a) b)
-> (((s, a) -> c) -> Fold m a b -> Fold m a b)
with :: (Fold m (s, a) b -> Fold m a b)
-> (((s, a) -> c) -> Fold m (s, a) b -> Fold m (s, a) b)
-> ((s, a) -> c)
-> Fold m a b
-> Fold m a b
with Fold m (s, a) b -> Fold m a b
f ((s, a) -> c) -> Fold m (s, a) b -> Fold m (s, a) b
comb (s, a) -> c
g = Fold m (s, a) b -> Fold m a b
f (Fold m (s, a) b -> Fold m a b)
-> (Fold m a b -> Fold m (s, a) b) -> Fold m a b -> Fold m a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((s, a) -> c) -> Fold m (s, a) b -> Fold m (s, a) b
comb (s, a) -> c
g (Fold m (s, a) b -> Fold m (s, a) b)
-> (Fold m a b -> Fold m (s, a) b) -> Fold m a b -> Fold m (s, a) b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((s, a) -> a) -> Fold m a b -> Fold m (s, a) b
forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
map (s, a) -> a
forall a b. (a, b) -> b
snd
{-# INLINE sampleFromthen #-}
sampleFromthen :: Monad m => Int -> Int -> Fold m a b -> Fold m a b
sampleFromthen :: Int -> Int -> Fold m a b -> Fold m a b
sampleFromthen Int
offset Int
size =
(Fold m (Int, a) b -> Fold m a b)
-> (((Int, a) -> Bool) -> Fold m (Int, a) b -> Fold m (Int, a) b)
-> ((Int, a) -> Bool)
-> Fold m a b
-> Fold m a b
forall (m :: * -> *) s a b c.
(Fold m (s, a) b -> Fold m a b)
-> (((s, a) -> c) -> Fold m (s, a) b -> Fold m (s, a) b)
-> ((s, a) -> c)
-> Fold m a b
-> Fold m a b
with Fold m (Int, a) b -> Fold m a b
forall (m :: * -> *) a b.
Monad m =>
Fold m (Int, a) b -> Fold m a b
indexed ((Int, a) -> Bool) -> Fold m (Int, a) b -> Fold m (Int, a) b
forall (m :: * -> *) a r.
Monad m =>
(a -> Bool) -> Fold m a r -> Fold m a r
filter (\(Int
i, a
_) -> (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
offset) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
size Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0)
{-# INLINE concatSequence #-}
concatSequence ::
Fold m b c -> t (Fold m a b) -> Fold m a c
concatSequence :: Fold m b c -> t (Fold m a b) -> Fold m a c
concatSequence Fold m b c
_f t (Fold m a b)
_p = Fold m a c
forall a. HasCallStack => a
undefined
{-# INLINE chunksBetween #-}
chunksBetween ::
Int -> Int -> Fold m a b -> Fold m b c -> Fold m a c
chunksBetween :: Int -> Int -> Fold m a b -> Fold m b c -> Fold m a c
chunksBetween Int
_low Int
_high Fold m a b
_f1 Fold m b c
_f2 = Fold m a c
forall a. HasCallStack => a
undefined
{-# INLINE toStream #-}
toStream :: Monad m => Fold m a (SerialT Identity a)
toStream :: Fold m a (SerialT Identity a)
toStream = (a -> SerialT Identity a -> SerialT Identity a)
-> SerialT Identity a -> Fold m a (SerialT Identity a)
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Fold m a b
foldr a -> SerialT Identity a -> SerialT Identity a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
K.cons SerialT Identity a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
K.nil
{-# INLINABLE toStreamRev #-}
toStreamRev :: Monad m => Fold m a (SerialT Identity a)
toStreamRev :: Fold m a (SerialT Identity a)
toStreamRev = (SerialT Identity a -> a -> SerialT Identity a)
-> SerialT Identity a -> Fold m a (SerialT Identity a)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' ((a -> SerialT Identity a -> SerialT Identity a)
-> SerialT Identity a -> a -> SerialT Identity a
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> SerialT Identity a -> SerialT Identity a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
K.cons) SerialT Identity a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
K.nil