{-# LANGUAGE CPP #-}
module Streamly.Internal.Data.Fold.Type
(
module Streamly.Internal.Data.Fold.Step
, Fold (..)
, foldl'
, foldlM'
, foldl1'
, foldlM1'
, foldt'
, foldtM'
, foldr'
, foldrM'
, fromPure
, fromEffect
, fromRefold
, drain
, toList
, toStreamK
, toStreamKRev
, lengthGeneric
, length
, rmapM
, lmap
, lmapM
, postscan
, catMaybes
, scanMaybe
, filter
, filtering
, filterM
, catLefts
, catRights
, catEithers
, take
, taking
, takeEndBy_
, takeEndBy
, dropping
, splitWith
, split_
, ManyState
, many
, manyPost
, groupsOf
, refoldMany
, refoldMany1
, concatMap
, duplicate
, refold
, teeWith
, teeWithFst
, teeWithMin
, shortest
, longest
, extractM
, reduce
, snoc
, addOne
, snocM
, snocl
, snoclM
, close
, isClosed
, morphInner
, generalizeInner
, foldr
, serialWith
)
where
#include "inline.hs"
#if !MIN_VERSION_base(4,18,0)
import Control.Applicative (liftA2)
#endif
import Control.Monad ((>=>), void)
import Data.Bifunctor (Bifunctor(..))
import Data.Either (fromLeft, fromRight, isLeft, isRight)
import Data.Functor.Identity (Identity(..))
import Fusion.Plugin.Types (Fuse(..))
import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
import Streamly.Internal.Data.Refold.Type (Refold(..))
import qualified Streamly.Internal.Data.StreamK.Type as K
import Prelude hiding (Foldable(..), concatMap, filter, map, take)
import Streamly.Internal.Data.Fold.Step
#include "DocTestDataFold.hs"
data Fold m a b =
forall s. Fold (s -> a -> m (Step s b)) (m (Step s b)) (s -> m b) (s -> m b)
{-# INLINE rmapM #-}
rmapM :: Monad m => (b -> m c) -> Fold m a b -> Fold m a c
rmapM :: forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> Fold m a b -> Fold m a c
rmapM b -> m c
f (Fold s -> a -> m (Step s b)
step m (Step s b)
initial s -> m b
extract s -> m b
final) =
(s -> a -> m (Step s c))
-> m (Step s c) -> (s -> m c) -> (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) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s c)
step1 m (Step s c)
initial1 (s -> m b
extract (s -> m b) -> (b -> m c) -> s -> m c
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> b -> m c
f) (s -> m b
final (s -> m b) -> (b -> m c) -> s -> m c
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> b -> m c
f)
where
initial1 :: m (Step s c)
initial1 = m (Step s b)
initial m (Step s b) -> (Step s b -> m (Step s c)) -> m (Step s c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (b -> m c) -> Step s b -> m (Step s c)
forall (m :: * -> *) a b s.
Applicative m =>
(a -> m b) -> Step s a -> m (Step s b)
mapMStep b -> m c
f
step1 :: s -> a -> m (Step s c)
step1 s
s a
a = s -> a -> m (Step s b)
step s
s a
a m (Step s b) -> (Step s b -> m (Step s c)) -> m (Step s c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (b -> m c) -> Step s b -> m (Step s c)
forall (m :: * -> *) a b s.
Applicative m =>
(a -> m b) -> Step s a -> m (Step s b)
mapMStep b -> m c
f
{-# INLINE foldl' #-}
foldl' :: Monad m => (b -> a -> b) -> b -> Fold m a b
foldl' :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' b -> a -> b
step b
initial =
(b -> a -> m (Step b b))
-> m (Step b b) -> (b -> m b) -> (b -> m b) -> Fold m a b
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
(\b
s a
a -> Step b b -> m (Step b b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step b b -> m (Step b b)) -> Step b b -> m (Step b b)
forall a b. (a -> b) -> a -> b
$ b -> Step b b
forall s b. s -> Step s b
Partial (b -> Step b b) -> b -> Step b b
forall a b. (a -> b) -> a -> b
$ b -> a -> b
step b
s a
a)
(Step b b -> m (Step b b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Step b b
forall s b. s -> Step s b
Partial b
initial))
b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
{-# INLINE foldlM' #-}
foldlM' :: Monad m => (b -> a -> m b) -> m b -> Fold m a b
foldlM' :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
foldlM' b -> a -> m b
step m b
initial =
(b -> a -> m (Step b b))
-> m (Step b b) -> (b -> m b) -> (b -> m b) -> Fold m a b
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 (\b
s a
a -> b -> Step b b
forall s b. s -> Step s b
Partial (b -> Step b b) -> m b -> m (Step b b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> b -> a -> m b
step b
s a
a) (b -> Step b b
forall s b. s -> Step s b
Partial (b -> Step b b) -> m b -> m (Step b b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m b
initial) b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
{-# INLINE foldl1' #-}
foldl1' :: Monad m => (a -> a -> a) -> Fold m a (Maybe a)
foldl1' :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Fold m a (Maybe a)
foldl1' a -> a -> a
step = (Maybe' a -> Maybe a) -> Fold m a (Maybe' a) -> Fold m a (Maybe a)
forall a b. (a -> b) -> Fold m a a -> Fold m a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe' a -> Maybe a
forall a. Maybe' a -> Maybe a
toMaybe (Fold m a (Maybe' a) -> Fold m a (Maybe a))
-> Fold m a (Maybe' a) -> Fold m a (Maybe a)
forall a b. (a -> b) -> a -> b
$ (Maybe' a -> a -> Maybe' a) -> Maybe' a -> Fold m a (Maybe' a)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' Maybe' a -> a -> Maybe' a
step1 Maybe' a
forall a. Maybe' a
Nothing'
where
step1 :: Maybe' a -> a -> Maybe' a
step1 Maybe' a
Nothing' a
a = a -> Maybe' a
forall a. a -> Maybe' a
Just' a
a
step1 (Just' a
x) a
a = a -> Maybe' a
forall a. a -> Maybe' a
Just' (a -> Maybe' a) -> a -> Maybe' a
forall a b. (a -> b) -> a -> b
$ a -> a -> a
step a
x a
a
{-# INLINE foldlM1' #-}
foldlM1' :: Monad m => (a -> a -> m a) -> Fold m a (Maybe a)
foldlM1' :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Fold m a (Maybe a)
foldlM1' a -> a -> m a
step = (Maybe' a -> Maybe a) -> Fold m a (Maybe' a) -> Fold m a (Maybe a)
forall a b. (a -> b) -> Fold m a a -> Fold m a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe' a -> Maybe a
forall a. Maybe' a -> Maybe a
toMaybe (Fold m a (Maybe' a) -> Fold m a (Maybe a))
-> Fold m a (Maybe' a) -> Fold m a (Maybe a)
forall a b. (a -> b) -> a -> b
$ (Maybe' a -> a -> m (Maybe' a))
-> m (Maybe' a) -> Fold m a (Maybe' a)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
foldlM' Maybe' a -> a -> m (Maybe' a)
step1 (Maybe' a -> m (Maybe' a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe' a
forall a. Maybe' a
Nothing')
where
step1 :: Maybe' a -> a -> m (Maybe' a)
step1 Maybe' a
Nothing' a
a = Maybe' a -> m (Maybe' a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe' a -> m (Maybe' a)) -> Maybe' a -> m (Maybe' a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe' a
forall a. a -> Maybe' a
Just' a
a
step1 (Just' a
x) a
a = a -> Maybe' a
forall a. a -> Maybe' a
Just' (a -> Maybe' a) -> m a -> m (Maybe' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> a -> m a
step a
x a
a
{-# INLINE foldr' #-}
foldr' :: Monad m => (a -> b -> b) -> b -> Fold m a b
foldr' :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Fold m a b
foldr' a -> b -> b
f b
z = ((b -> b) -> b) -> Fold m a (b -> b) -> Fold m a b
forall a b. (a -> b) -> Fold m a a -> Fold m a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((b -> b) -> b -> b
forall a b. (a -> b) -> a -> b
$ b
z) (Fold m a (b -> b) -> Fold m a b)
-> Fold m a (b -> b) -> Fold m a b
forall a b. (a -> b) -> a -> b
$ ((b -> b) -> a -> b -> b) -> (b -> b) -> Fold m a (b -> b)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' (\b -> b
g a
x -> b -> b
g (b -> b) -> (b -> b) -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> b
f a
x) b -> b
forall a. a -> a
id
{-# DEPRECATED foldr "Please use foldr' instead." #-}
{-# INLINE foldr #-}
foldr :: Monad m => (a -> b -> b) -> b -> Fold m a b
foldr :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Fold m a b
foldr = (a -> b -> b) -> b -> Fold m a b
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Fold m a b
foldr'
{-# INLINE foldrM' #-}
foldrM' :: Monad m => (a -> b -> m b) -> m b -> Fold m a b
foldrM' :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m b) -> m b -> Fold m a b
foldrM' a -> b -> m b
g m b
z =
((b -> m b) -> m b) -> Fold m a (b -> 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
z m b -> (b -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=) (Fold m a (b -> m b) -> Fold m a b)
-> Fold m a (b -> m b) -> Fold m a b
forall a b. (a -> b) -> a -> b
$ ((b -> m b) -> a -> m (b -> m b))
-> m (b -> m b) -> Fold m a (b -> m b)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
foldlM' (\b -> m b
f a
x -> (b -> m b) -> m (b -> m b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ((b -> m b) -> m (b -> m b)) -> (b -> m b) -> m (b -> m b)
forall a b. (a -> b) -> a -> b
$ a -> b -> m b
g a
x (b -> m b) -> (b -> m b) -> b -> m b
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> b -> m b
f) ((b -> m b) -> m (b -> m b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return)
{-# INLINE foldt' #-}
foldt' :: Monad m => (s -> a -> Step s b) -> Step s b -> (s -> b) -> Fold m a b
foldt' :: forall (m :: * -> *) s a b.
Monad m =>
(s -> a -> Step s b) -> Step s b -> (s -> b) -> Fold m a b
foldt' s -> a -> Step s b
step Step s b
initial s -> b
extract =
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m 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) -> (s -> m b) -> Fold m a b
Fold
(\s
s a
a -> Step s b -> m (Step s b)
forall a. a -> m a
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 -> a -> Step s b
step s
s a
a)
(Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step s b
initial)
(b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> m b) -> (s -> b) -> s -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> b
extract)
(b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> m b) -> (s -> b) -> s -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> b
extract)
{-# INLINE foldtM' #-}
foldtM' :: (s -> a -> m (Step s b)) -> m (Step s b) -> (s -> m b) -> Fold m a b
foldtM' :: forall s a (m :: * -> *) b.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
foldtM' s -> a -> m (Step s b)
step m (Step s b)
initial s -> m b
extract = (s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m 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) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step m (Step s b)
initial s -> m b
extract s -> m b
extract
fromRefold :: Refold m c a b -> c -> Fold m a b
fromRefold :: forall (m :: * -> *) c a b. Refold m c a b -> c -> Fold m a b
fromRefold (Refold s -> a -> m (Step s b)
step c -> m (Step s b)
inject s -> m b
extract) c
c =
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m 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) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step (c -> m (Step s b)
inject c
c) s -> m b
extract s -> m b
extract
{-# INLINE drain #-}
drain :: Monad m => Fold m a ()
drain :: forall (m :: * -> *) a. Monad m => Fold m a ()
drain = (() -> a -> ()) -> () -> Fold m a ()
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' (\()
_ a
_ -> ()) ()
{-# INLINE toList #-}
toList :: Monad m => Fold m a [a]
toList :: forall (m :: * -> *) a. Monad m => Fold m a [a]
toList = (a -> [a] -> [a]) -> [a] -> Fold m a [a]
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Fold m a b
foldr' (:) []
{-# INLINE toStreamKRev #-}
toStreamKRev :: Monad m => Fold m a (K.StreamK n a)
toStreamKRev :: forall (m :: * -> *) a (n :: * -> *).
Monad m =>
Fold m a (StreamK n a)
toStreamKRev = (StreamK n a -> a -> StreamK n a)
-> StreamK n a -> Fold m a (StreamK n a)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' ((a -> StreamK n a -> StreamK n a)
-> StreamK n a -> a -> StreamK n a
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> StreamK n a -> StreamK n a
forall a (m :: * -> *). a -> StreamK m a -> StreamK m a
K.cons) StreamK n a
forall (m :: * -> *) a. StreamK m a
K.nil
{-# INLINE toStreamK #-}
toStreamK :: Monad m => Fold m a (K.StreamK n a)
toStreamK :: forall (m :: * -> *) a (n :: * -> *).
Monad m =>
Fold m a (StreamK n a)
toStreamK = (a -> StreamK n a -> StreamK n a)
-> StreamK n a -> Fold m a (StreamK n a)
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Fold m a b
foldr a -> StreamK n a -> StreamK n a
forall a (m :: * -> *). a -> StreamK m a -> StreamK m a
K.cons StreamK n a
forall (m :: * -> *) a. StreamK m a
K.nil
{-# INLINE lengthGeneric #-}
lengthGeneric :: (Monad m, Num b) => Fold m a b
lengthGeneric :: forall (m :: * -> *) b a. (Monad m, Num b) => Fold m a b
lengthGeneric = (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 :: forall (m :: * -> *) a. Monad m => Fold m a Int
length = Fold m a Int
forall (m :: * -> *) b a. (Monad m, Num b) => Fold m a b
lengthGeneric
instance Functor m => Functor (Fold m a) where
{-# INLINE fmap #-}
fmap :: forall a b. (a -> b) -> Fold m a a -> Fold m a b
fmap a -> b
f (Fold s -> a -> m (Step s a)
step1 m (Step s a)
initial1 s -> m a
extract s -> m a
final) =
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m 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) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step m (Step s b)
initial ((a -> b) -> (s -> m a) -> s -> m b
forall {f :: * -> *} {f :: * -> *} {a} {b}.
(Functor f, Functor f) =>
(a -> b) -> f (f a) -> f (f b)
fmap2 a -> b
f s -> m a
extract) ((a -> b) -> (s -> m a) -> s -> m b
forall {f :: * -> *} {f :: * -> *} {a} {b}.
(Functor f, Functor f) =>
(a -> b) -> f (f a) -> f (f b)
fmap2 a -> b
f s -> m a
final)
where
initial :: m (Step s b)
initial = (a -> b) -> m (Step s a) -> m (Step s b)
forall {f :: * -> *} {f :: * -> *} {a} {b}.
(Functor f, Functor f) =>
(a -> b) -> f (f a) -> f (f b)
fmap2 a -> b
f m (Step s a)
initial1
step :: s -> a -> m (Step s b)
step s
s a
b = (a -> b) -> m (Step s a) -> m (Step s b)
forall {f :: * -> *} {f :: * -> *} {a} {b}.
(Functor f, Functor f) =>
(a -> b) -> f (f a) -> f (f b)
fmap2 a -> b
f (s -> a -> m (Step s a)
step1 s
s a
b)
fmap2 :: (a -> b) -> f (f a) -> f (f b)
fmap2 a -> b
g = (f a -> f b) -> f (f a) -> f (f b)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> f a -> f b
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
g)
{-# INLINE fromPure #-}
fromPure :: Applicative m => b -> Fold m a b
fromPure :: forall (m :: * -> *) b a. Applicative m => b -> Fold m a b
fromPure b
b = (b -> a -> m (Step b b))
-> m (Step b b) -> (b -> m b) -> (b -> m b) -> Fold m a b
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 b -> a -> m (Step b b)
forall a. HasCallStack => a
undefined (Step b b -> m (Step b b)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step b b -> m (Step b b)) -> Step b b -> m (Step b b)
forall a b. (a -> b) -> a -> b
$ b -> Step b b
forall s b. b -> Step s b
Done b
b) b -> m b
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure b -> m b
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# INLINE fromEffect #-}
fromEffect :: Applicative m => m b -> Fold m a b
fromEffect :: forall (m :: * -> *) b a. Applicative m => m b -> Fold m a b
fromEffect m b
b = (b -> a -> m (Step b b))
-> m (Step b b) -> (b -> m b) -> (b -> m b) -> Fold m a b
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 b -> a -> m (Step b b)
forall a. HasCallStack => a
undefined (b -> Step b b
forall s b. b -> Step s b
Done (b -> Step b b) -> m b -> m (Step b b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m b
b) b -> m b
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure b -> m b
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# ANN type SeqFoldState Fuse #-}
data SeqFoldState sl f sr = SeqFoldL !sl | SeqFoldR !f !sr
{-# INLINE splitWith #-}
splitWith :: Monad m =>
(a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
splitWith :: forall (m :: * -> *) a b c x.
Monad m =>
(a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
splitWith a -> b -> c
func
(Fold s -> x -> m (Step s a)
stepL m (Step s a)
initialL s -> m a
_ s -> m a
finalL)
(Fold s -> x -> m (Step s b)
stepR m (Step s b)
initialR s -> m b
_ s -> m b
finalR) =
(SeqFoldState s (b -> c) s
-> x -> m (Step (SeqFoldState s (b -> c) s) c))
-> m (Step (SeqFoldState s (b -> c) s) c)
-> (SeqFoldState s (b -> c) s -> m c)
-> (SeqFoldState s (b -> c) s -> m c)
-> Fold m x c
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 SeqFoldState s (b -> c) s
-> x -> m (Step (SeqFoldState s (b -> c) s) c)
step m (Step (SeqFoldState s (b -> c) s) c)
initial SeqFoldState s (b -> c) s -> m c
forall {p} {a}. p -> a
extract SeqFoldState s (b -> c) s -> m c
final
where
{-# INLINE runR #-}
runR :: f (p a c) -> (c -> d) -> f (p (SeqFoldState sl (c -> d) a) d)
runR f (p a c)
action c -> d
f = (a -> SeqFoldState sl (c -> d) a)
-> (c -> d) -> p a c -> p (SeqFoldState sl (c -> d) a) d
forall a b c d. (a -> b) -> (c -> d) -> p a c -> p b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap ((c -> d) -> a -> SeqFoldState sl (c -> d) a
forall sl f sr. f -> sr -> SeqFoldState sl f sr
SeqFoldR c -> d
f) c -> d
f (p a c -> p (SeqFoldState sl (c -> d) a) d)
-> f (p a c) -> f (p (SeqFoldState sl (c -> d) a) d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (p a c)
action
{-# INLINE runL #-}
runL :: m (Step s1 a) -> m (Step (SeqFoldState s1 (b -> c) s) c)
runL m (Step s1 a)
action = do
Step s1 a
resL <- m (Step s1 a)
action
(s1 -> m (SeqFoldState s1 (b -> c) s))
-> (a -> m (Step (SeqFoldState s1 (b -> c) s) c))
-> Step s1 a
-> m (Step (SeqFoldState s1 (b -> c) s) c)
forall (m :: * -> *) s1 s2 a b.
Applicative m =>
(s1 -> m s2) -> (a -> m (Step s2 b)) -> Step s1 a -> m (Step s2 b)
chainStepM (SeqFoldState s1 (b -> c) s -> m (SeqFoldState s1 (b -> c) s)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (SeqFoldState s1 (b -> c) s -> m (SeqFoldState s1 (b -> c) s))
-> (s1 -> SeqFoldState s1 (b -> c) s)
-> s1
-> m (SeqFoldState s1 (b -> c) s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s1 -> SeqFoldState s1 (b -> c) s
forall sl f sr. sl -> SeqFoldState sl f sr
SeqFoldL) (m (Step s b) -> (b -> c) -> m (Step (SeqFoldState s1 (b -> c) s) c)
forall {f :: * -> *} {p :: * -> * -> *} {a} {c} {d} {sl}.
(Functor f, Bifunctor p) =>
f (p a c) -> (c -> d) -> f (p (SeqFoldState sl (c -> d) a) d)
runR m (Step s b)
initialR ((b -> c) -> m (Step (SeqFoldState s1 (b -> c) s) c))
-> (a -> b -> c) -> a -> m (Step (SeqFoldState s1 (b -> c) s) c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> c
func) Step s1 a
resL
initial :: m (Step (SeqFoldState s (b -> c) s) c)
initial = m (Step s a) -> m (Step (SeqFoldState s (b -> c) s) c)
forall {s1}.
m (Step s1 a) -> m (Step (SeqFoldState s1 (b -> c) s) c)
runL m (Step s a)
initialL
step :: SeqFoldState s (b -> c) s
-> x -> m (Step (SeqFoldState s (b -> c) s) c)
step (SeqFoldL s
st) x
a = m (Step s a) -> m (Step (SeqFoldState s (b -> c) s) c)
forall {s1}.
m (Step s1 a) -> m (Step (SeqFoldState s1 (b -> c) s) c)
runL (s -> x -> m (Step s a)
stepL s
st x
a)
step (SeqFoldR b -> c
f s
st) x
a = m (Step s b) -> (b -> c) -> m (Step (SeqFoldState s (b -> c) s) c)
forall {f :: * -> *} {p :: * -> * -> *} {a} {c} {d} {sl}.
(Functor f, Bifunctor p) =>
f (p a c) -> (c -> d) -> f (p (SeqFoldState sl (c -> d) a) d)
runR (s -> x -> m (Step s b)
stepR s
st x
a) b -> c
f
extract :: p -> a
extract p
_ = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"splitWith: cannot be used for scanning"
final :: SeqFoldState s (b -> c) s -> m c
final (SeqFoldR b -> c
f s
sR) = (b -> c) -> m b -> m c
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> c
f (s -> m b
finalR s
sR)
final (SeqFoldL s
sL) = do
a
rL <- s -> m a
finalL s
sL
Step s b
res <- m (Step s b)
initialR
(b -> c) -> m b -> m c
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> b -> c
func a
rL)
(m b -> m c) -> m b -> m c
forall a b. (a -> b) -> a -> b
$ case Step s b
res of
Partial s
sR -> s -> m b
finalR s
sR
Done b
rR -> b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return b
rR
{-# DEPRECATED serialWith "Please use \"splitWith\" instead" #-}
{-# INLINE serialWith #-}
serialWith :: Monad m => (a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
serialWith :: forall (m :: * -> *) a b c x.
Monad m =>
(a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
serialWith = (a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
forall (m :: * -> *) a b c x.
Monad m =>
(a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
splitWith
{-# ANN type SeqFoldState_ Fuse #-}
data SeqFoldState_ sl sr = SeqFoldL_ !sl | SeqFoldR_ !sr
{-# INLINE split_ #-}
split_ :: Monad m => Fold m x a -> Fold m x b -> Fold m x b
split_ :: forall (m :: * -> *) x a b.
Monad m =>
Fold m x a -> Fold m x b -> Fold m x b
split_ (Fold s -> x -> m (Step s a)
stepL m (Step s a)
initialL s -> m a
_ s -> m a
finalL) (Fold s -> x -> m (Step s b)
stepR m (Step s b)
initialR s -> m b
_ s -> m b
finalR) =
(SeqFoldState_ s s -> x -> m (Step (SeqFoldState_ s s) b))
-> m (Step (SeqFoldState_ s s) b)
-> (SeqFoldState_ s s -> m b)
-> (SeqFoldState_ s s -> m b)
-> Fold m x b
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 SeqFoldState_ s s -> x -> m (Step (SeqFoldState_ s s) b)
step m (Step (SeqFoldState_ s s) b)
initial SeqFoldState_ s s -> m b
forall {p} {a}. p -> a
extract SeqFoldState_ s s -> m b
final
where
initial :: m (Step (SeqFoldState_ s s) b)
initial = do
Step s a
resL <- m (Step s a)
initialL
case Step s a
resL of
Partial s
sl -> Step (SeqFoldState_ s s) b -> m (Step (SeqFoldState_ s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SeqFoldState_ s s) b -> m (Step (SeqFoldState_ s s) b))
-> Step (SeqFoldState_ s s) b -> m (Step (SeqFoldState_ s s) b)
forall a b. (a -> b) -> a -> b
$ SeqFoldState_ s s -> Step (SeqFoldState_ s s) b
forall s b. s -> Step s b
Partial (SeqFoldState_ s s -> Step (SeqFoldState_ s s) b)
-> SeqFoldState_ s s -> Step (SeqFoldState_ s s) b
forall a b. (a -> b) -> a -> b
$ s -> SeqFoldState_ s s
forall sl sr. sl -> SeqFoldState_ sl sr
SeqFoldL_ s
sl
Done a
_ -> do
Step s b
resR <- m (Step s b)
initialR
Step (SeqFoldState_ s s) b -> m (Step (SeqFoldState_ s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SeqFoldState_ s s) b -> m (Step (SeqFoldState_ s s) b))
-> Step (SeqFoldState_ s s) b -> m (Step (SeqFoldState_ s s) b)
forall a b. (a -> b) -> a -> b
$ (s -> SeqFoldState_ s s) -> Step s b -> Step (SeqFoldState_ s s) b
forall a b c. (a -> b) -> Step a c -> Step b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first s -> SeqFoldState_ s s
forall sl sr. sr -> SeqFoldState_ sl sr
SeqFoldR_ Step s b
resR
step :: SeqFoldState_ s s -> x -> m (Step (SeqFoldState_ s s) b)
step (SeqFoldL_ s
st) x
a = do
Step s a
r <- s -> x -> m (Step s a)
stepL s
st x
a
case Step s a
r of
Partial s
s -> Step (SeqFoldState_ s s) b -> m (Step (SeqFoldState_ s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SeqFoldState_ s s) b -> m (Step (SeqFoldState_ s s) b))
-> Step (SeqFoldState_ s s) b -> m (Step (SeqFoldState_ s s) b)
forall a b. (a -> b) -> a -> b
$ SeqFoldState_ s s -> Step (SeqFoldState_ s s) b
forall s b. s -> Step s b
Partial (s -> SeqFoldState_ s s
forall sl sr. sl -> SeqFoldState_ sl sr
SeqFoldL_ s
s)
Done a
_ -> do
Step s b
resR <- m (Step s b)
initialR
Step (SeqFoldState_ s s) b -> m (Step (SeqFoldState_ s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SeqFoldState_ s s) b -> m (Step (SeqFoldState_ s s) b))
-> Step (SeqFoldState_ s s) b -> m (Step (SeqFoldState_ s s) b)
forall a b. (a -> b) -> a -> b
$ (s -> SeqFoldState_ s s) -> Step s b -> Step (SeqFoldState_ s s) b
forall a b c. (a -> b) -> Step a c -> Step b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first s -> SeqFoldState_ s s
forall sl sr. sr -> SeqFoldState_ sl sr
SeqFoldR_ Step s b
resR
step (SeqFoldR_ s
st) x
a = do
Step s b
resR <- s -> x -> m (Step s b)
stepR s
st x
a
Step (SeqFoldState_ s s) b -> m (Step (SeqFoldState_ s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SeqFoldState_ s s) b -> m (Step (SeqFoldState_ s s) b))
-> Step (SeqFoldState_ s s) b -> m (Step (SeqFoldState_ s s) b)
forall a b. (a -> b) -> a -> b
$ (s -> SeqFoldState_ s s) -> Step s b -> Step (SeqFoldState_ s s) b
forall a b c. (a -> b) -> Step a c -> Step b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first s -> SeqFoldState_ s s
forall sl sr. sr -> SeqFoldState_ sl sr
SeqFoldR_ Step s b
resR
extract :: p -> a
extract p
_ = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"split_: cannot be used for scanning"
final :: SeqFoldState_ s s -> m b
final (SeqFoldR_ s
sR) = s -> m b
finalR s
sR
final (SeqFoldL_ s
sL) = do
a
_ <- s -> m a
finalL s
sL
Step s b
res <- m (Step s b)
initialR
case Step s b
res of
Partial s
sR -> s -> m b
finalR s
sR
Done b
rR -> b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return b
rR
instance Monad m => Applicative (Fold m a) where
{-# INLINE pure #-}
pure :: forall a. a -> Fold m a a
pure = a -> Fold m a a
forall (m :: * -> *) b a. Applicative m => b -> Fold m a b
fromPure
{-# INLINE (<*>) #-}
<*> :: forall a b. Fold m a (a -> b) -> Fold m a a -> Fold m a b
(<*>) = ((a -> b) -> a -> b)
-> Fold m a (a -> b) -> Fold m a a -> 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
splitWith (a -> b) -> a -> b
forall a. a -> a
id
{-# INLINE (*>) #-}
*> :: forall a b. Fold m a a -> Fold m a b -> Fold m a b
(*>) = Fold m a a -> Fold m a b -> Fold m a b
forall (m :: * -> *) x a b.
Monad m =>
Fold m x a -> Fold m x b -> Fold m x b
split_
{-# INLINE liftA2 #-}
liftA2 :: forall a b c.
(a -> b -> c) -> Fold m a a -> Fold m a b -> Fold m a c
liftA2 a -> b -> c
f Fold m a a
x = Fold m a (b -> c) -> Fold m a b -> Fold m a c
forall a b. Fold m a (a -> b) -> Fold m a a -> Fold m a b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>) ((a -> b -> c) -> Fold m a a -> Fold m a (b -> c)
forall a b. (a -> b) -> Fold m a a -> Fold m a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b -> c
f Fold m a a
x)
{-# ANN type TeeState Fuse #-}
data TeeState sL sR bL bR
= TeeBoth !sL !sR
| TeeLeft !bR !sL
| TeeRight !bL !sR
{-# INLINE teeWith #-}
teeWith :: Monad m => (a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
teeWith :: forall (m :: * -> *) a b c x.
Monad m =>
(a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
teeWith a -> b -> c
f
(Fold s -> x -> m (Step s a)
stepL m (Step s a)
initialL s -> m a
extractL s -> m a
finalL)
(Fold s -> x -> m (Step s b)
stepR m (Step s b)
initialR s -> m b
extractR s -> m b
finalR) =
(TeeState s s a b -> x -> m (Step (TeeState s s a b) c))
-> m (Step (TeeState s s a b) c)
-> (TeeState s s a b -> m c)
-> (TeeState s s a b -> m c)
-> Fold m x c
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 TeeState s s a b -> x -> m (Step (TeeState s s a b) c)
step m (Step (TeeState s s a b) c)
initial TeeState s s a b -> m c
extract TeeState s s a b -> m c
final
where
{-# INLINE runBoth #-}
runBoth :: m (Step sL a) -> m (Step sR b) -> m (Step (TeeState sL sR a b) c)
runBoth m (Step sL a)
actionL m (Step sR b)
actionR = do
Step sL a
resL <- m (Step sL a)
actionL
Step sR b
resR <- m (Step sR b)
actionR
Step (TeeState sL sR a b) c -> m (Step (TeeState sL sR a b) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (TeeState sL sR a b) c -> m (Step (TeeState sL sR a b) c))
-> Step (TeeState sL sR a b) c -> m (Step (TeeState sL sR a b) c)
forall a b. (a -> b) -> a -> b
$ case Step sL a
resL of
Partial sL
sl ->
TeeState sL sR a b -> Step (TeeState sL sR a b) c
forall s b. s -> Step s b
Partial
(TeeState sL sR a b -> Step (TeeState sL sR a b) c)
-> TeeState sL sR a b -> Step (TeeState sL sR a b) c
forall a b. (a -> b) -> a -> b
$ case Step sR b
resR of
Partial sR
sr -> sL -> sR -> TeeState sL sR a b
forall sL sR bL bR. sL -> sR -> TeeState sL sR bL bR
TeeBoth sL
sl sR
sr
Done b
br -> b -> sL -> TeeState sL sR a b
forall sL sR bL bR. bR -> sL -> TeeState sL sR bL bR
TeeLeft b
br sL
sl
Done a
bl -> (sR -> TeeState sL sR a b)
-> (b -> c) -> Step sR b -> Step (TeeState sL sR a b) c
forall a b c d. (a -> b) -> (c -> d) -> Step a c -> Step b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (a -> sR -> TeeState sL sR a b
forall sL sR bL bR. bL -> sR -> TeeState sL sR bL bR
TeeRight a
bl) (a -> b -> c
f a
bl) Step sR b
resR
initial :: m (Step (TeeState s s a b) c)
initial = m (Step s a) -> m (Step s b) -> m (Step (TeeState s s a b) c)
forall {m :: * -> *} {sL} {sR}.
Monad m =>
m (Step sL a) -> m (Step sR b) -> m (Step (TeeState sL sR a b) c)
runBoth m (Step s a)
initialL m (Step s b)
initialR
step :: TeeState s s a b -> x -> m (Step (TeeState s s a b) c)
step (TeeBoth s
sL s
sR) x
a = m (Step s a) -> m (Step s b) -> m (Step (TeeState s s a b) c)
forall {m :: * -> *} {sL} {sR}.
Monad m =>
m (Step sL a) -> m (Step sR b) -> m (Step (TeeState sL sR a b) c)
runBoth (s -> x -> m (Step s a)
stepL s
sL x
a) (s -> x -> m (Step s b)
stepR s
sR x
a)
step (TeeLeft b
bR s
sL) x
a = (s -> TeeState s s a b)
-> (a -> c) -> Step s a -> Step (TeeState s s a b) c
forall a b c d. (a -> b) -> (c -> d) -> Step a c -> Step b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (b -> s -> TeeState s s a b
forall sL sR bL bR. bR -> sL -> TeeState sL sR bL bR
TeeLeft b
bR) (a -> b -> c
`f` b
bR) (Step s a -> Step (TeeState s s a b) c)
-> m (Step s a) -> m (Step (TeeState s s a b) c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> x -> m (Step s a)
stepL s
sL x
a
step (TeeRight a
bL s
sR) x
a = (s -> TeeState s s a b)
-> (b -> c) -> Step s b -> Step (TeeState s s a b) c
forall a b c d. (a -> b) -> (c -> d) -> Step a c -> Step b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (a -> s -> TeeState s s a b
forall sL sR bL bR. bL -> sR -> TeeState sL sR bL bR
TeeRight a
bL) (a -> b -> c
f a
bL) (Step s b -> Step (TeeState s s a b) c)
-> m (Step s b) -> m (Step (TeeState s s a b) c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> x -> m (Step s b)
stepR s
sR x
a
extract :: TeeState s s a b -> m c
extract (TeeBoth s
sL s
sR) = a -> b -> c
f (a -> b -> c) -> m a -> m (b -> c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m a
extractL s
sL m (b -> c) -> m b -> m c
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> s -> m b
extractR s
sR
extract (TeeLeft b
bR s
sL) = (a -> b -> c
`f` b
bR) (a -> c) -> m a -> m c
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m a
extractL s
sL
extract (TeeRight a
bL s
sR) = a -> b -> c
f a
bL (b -> c) -> m b -> m c
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
extractR s
sR
final :: TeeState s s a b -> m c
final (TeeBoth s
sL s
sR) = a -> b -> c
f (a -> b -> c) -> m a -> m (b -> c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m a
finalL s
sL m (b -> c) -> m b -> m c
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> s -> m b
finalR s
sR
final (TeeLeft b
bR s
sL) = (a -> b -> c
`f` b
bR) (a -> c) -> m a -> m c
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m a
finalL s
sL
final (TeeRight a
bL s
sR) = a -> b -> c
f a
bL (b -> c) -> m b -> m c
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
finalR s
sR
{-# ANN type TeeFstState Fuse #-}
data TeeFstState sL sR b
= TeeFstBoth !sL !sR
| TeeFstLeft !b !sL
{-# INLINE teeWithFst #-}
teeWithFst :: Monad m =>
(b -> c -> d) -> Fold m a b -> Fold m a c -> Fold m a d
teeWithFst :: forall (m :: * -> *) a b c x.
Monad m =>
(a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
teeWithFst b -> c -> d
f
(Fold s -> a -> m (Step s b)
stepL m (Step s b)
initialL s -> m b
extractL s -> m b
finalL)
(Fold s -> a -> m (Step s c)
stepR m (Step s c)
initialR s -> m c
extractR s -> m c
finalR) =
(TeeFstState s s c -> a -> m (Step (TeeFstState s s c) d))
-> m (Step (TeeFstState s s c) d)
-> (TeeFstState s s c -> m d)
-> (TeeFstState s s c -> m d)
-> Fold m a d
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 TeeFstState s s c -> a -> m (Step (TeeFstState s s c) d)
step m (Step (TeeFstState s s c) d)
initial TeeFstState s s c -> m d
extract TeeFstState s s c -> m d
final
where
{-# INLINE runBoth #-}
runBoth :: m (Step sL b) -> m (Step s c) -> m (Step (TeeFstState sL s c) d)
runBoth m (Step sL b)
actionL m (Step s c)
actionR = do
Step sL b
resL <- m (Step sL b)
actionL
Step s c
resR <- m (Step s c)
actionR
case Step sL b
resL of
Partial sL
sl ->
Step (TeeFstState sL s c) d -> m (Step (TeeFstState sL s c) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (TeeFstState sL s c) d -> m (Step (TeeFstState sL s c) d))
-> Step (TeeFstState sL s c) d -> m (Step (TeeFstState sL s c) d)
forall a b. (a -> b) -> a -> b
$ TeeFstState sL s c -> Step (TeeFstState sL s c) d
forall s b. s -> Step s b
Partial
(TeeFstState sL s c -> Step (TeeFstState sL s c) d)
-> TeeFstState sL s c -> Step (TeeFstState sL s c) d
forall a b. (a -> b) -> a -> b
$ case Step s c
resR of
Partial s
sr -> sL -> s -> TeeFstState sL s c
forall sL sR b. sL -> sR -> TeeFstState sL sR b
TeeFstBoth sL
sl s
sr
Done c
br -> c -> sL -> TeeFstState sL s c
forall sL sR b. b -> sL -> TeeFstState sL sR b
TeeFstLeft c
br sL
sl
Done b
bl -> do
d -> Step (TeeFstState sL s c) d
forall s b. b -> Step s b
Done (d -> Step (TeeFstState sL s c) d)
-> (c -> d) -> c -> Step (TeeFstState sL s c) d
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> c -> d
f b
bl (c -> Step (TeeFstState sL s c) d)
-> m c -> m (Step (TeeFstState sL s c) d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
case Step s c
resR of
Partial s
sr -> s -> m c
finalR s
sr
Done c
br -> c -> m c
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return c
br
initial :: m (Step (TeeFstState s s c) d)
initial = m (Step s b) -> m (Step s c) -> m (Step (TeeFstState s s c) d)
forall {sL}.
m (Step sL b) -> m (Step s c) -> m (Step (TeeFstState sL s c) d)
runBoth m (Step s b)
initialL m (Step s c)
initialR
step :: TeeFstState s s c -> a -> m (Step (TeeFstState s s c) d)
step (TeeFstBoth s
sL s
sR) a
a = m (Step s b) -> m (Step s c) -> m (Step (TeeFstState s s c) d)
forall {sL}.
m (Step sL b) -> m (Step s c) -> m (Step (TeeFstState sL s c) d)
runBoth (s -> a -> m (Step s b)
stepL s
sL a
a) (s -> a -> m (Step s c)
stepR s
sR a
a)
step (TeeFstLeft c
bR s
sL) a
a = (s -> TeeFstState s s c)
-> (b -> d) -> Step s b -> Step (TeeFstState s s c) d
forall a b c d. (a -> b) -> (c -> d) -> Step a c -> Step b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (c -> s -> TeeFstState s s c
forall sL sR b. b -> sL -> TeeFstState sL sR b
TeeFstLeft c
bR) (b -> c -> d
`f` c
bR) (Step s b -> Step (TeeFstState s s c) d)
-> m (Step s b) -> m (Step (TeeFstState s s c) d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> a -> m (Step s b)
stepL s
sL a
a
extract :: TeeFstState s s c -> m d
extract (TeeFstBoth s
sL s
sR) = b -> c -> d
f (b -> c -> d) -> m b -> m (c -> d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
extractL s
sL m (c -> d) -> m c -> m d
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> s -> m c
extractR s
sR
extract (TeeFstLeft c
bR s
sL) = (b -> c -> d
`f` c
bR) (b -> d) -> m b -> m d
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
extractL s
sL
final :: TeeFstState s s c -> m d
final (TeeFstBoth s
sL s
sR) = b -> c -> d
f (b -> c -> d) -> m b -> m (c -> d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
finalL s
sL m (c -> d) -> m c -> m d
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> s -> m c
finalR s
sR
final (TeeFstLeft c
bR s
sL) = (b -> c -> d
`f` c
bR) (b -> d) -> m b -> m d
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
finalL s
sL
{-# INLINE teeWithMin #-}
teeWithMin :: Monad m =>
(b -> c -> d) -> Fold m a b -> Fold m a c -> Fold m a d
teeWithMin :: forall (m :: * -> *) a b c x.
Monad m =>
(a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
teeWithMin b -> c -> d
f
(Fold s -> a -> m (Step s b)
stepL m (Step s b)
initialL s -> m b
extractL s -> m b
finalL)
(Fold s -> a -> m (Step s c)
stepR m (Step s c)
initialR s -> m c
extractR s -> m c
finalR) =
(Tuple' s s -> a -> m (Step (Tuple' s s) d))
-> m (Step (Tuple' s s) d)
-> (Tuple' s s -> m d)
-> (Tuple' s s -> m d)
-> Fold m a d
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' s s -> a -> m (Step (Tuple' s s) d)
step m (Step (Tuple' s s) d)
initial Tuple' s s -> m d
extract Tuple' s s -> m d
final
where
{-# INLINE runBoth #-}
runBoth :: m (Step s b) -> m (Step s c) -> m (Step (Tuple' s s) d)
runBoth m (Step s b)
actionL m (Step s c)
actionR = do
Step s b
resL <- m (Step s b)
actionL
Step s c
resR <- m (Step s c)
actionR
case Step s b
resL of
Partial s
sl -> do
case Step s c
resR of
Partial s
sr -> Step (Tuple' s s) d -> m (Step (Tuple' s s) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple' s s) d -> m (Step (Tuple' s s) d))
-> Step (Tuple' s s) d -> m (Step (Tuple' s s) d)
forall a b. (a -> b) -> a -> b
$ Tuple' s s -> Step (Tuple' s s) d
forall s b. s -> Step s b
Partial (Tuple' s s -> Step (Tuple' s s) d)
-> Tuple' s s -> Step (Tuple' s s) d
forall a b. (a -> b) -> a -> b
$ s -> s -> Tuple' s s
forall a b. a -> b -> Tuple' a b
Tuple' s
sl s
sr
Done c
br -> d -> Step (Tuple' s s) d
forall s b. b -> Step s b
Done (d -> Step (Tuple' s s) d) -> (b -> d) -> b -> Step (Tuple' s s) d
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> c -> d
`f` c
br) (b -> Step (Tuple' s s) d) -> m b -> m (Step (Tuple' s s) d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
finalL s
sl
Done b
bl -> do
d -> Step (Tuple' s s) d
forall s b. b -> Step s b
Done (d -> Step (Tuple' s s) d) -> (c -> d) -> c -> Step (Tuple' s s) d
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> c -> d
f b
bl (c -> Step (Tuple' s s) d) -> m c -> m (Step (Tuple' s s) d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
case Step s c
resR of
Partial s
sr -> s -> m c
finalR s
sr
Done c
br -> c -> m c
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return c
br
initial :: m (Step (Tuple' s s) d)
initial = m (Step s b) -> m (Step s c) -> m (Step (Tuple' s s) d)
runBoth m (Step s b)
initialL m (Step s c)
initialR
step :: Tuple' s s -> a -> m (Step (Tuple' s s) d)
step (Tuple' s
sL s
sR) a
a = m (Step s b) -> m (Step s c) -> m (Step (Tuple' s s) d)
runBoth (s -> a -> m (Step s b)
stepL s
sL a
a) (s -> a -> m (Step s c)
stepR s
sR a
a)
extract :: Tuple' s s -> m d
extract (Tuple' s
sL s
sR) = b -> c -> d
f (b -> c -> d) -> m b -> m (c -> d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
extractL s
sL m (c -> d) -> m c -> m d
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> s -> m c
extractR s
sR
final :: Tuple' s s -> m d
final (Tuple' s
sL s
sR) = b -> c -> d
f (b -> c -> d) -> m b -> m (c -> d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
finalL s
sL m (c -> d) -> m c -> m d
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> s -> m c
finalR s
sR
{-# INLINE shortest #-}
shortest :: Monad m => Fold m x a -> Fold m x b -> Fold m x (Either a b)
shortest :: forall (m :: * -> *) x a b.
Monad m =>
Fold m x a -> Fold m x b -> Fold m x (Either a b)
shortest (Fold s -> x -> m (Step s a)
stepL m (Step s a)
initialL s -> m a
extractL s -> m a
finalL) (Fold s -> x -> m (Step s b)
stepR m (Step s b)
initialR s -> m b
_ s -> m b
finalR) =
(Tuple' s s -> x -> m (Step (Tuple' s s) (Either a b)))
-> m (Step (Tuple' s s) (Either a b))
-> (Tuple' s s -> m (Either a b))
-> (Tuple' s s -> m (Either a b))
-> Fold m x (Either a b)
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' s s -> x -> m (Step (Tuple' s s) (Either a b))
step m (Step (Tuple' s s) (Either a b))
initial Tuple' s s -> m (Either a b)
forall {b} {b}. Tuple' s b -> m (Either a b)
extract Tuple' s s -> m (Either a b)
forall {b}. Tuple' s s -> m (Either a b)
final
where
{-# INLINE runBoth #-}
runBoth :: m (Step s a) -> m (Step s b) -> m (Step (Tuple' s s) (Either a b))
runBoth m (Step s a)
actionL m (Step s b)
actionR = do
Step s a
resL <- m (Step s a)
actionL
Step s b
resR <- m (Step s b)
actionR
case Step s a
resL of
Partial s
sL ->
case Step s b
resR of
Partial s
sR -> Step (Tuple' s s) (Either a b)
-> m (Step (Tuple' s s) (Either a b))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple' s s) (Either a b)
-> m (Step (Tuple' s s) (Either a b)))
-> Step (Tuple' s s) (Either a b)
-> m (Step (Tuple' s s) (Either a b))
forall a b. (a -> b) -> a -> b
$ Tuple' s s -> Step (Tuple' s s) (Either a b)
forall s b. s -> Step s b
Partial (Tuple' s s -> Step (Tuple' s s) (Either a b))
-> Tuple' s s -> Step (Tuple' s s) (Either a b)
forall a b. (a -> b) -> a -> b
$ s -> s -> Tuple' s s
forall a b. a -> b -> Tuple' a b
Tuple' s
sL s
sR
Done b
bR -> s -> m a
finalL s
sL m a
-> m (Step (Tuple' s s) (Either a b))
-> m (Step (Tuple' s s) (Either a b))
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Step (Tuple' s s) (Either a b)
-> m (Step (Tuple' s s) (Either a b))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either a b -> Step (Tuple' s s) (Either a b)
forall s b. b -> Step s b
Done (b -> Either a b
forall a b. b -> Either a b
Right b
bR))
Done a
bL -> do
case Step s b
resR of
Partial s
sR -> m b -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (s -> m b
finalR s
sR)
Done b
_ -> () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Step (Tuple' s s) (Either a b)
-> m (Step (Tuple' s s) (Either a b))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either a b -> Step (Tuple' s s) (Either a b)
forall s b. b -> Step s b
Done (a -> Either a b
forall a b. a -> Either a b
Left a
bL))
initial :: m (Step (Tuple' s s) (Either a b))
initial = m (Step s a) -> m (Step s b) -> m (Step (Tuple' s s) (Either a b))
forall {a} {b}.
m (Step s a) -> m (Step s b) -> m (Step (Tuple' s s) (Either a b))
runBoth m (Step s a)
initialL m (Step s b)
initialR
step :: Tuple' s s -> x -> m (Step (Tuple' s s) (Either a b))
step (Tuple' s
sL s
sR) x
a = m (Step s a) -> m (Step s b) -> m (Step (Tuple' s s) (Either a b))
forall {a} {b}.
m (Step s a) -> m (Step s b) -> m (Step (Tuple' s s) (Either a b))
runBoth (s -> x -> m (Step s a)
stepL s
sL x
a) (s -> x -> m (Step s b)
stepR s
sR x
a)
extract :: Tuple' s b -> m (Either a b)
extract (Tuple' s
sL b
_) = a -> Either a b
forall a b. a -> Either a b
Left (a -> Either a b) -> m a -> m (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m a
extractL s
sL
final :: Tuple' s s -> m (Either a b)
final (Tuple' s
sL s
sR) = a -> Either a b
forall a b. a -> Either a b
Left (a -> Either a b) -> m a -> m (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m a
finalL s
sL m (Either a b) -> m b -> m (Either a b)
forall a b. m a -> m b -> m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* s -> m b
finalR s
sR
{-# ANN type LongestState Fuse #-}
data LongestState sL sR
= LongestBoth !sL !sR
| LongestLeft !sL
| LongestRight !sR
{-# INLINE longest #-}
longest :: Monad m => Fold m x a -> Fold m x b -> Fold m x (Either a b)
longest :: forall (m :: * -> *) x a b.
Monad m =>
Fold m x a -> Fold m x b -> Fold m x (Either a b)
longest
(Fold s -> x -> m (Step s a)
stepL m (Step s a)
initialL s -> m a
_ s -> m a
finalL)
(Fold s -> x -> m (Step s b)
stepR m (Step s b)
initialR s -> m b
_ s -> m b
finalR) =
(LongestState s s -> x -> m (Step (LongestState s s) (Either a b)))
-> m (Step (LongestState s s) (Either a b))
-> (LongestState s s -> m (Either a b))
-> (LongestState s s -> m (Either a b))
-> Fold m x (Either a b)
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 LongestState s s -> x -> m (Step (LongestState s s) (Either a b))
step m (Step (LongestState s s) (Either a b))
forall {b}. m (Step (LongestState s s) (Either a b))
initial LongestState s s -> m (Either a b)
forall {p} {a}. p -> a
extract LongestState s s -> m (Either a b)
final
where
{-# INLINE runBoth #-}
runBoth :: m (Step sL a)
-> m (Step sR c) -> m (Step (LongestState sL sR) (Either a b))
runBoth m (Step sL a)
actionL m (Step sR c)
actionR = do
Step sL a
resL <- m (Step sL a)
actionL
Step sR c
resR <- m (Step sR c)
actionR
Step (LongestState sL sR) (Either a b)
-> m (Step (LongestState sL sR) (Either a b))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (LongestState sL sR) (Either a b)
-> m (Step (LongestState sL sR) (Either a b)))
-> Step (LongestState sL sR) (Either a b)
-> m (Step (LongestState sL sR) (Either a b))
forall a b. (a -> b) -> a -> b
$
case Step sL a
resL of
Partial sL
sL ->
LongestState sL sR -> Step (LongestState sL sR) (Either a b)
forall s b. s -> Step s b
Partial (LongestState sL sR -> Step (LongestState sL sR) (Either a b))
-> LongestState sL sR -> Step (LongestState sL sR) (Either a b)
forall a b. (a -> b) -> a -> b
$
case Step sR c
resR of
Partial sR
sR -> sL -> sR -> LongestState sL sR
forall sL sR. sL -> sR -> LongestState sL sR
LongestBoth sL
sL sR
sR
Done c
_ -> sL -> LongestState sL sR
forall sL sR. sL -> LongestState sL sR
LongestLeft sL
sL
Done a
bL -> (sR -> LongestState sL sR)
-> (c -> Either a b)
-> Step sR c
-> Step (LongestState sL sR) (Either a b)
forall a b c d. (a -> b) -> (c -> d) -> Step a c -> Step b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap sR -> LongestState sL sR
forall sL sR. sR -> LongestState sL sR
LongestRight (Either a b -> c -> Either a b
forall a b. a -> b -> a
const (a -> Either a b
forall a b. a -> Either a b
Left a
bL)) Step sR c
resR
initial :: m (Step (LongestState s s) (Either a b))
initial = m (Step s a)
-> m (Step s b) -> m (Step (LongestState s s) (Either a b))
forall {m :: * -> *} {sL} {a} {sR} {c} {b}.
Monad m =>
m (Step sL a)
-> m (Step sR c) -> m (Step (LongestState sL sR) (Either a b))
runBoth m (Step s a)
initialL m (Step s b)
initialR
step :: LongestState s s -> x -> m (Step (LongestState s s) (Either a b))
step (LongestBoth s
sL s
sR) x
a = m (Step s a)
-> m (Step s b) -> m (Step (LongestState s s) (Either a b))
forall {m :: * -> *} {sL} {a} {sR} {c} {b}.
Monad m =>
m (Step sL a)
-> m (Step sR c) -> m (Step (LongestState sL sR) (Either a b))
runBoth (s -> x -> m (Step s a)
stepL s
sL x
a) (s -> x -> m (Step s b)
stepR s
sR x
a)
step (LongestLeft s
sL) x
a = (s -> LongestState s s)
-> (a -> Either a b)
-> Step s a
-> Step (LongestState s s) (Either a b)
forall a b c d. (a -> b) -> (c -> d) -> Step a c -> Step b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap s -> LongestState s s
forall sL sR. sL -> LongestState sL sR
LongestLeft a -> Either a b
forall a b. a -> Either a b
Left (Step s a -> Step (LongestState s s) (Either a b))
-> m (Step s a) -> m (Step (LongestState s s) (Either a b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> x -> m (Step s a)
stepL s
sL x
a
step (LongestRight s
sR) x
a = (s -> LongestState s s)
-> (b -> Either a b)
-> Step s b
-> Step (LongestState s s) (Either a b)
forall a b c d. (a -> b) -> (c -> d) -> Step a c -> Step b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap s -> LongestState s s
forall sL sR. sR -> LongestState sL sR
LongestRight b -> Either a b
forall a b. b -> Either a b
Right (Step s b -> Step (LongestState s s) (Either a b))
-> m (Step s b) -> m (Step (LongestState s s) (Either a b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> x -> m (Step s b)
stepR s
sR x
a
extract :: p -> a
extract p
_ = [Char] -> a
forall a. HasCallStack => [Char] -> a
error ([Char] -> a) -> [Char] -> a
forall a b. (a -> b) -> a -> b
$ [Char]
"longest: scan is not allowed as longest cannot be "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"determined until one fold has exhausted."
final :: LongestState s s -> m (Either a b)
final (LongestLeft s
sL) = a -> Either a b
forall a b. a -> Either a b
Left (a -> Either a b) -> m a -> m (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m a
finalL s
sL
final (LongestRight s
sR) = b -> Either a b
forall a b. b -> Either a b
Right (b -> Either a b) -> m b -> m (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
finalR s
sR
final (LongestBoth s
sL s
sR) = a -> Either a b
forall a b. a -> Either a b
Left (a -> Either a b) -> m a -> m (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m a
finalL s
sL m (Either a b) -> m b -> m (Either a b)
forall a b. m a -> m b -> m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* s -> m b
finalR s
sR
data ConcatMapState m sa a b c
= B !sa (sa -> m b)
| forall s. C (s -> a -> m (Step s c)) !s (s -> m c) (s -> m c)
{-# INLINE concatMap #-}
concatMap :: Monad m => (b -> Fold m a c) -> Fold m a b -> Fold m a c
concatMap :: forall (m :: * -> *) b a c.
Monad m =>
(b -> Fold m a c) -> Fold m a b -> Fold m a c
concatMap b -> Fold m a c
f (Fold s -> a -> m (Step s b)
stepa m (Step s b)
initiala s -> m b
_ s -> m b
finala) =
(ConcatMapState m s a b c
-> a -> m (Step (ConcatMapState m s a b c) c))
-> m (Step (ConcatMapState m s a b c) c)
-> (ConcatMapState m s a b c -> m c)
-> (ConcatMapState m s a b c -> m c)
-> Fold m a c
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 ConcatMapState m s a b c
-> a -> m (Step (ConcatMapState m s a b c) c)
forall {b}.
ConcatMapState m s a b c
-> a -> m (Step (ConcatMapState m s a b c) c)
stepc m (Step (ConcatMapState m s a b c) c)
initialc ConcatMapState m s a b c -> m c
forall {p} {a}. p -> a
extractc ConcatMapState m s a b c -> m c
forall {sa} {a}. ConcatMapState m sa a b c -> m c
finalc
where
initialc :: m (Step (ConcatMapState m s a b c) c)
initialc = do
Step s b
r <- m (Step s b)
initiala
case Step s b
r of
Partial s
s -> Step (ConcatMapState m s a b c) c
-> m (Step (ConcatMapState m s a b c) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ConcatMapState m s a b c) c
-> m (Step (ConcatMapState m s a b c) c))
-> Step (ConcatMapState m s a b c) c
-> m (Step (ConcatMapState m s a b c) c)
forall a b. (a -> b) -> a -> b
$ ConcatMapState m s a b c -> Step (ConcatMapState m s a b c) c
forall s b. s -> Step s b
Partial (s -> (s -> m b) -> ConcatMapState m s a b c
forall (m :: * -> *) sa a b c.
sa -> (sa -> m b) -> ConcatMapState m sa a b c
B s
s s -> m b
finala)
Done b
b -> Fold m a c -> m (Step (ConcatMapState m s a b c) c)
forall {m :: * -> *} {a} {b} {sa} {b}.
Monad m =>
Fold m a b -> m (Step (ConcatMapState m sa a b b) b)
initInnerFold (b -> Fold m a c
f b
b)
stepc :: ConcatMapState m s a b c
-> a -> m (Step (ConcatMapState m s a b c) c)
stepc (B s
s s -> m b
fin) a
a = do
Step s b
r <- s -> a -> m (Step s b)
stepa s
s a
a
case Step s b
r of
Partial s
s1 -> Step (ConcatMapState m s a b c) c
-> m (Step (ConcatMapState m s a b c) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ConcatMapState m s a b c) c
-> m (Step (ConcatMapState m s a b c) c))
-> Step (ConcatMapState m s a b c) c
-> m (Step (ConcatMapState m s a b c) c)
forall a b. (a -> b) -> a -> b
$ ConcatMapState m s a b c -> Step (ConcatMapState m s a b c) c
forall s b. s -> Step s b
Partial (s -> (s -> m b) -> ConcatMapState m s a b c
forall (m :: * -> *) sa a b c.
sa -> (sa -> m b) -> ConcatMapState m sa a b c
B s
s1 s -> m b
fin)
Done b
b -> Fold m a c -> m (Step (ConcatMapState m s a b c) c)
forall {m :: * -> *} {a} {b} {sa} {b}.
Monad m =>
Fold m a b -> m (Step (ConcatMapState m sa a b b) b)
initInnerFold (b -> Fold m a c
f b
b)
stepc (C s -> a -> m (Step s c)
stepInner s
s s -> m c
extractInner s -> m c
fin) a
a = do
Step s c
r <- s -> a -> m (Step s c)
stepInner s
s a
a
Step (ConcatMapState m s a b c) c
-> m (Step (ConcatMapState m s a b c) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ConcatMapState m s a b c) c
-> m (Step (ConcatMapState m s a b c) c))
-> Step (ConcatMapState m s a b c) c
-> m (Step (ConcatMapState m s a b c) c)
forall a b. (a -> b) -> a -> b
$ case Step s c
r of
Partial s
sc -> ConcatMapState m s a b c -> Step (ConcatMapState m s a b c) c
forall s b. s -> Step s b
Partial ((s -> a -> m (Step s c))
-> s -> (s -> m c) -> (s -> m c) -> ConcatMapState m s a b c
forall (m :: * -> *) sa a b c s.
(s -> a -> m (Step s c))
-> s -> (s -> m c) -> (s -> m c) -> ConcatMapState m sa a b c
C s -> a -> m (Step s c)
stepInner s
sc s -> m c
extractInner s -> m c
fin)
Done c
c -> c -> Step (ConcatMapState m s a b c) c
forall s b. b -> Step s b
Done c
c
extractc :: p -> a
extractc p
_ = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"concatMap: cannot be used for scanning"
initInnerFold :: Fold m a b -> m (Step (ConcatMapState m sa a b b) b)
initInnerFold (Fold s -> a -> m (Step s b)
step m (Step s b)
i s -> m b
e s -> m b
fin) = do
Step s b
r <- m (Step s b)
i
Step (ConcatMapState m sa a b b) b
-> m (Step (ConcatMapState m sa a b b) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ConcatMapState m sa a b b) b
-> m (Step (ConcatMapState m sa a b b) b))
-> Step (ConcatMapState m sa a b b) b
-> m (Step (ConcatMapState m sa a b b) b)
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Partial s
s -> ConcatMapState m sa a b b -> Step (ConcatMapState m sa a b b) b
forall s b. s -> Step s b
Partial ((s -> a -> m (Step s b))
-> s -> (s -> m b) -> (s -> m b) -> ConcatMapState m sa a b b
forall (m :: * -> *) sa a b c s.
(s -> a -> m (Step s c))
-> s -> (s -> m c) -> (s -> m c) -> ConcatMapState m sa a b c
C s -> a -> m (Step s b)
step s
s s -> m b
e s -> m b
fin)
Done b
c -> b -> Step (ConcatMapState m sa a b b) b
forall s b. b -> Step s b
Done b
c
initFinalize :: Fold m a b -> m b
initFinalize (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
Done b
c -> b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return b
c
finalc :: ConcatMapState m sa a b c -> m c
finalc (B sa
s sa -> m b
fin) = do
b
r <- sa -> m b
fin sa
s
Fold m a c -> m c
forall {m :: * -> *} {a} {b}. Monad m => Fold m a b -> m b
initFinalize (b -> Fold m a c
f b
r)
finalc (C s -> a -> m (Step s c)
_ s
sInner s -> m c
_ s -> m c
fin) = s -> m c
fin s
sInner
{-# INLINE lmap #-}
lmap :: (a -> b) -> Fold m b r -> Fold m a r
lmap :: forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
lmap a -> b
f (Fold s -> b -> m (Step s r)
step m (Step s r)
begin s -> m r
done s -> m r
final) = (s -> a -> m (Step s r))
-> m (Step s r) -> (s -> m r) -> (s -> m r) -> Fold m a r
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 r)
step' m (Step s r)
begin s -> m r
done s -> m r
final
where
step' :: s -> a -> m (Step s r)
step' s
x a
a = s -> b -> m (Step s r)
step s
x (a -> b
f a
a)
{-# INLINE lmapM #-}
lmapM :: Monad m => (a -> m b) -> Fold m b r -> Fold m a r
lmapM :: forall (m :: * -> *) a b r.
Monad m =>
(a -> m b) -> Fold m b r -> Fold m a r
lmapM a -> m b
f (Fold s -> b -> m (Step s r)
step m (Step s r)
begin s -> m r
done s -> m r
final) = (s -> a -> m (Step s r))
-> m (Step s r) -> (s -> m r) -> (s -> m r) -> Fold m a r
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 r)
step' m (Step s r)
begin s -> m r
done s -> m r
final
where
step' :: s -> a -> m (Step s r)
step' s
x a
a = a -> m b
f a
a m b -> (b -> m (Step s r)) -> m (Step s r)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> b -> m (Step s r)
step s
x
{-# INLINE postscan #-}
postscan :: Monad m => Fold m a b -> Fold m b c -> Fold m a c
postscan :: forall (m :: * -> *) a b c.
Monad m =>
Fold m a b -> Fold m b c -> Fold m a c
postscan
(Fold s -> a -> m (Step s b)
stepL m (Step s b)
initialL s -> m b
extractL s -> m b
finalL)
(Fold s -> b -> m (Step s c)
stepR m (Step s c)
initialR s -> m c
extractR s -> m c
finalR) =
((s, s) -> a -> m (Step (s, s) c))
-> m (Step (s, s) c)
-> ((s, s) -> m c)
-> ((s, 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) -> (s -> m b) -> Fold m a b
Fold (s, s) -> a -> m (Step (s, s) c)
step m (Step (s, s) c)
initial (s, s) -> m c
forall {a}. (a, s) -> m c
extract (s, s) -> m c
final
where
{-# INLINE runStep #-}
runStep :: m (Step s b) -> s -> m (Step (s, s) c)
runStep m (Step s b)
actionL s
sR = do
Step s b
rL <- m (Step s b)
actionL
case Step s b
rL of
Done b
bL -> do
Step s c
rR <- s -> b -> m (Step s c)
stepR s
sR b
bL
case Step s c
rR of
Partial s
sR1 -> c -> Step (s, s) c
forall s b. b -> Step s b
Done (c -> Step (s, s) c) -> m c -> m (Step (s, s) c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m c
finalR s
sR1
Done c
bR -> Step (s, s) c -> m (Step (s, s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s) c -> m (Step (s, s) c))
-> Step (s, s) c -> m (Step (s, s) c)
forall a b. (a -> b) -> a -> b
$ c -> Step (s, s) c
forall s b. b -> Step s b
Done c
bR
Partial s
sL -> do
!b
b <- s -> m b
extractL s
sL
Step s c
rR <- s -> b -> m (Step s c)
stepR s
sR b
b
case Step s c
rR of
Partial s
sR1 -> Step (s, s) c -> m (Step (s, s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s) c -> m (Step (s, s) c))
-> Step (s, s) c -> m (Step (s, s) c)
forall a b. (a -> b) -> a -> b
$ (s, s) -> Step (s, s) c
forall s b. s -> Step s b
Partial (s
sL, s
sR1)
Done c
bR -> s -> m b
finalL s
sL m b -> m (Step (s, s) c) -> m (Step (s, s) c)
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Step (s, s) c -> m (Step (s, s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> Step (s, s) c
forall s b. b -> Step s b
Done c
bR)
initial :: m (Step (s, s) c)
initial = do
Step s c
rR <- m (Step s c)
initialR
case Step s c
rR of
Partial s
sR -> do
Step s b
rL <- m (Step s b)
initialL
case Step s b
rL of
Done b
_ -> c -> Step (s, s) c
forall s b. b -> Step s b
Done (c -> Step (s, s) c) -> m c -> m (Step (s, s) c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m c
finalR s
sR
Partial s
sL -> Step (s, s) c -> m (Step (s, s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s) c -> m (Step (s, s) c))
-> Step (s, s) c -> m (Step (s, s) c)
forall a b. (a -> b) -> a -> b
$ (s, s) -> Step (s, s) c
forall s b. s -> Step s b
Partial (s
sL, s
sR)
Done c
b -> Step (s, s) c -> m (Step (s, s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s) c -> m (Step (s, s) c))
-> Step (s, s) c -> m (Step (s, s) c)
forall a b. (a -> b) -> a -> b
$ c -> Step (s, s) c
forall s b. b -> Step s b
Done c
b
step :: (s, s) -> a -> m (Step (s, s) c)
step (s
sL, s
sR) a
x = m (Step s b) -> s -> m (Step (s, s) c)
runStep (s -> a -> m (Step s b)
stepL s
sL a
x) s
sR
extract :: (a, s) -> m c
extract = s -> m c
extractR (s -> m c) -> ((a, s) -> s) -> (a, s) -> m c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, s) -> s
forall a b. (a, b) -> b
snd
final :: (s, s) -> m c
final (s
sL, s
sR) = s -> m b
finalL s
sL m b -> m c -> m c
forall a b. m a -> m b -> m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> s -> m c
finalR s
sR
{-# INLINE_NORMAL catMaybes #-}
catMaybes :: Monad m => Fold m a b -> Fold m (Maybe a) b
catMaybes :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Fold m (Maybe a) b
catMaybes (Fold s -> a -> m (Step s b)
step m (Step s b)
initial s -> m b
extract s -> m b
final) = (s -> Maybe a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Fold m (Maybe a) b
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 -> Maybe a -> m (Step s b)
step1 m (Step s b)
initial s -> m b
extract s -> m b
final
where
step1 :: s -> Maybe a -> m (Step s b)
step1 s
s Maybe a
a =
case Maybe a
a of
Maybe a
Nothing -> Step s b -> m (Step s b)
forall a. a -> m a
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
Just a
x -> s -> a -> m (Step s b)
step s
s a
x
{-# INLINE scanMaybe #-}
scanMaybe :: Monad m => Fold m a (Maybe b) -> Fold m b c -> Fold m a c
scanMaybe :: forall (m :: * -> *) a b c.
Monad m =>
Fold m a (Maybe b) -> Fold m b c -> Fold m a c
scanMaybe Fold m a (Maybe b)
f1 Fold m b c
f2 = Fold m a (Maybe b) -> Fold m (Maybe b) c -> Fold m a c
forall (m :: * -> *) a b c.
Monad m =>
Fold m a b -> Fold m b c -> Fold m a c
postscan Fold m a (Maybe b)
f1 (Fold m b c -> Fold m (Maybe b) c
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Fold m (Maybe a) b
catMaybes Fold m b c
f2)
{-# INLINE filtering #-}
filtering :: Monad m => (a -> Bool) -> Fold m a (Maybe a)
filtering :: forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Fold m a (Maybe a)
filtering a -> Bool
f = (Maybe a -> a -> Maybe a) -> Maybe a -> Fold m a (Maybe a)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
foldl' Maybe a -> a -> Maybe a
forall {p}. p -> a -> Maybe a
step Maybe a
forall a. Maybe a
Nothing
where
step :: p -> a -> Maybe a
step p
_ a
a = if a -> Bool
f a
a then a -> Maybe a
forall a. a -> Maybe a
Just a
a else Maybe a
forall a. Maybe a
Nothing
{-# INLINE filter #-}
filter :: Monad m => (a -> Bool) -> Fold m a r -> Fold m a r
filter :: forall (m :: * -> *) a r.
Monad m =>
(a -> Bool) -> Fold m a r -> Fold m a r
filter a -> Bool
f (Fold s -> a -> m (Step s r)
step m (Step s r)
begin s -> m r
extract s -> m r
final) = (s -> a -> m (Step s r))
-> m (Step s r) -> (s -> m r) -> (s -> m r) -> Fold m a r
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 r)
step' m (Step s r)
begin s -> m r
extract s -> m r
final
where
step' :: s -> a -> m (Step s r)
step' s
x a
a = if a -> Bool
f a
a then s -> a -> m (Step s r)
step s
x a
a else Step s r -> m (Step s r)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s r -> m (Step s r)) -> Step s r -> m (Step s r)
forall a b. (a -> b) -> a -> b
$ s -> Step s r
forall s b. s -> Step s b
Partial s
x
{-# INLINE filterM #-}
filterM :: Monad m => (a -> m Bool) -> Fold m a r -> Fold m a r
filterM :: forall (m :: * -> *) a r.
Monad m =>
(a -> m Bool) -> Fold m a r -> Fold m a r
filterM a -> m Bool
f (Fold s -> a -> m (Step s r)
step m (Step s r)
begin s -> m r
extract s -> m r
final) = (s -> a -> m (Step s r))
-> m (Step s r) -> (s -> m r) -> (s -> m r) -> Fold m a r
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 r)
step' m (Step s r)
begin s -> m r
extract s -> m r
final
where
step' :: s -> a -> m (Step s r)
step' s
x a
a = do
Bool
use <- a -> m Bool
f a
a
if Bool
use then s -> a -> m (Step s r)
step s
x a
a else Step s r -> m (Step s r)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s r -> m (Step s r)) -> Step s r -> m (Step s r)
forall a b. (a -> b) -> a -> b
$ s -> Step s r
forall s b. s -> Step s b
Partial s
x
{-# INLINE catLefts #-}
catLefts :: (Monad m) => Fold m a c -> Fold m (Either a b) c
catLefts :: forall (m :: * -> *) a c b.
Monad m =>
Fold m a c -> Fold m (Either a b) c
catLefts = (Either a b -> Bool)
-> Fold m (Either a b) c -> Fold m (Either a b) c
forall (m :: * -> *) a r.
Monad m =>
(a -> Bool) -> Fold m a r -> Fold m a r
filter Either a b -> Bool
forall a b. Either a b -> Bool
isLeft (Fold m (Either a b) c -> Fold m (Either a b) c)
-> (Fold m a c -> Fold m (Either a b) c)
-> Fold m a c
-> Fold m (Either a b) c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Either a b -> a) -> Fold m a c -> Fold m (Either a b) c
forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
lmap (a -> Either a b -> a
forall a b. a -> Either a b -> a
fromLeft a
forall a. HasCallStack => a
undefined)
{-# INLINE catRights #-}
catRights :: (Monad m) => Fold m b c -> Fold m (Either a b) c
catRights :: forall (m :: * -> *) b c a.
Monad m =>
Fold m b c -> Fold m (Either a b) c
catRights = (Either a b -> Bool)
-> Fold m (Either a b) c -> Fold m (Either a b) c
forall (m :: * -> *) a r.
Monad m =>
(a -> Bool) -> Fold m a r -> Fold m a r
filter Either a b -> Bool
forall a b. Either a b -> Bool
isRight (Fold m (Either a b) c -> Fold m (Either a b) c)
-> (Fold m b c -> Fold m (Either a b) c)
-> Fold m b c
-> Fold m (Either a b) c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Either a b -> b) -> Fold m b c -> Fold m (Either a b) c
forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
lmap (b -> Either a b -> b
forall b a. b -> Either a b -> b
fromRight b
forall a. HasCallStack => a
undefined)
{-# INLINE catEithers #-}
catEithers :: Fold m a b -> Fold m (Either a a) b
catEithers :: forall (m :: * -> *) a b. Fold m a b -> Fold m (Either a a) b
catEithers = (Either a a -> a) -> Fold m a b -> Fold m (Either a a) b
forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
lmap ((a -> a) -> (a -> a) -> Either a a -> a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> a
forall a. a -> a
id a -> a
forall a. a -> a
id)
{-# ANN type Tuple'Fused Fuse #-}
data Tuple'Fused a b = Tuple'Fused !a !b deriving Int -> Tuple'Fused a b -> [Char] -> [Char]
[Tuple'Fused a b] -> [Char] -> [Char]
Tuple'Fused a b -> [Char]
(Int -> Tuple'Fused a b -> [Char] -> [Char])
-> (Tuple'Fused a b -> [Char])
-> ([Tuple'Fused a b] -> [Char] -> [Char])
-> Show (Tuple'Fused a b)
forall a.
(Int -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
forall a b.
(Show a, Show b) =>
Int -> Tuple'Fused a b -> [Char] -> [Char]
forall a b.
(Show a, Show b) =>
[Tuple'Fused a b] -> [Char] -> [Char]
forall a b. (Show a, Show b) => Tuple'Fused a b -> [Char]
$cshowsPrec :: forall a b.
(Show a, Show b) =>
Int -> Tuple'Fused a b -> [Char] -> [Char]
showsPrec :: Int -> Tuple'Fused a b -> [Char] -> [Char]
$cshow :: forall a b. (Show a, Show b) => Tuple'Fused a b -> [Char]
show :: Tuple'Fused a b -> [Char]
$cshowList :: forall a b.
(Show a, Show b) =>
[Tuple'Fused a b] -> [Char] -> [Char]
showList :: [Tuple'Fused a b] -> [Char] -> [Char]
Show
{-# INLINE taking #-}
taking :: Monad m => Int -> Fold m a (Maybe a)
taking :: forall (m :: * -> *) a. Monad m => Int -> Fold m a (Maybe a)
taking Int
n = (Tuple'Fused Int (Maybe a)
-> a -> Step (Tuple'Fused Int (Maybe a)) (Maybe a))
-> Step (Tuple'Fused Int (Maybe a)) (Maybe a)
-> (Tuple'Fused Int (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
foldt' Tuple'Fused Int (Maybe a)
-> a -> Step (Tuple'Fused Int (Maybe a)) (Maybe a)
forall {a} {b} {a}.
(Ord a, Num a) =>
Tuple'Fused a b -> a -> Step (Tuple'Fused a (Maybe a)) (Maybe a)
step Step (Tuple'Fused Int (Maybe a)) (Maybe a)
forall {a} {a}. Step (Tuple'Fused Int (Maybe a)) (Maybe a)
initial Tuple'Fused Int (Maybe a) -> Maybe a
forall {a} {b}. Tuple'Fused a b -> b
extract
where
initial :: Step (Tuple'Fused Int (Maybe a)) (Maybe a)
initial =
if Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0
then Maybe a -> Step (Tuple'Fused Int (Maybe a)) (Maybe a)
forall s b. b -> Step s b
Done Maybe a
forall a. Maybe a
Nothing
else Tuple'Fused Int (Maybe a)
-> Step (Tuple'Fused Int (Maybe a)) (Maybe a)
forall s b. s -> Step s b
Partial (Int -> Maybe a -> Tuple'Fused Int (Maybe a)
forall a b. a -> b -> Tuple'Fused a b
Tuple'Fused Int
n Maybe a
forall a. Maybe a
Nothing)
step :: Tuple'Fused a b -> a -> Step (Tuple'Fused a (Maybe a)) (Maybe a)
step (Tuple'Fused a
i b
_) a
a =
if a
i a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
1
then Tuple'Fused a (Maybe a) -> Step (Tuple'Fused a (Maybe a)) (Maybe a)
forall s b. s -> Step s b
Partial (a -> Maybe a -> Tuple'Fused a (Maybe a)
forall a b. a -> b -> Tuple'Fused a b
Tuple'Fused (a
i a -> a -> a
forall a. Num a => a -> a -> a
- a
1) (a -> Maybe a
forall a. a -> Maybe a
Just a
a))
else Maybe a -> Step (Tuple'Fused a (Maybe a)) (Maybe a)
forall s b. b -> Step s b
Done (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
extract :: Tuple'Fused a b -> b
extract (Tuple'Fused a
_ b
r) = b
r
{-# INLINE dropping #-}
dropping :: Monad m => Int -> Fold m a (Maybe a)
dropping :: forall (m :: * -> *) a. Monad m => Int -> Fold m a (Maybe a)
dropping Int
n = (Tuple'Fused Int (Maybe a)
-> a -> Step (Tuple'Fused Int (Maybe a)) (Maybe a))
-> Step (Tuple'Fused Int (Maybe a)) (Maybe a)
-> (Tuple'Fused Int (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
foldt' Tuple'Fused Int (Maybe a)
-> a -> Step (Tuple'Fused Int (Maybe a)) (Maybe a)
forall {a} {b} {a} {b}.
(Ord a, Num a) =>
Tuple'Fused a b -> a -> Step (Tuple'Fused a (Maybe a)) b
step Step (Tuple'Fused Int (Maybe a)) (Maybe a)
forall {a} {b}. Step (Tuple'Fused Int (Maybe a)) b
initial Tuple'Fused Int (Maybe a) -> Maybe a
forall {a} {b}. Tuple'Fused a b -> b
extract
where
initial :: Step (Tuple'Fused Int (Maybe a)) b
initial = Tuple'Fused Int (Maybe a) -> Step (Tuple'Fused Int (Maybe a)) b
forall s b. s -> Step s b
Partial (Int -> Maybe a -> Tuple'Fused Int (Maybe a)
forall a b. a -> b -> Tuple'Fused a b
Tuple'Fused Int
n Maybe a
forall a. Maybe a
Nothing)
step :: Tuple'Fused a b -> a -> Step (Tuple'Fused a (Maybe a)) b
step (Tuple'Fused a
i b
_) a
a =
if a
i a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0
then Tuple'Fused a (Maybe a) -> Step (Tuple'Fused a (Maybe a)) b
forall s b. s -> Step s b
Partial (a -> Maybe a -> Tuple'Fused a (Maybe a)
forall a b. a -> b -> Tuple'Fused a b
Tuple'Fused (a
i a -> a -> a
forall a. Num a => a -> a -> a
- a
1) Maybe a
forall a. Maybe a
Nothing)
else Tuple'Fused a (Maybe a) -> Step (Tuple'Fused a (Maybe a)) b
forall s b. s -> Step s b
Partial (a -> Maybe a -> Tuple'Fused a (Maybe a)
forall a b. a -> b -> Tuple'Fused a b
Tuple'Fused a
i (a -> Maybe a
forall a. a -> Maybe a
Just a
a))
extract :: Tuple'Fused a b -> b
extract (Tuple'Fused a
_ b
r) = b
r
{-# INLINE take #-}
take :: Monad m => Int -> Fold m a b -> Fold m a b
take :: forall (m :: * -> *) a b.
Monad m =>
Int -> Fold m a b -> Fold m a b
take Int
n (Fold s -> a -> m (Step s b)
fstep m (Step s b)
finitial s -> m b
fextract s -> m b
ffinal) = (Tuple'Fused Int s -> a -> m (Step (Tuple'Fused Int s) b))
-> m (Step (Tuple'Fused Int s) b)
-> (Tuple'Fused Int s -> m b)
-> (Tuple'Fused Int 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) -> (s -> m b) -> Fold m a b
Fold Tuple'Fused Int s -> a -> m (Step (Tuple'Fused Int s) b)
step m (Step (Tuple'Fused Int s) b)
initial Tuple'Fused Int s -> m b
forall {a}. Tuple'Fused a s -> m b
extract Tuple'Fused Int s -> m b
forall {a}. Tuple'Fused a s -> m b
final
where
{-# INLINE next #-}
next :: Int -> Step s b -> m (Step (Tuple'Fused Int s) b)
next Int
i Step s b
res =
case Step s b
res of
Partial s
s -> do
let i1 :: Int
i1 = Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
s1 :: Tuple'Fused Int s
s1 = Int -> s -> Tuple'Fused Int s
forall a b. a -> b -> Tuple'Fused a b
Tuple'Fused Int
i1 s
s
if Int
i1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n
then Step (Tuple'Fused Int s) b -> m (Step (Tuple'Fused Int s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple'Fused Int s) b -> m (Step (Tuple'Fused Int s) b))
-> Step (Tuple'Fused Int s) b -> m (Step (Tuple'Fused Int s) b)
forall a b. (a -> b) -> a -> b
$ Tuple'Fused Int s -> Step (Tuple'Fused Int s) b
forall s b. s -> Step s b
Partial Tuple'Fused Int s
s1
else b -> Step (Tuple'Fused Int s) b
forall s b. b -> Step s b
Done (b -> Step (Tuple'Fused Int s) b)
-> m b -> m (Step (Tuple'Fused Int s) b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
ffinal s
s
Done b
b -> Step (Tuple'Fused Int s) b -> m (Step (Tuple'Fused Int s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple'Fused Int s) b -> m (Step (Tuple'Fused Int s) b))
-> Step (Tuple'Fused Int s) b -> m (Step (Tuple'Fused Int s) b)
forall a b. (a -> b) -> a -> b
$ b -> Step (Tuple'Fused Int s) b
forall s b. b -> Step s b
Done b
b
initial :: m (Step (Tuple'Fused Int s) b)
initial = m (Step s b)
finitial m (Step s b)
-> (Step s b -> m (Step (Tuple'Fused Int s) b))
-> m (Step (Tuple'Fused Int s) b)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Step s b -> m (Step (Tuple'Fused Int s) b)
next (-Int
1)
step :: Tuple'Fused Int s -> a -> m (Step (Tuple'Fused Int s) b)
step (Tuple'Fused Int
i s
r) a
a = s -> a -> m (Step s b)
fstep s
r a
a m (Step s b)
-> (Step s b -> m (Step (Tuple'Fused Int s) b))
-> m (Step (Tuple'Fused Int s) b)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Step s b -> m (Step (Tuple'Fused Int s) b)
next Int
i
extract :: Tuple'Fused a s -> m b
extract (Tuple'Fused a
_ s
r) = s -> m b
fextract s
r
final :: Tuple'Fused a s -> m b
final (Tuple'Fused a
_ s
r) = s -> m b
ffinal s
r
{-# INLINE takeEndBy_ #-}
takeEndBy_ :: Monad m => (a -> Bool) -> Fold m a b -> Fold m a b
takeEndBy_ :: forall (m :: * -> *) a r.
Monad m =>
(a -> Bool) -> Fold m a r -> Fold m a r
takeEndBy_ a -> Bool
predicate (Fold s -> a -> m (Step s b)
fstep m (Step s b)
finitial s -> m b
fextract s -> m b
ffinal) =
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m 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) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step m (Step s b)
finitial s -> m b
fextract s -> m b
ffinal
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
ffinal s
s
{-# INLINE takeEndBy #-}
takeEndBy :: Monad m => (a -> Bool) -> Fold m a b -> Fold m a b
takeEndBy :: forall (m :: * -> *) a r.
Monad m =>
(a -> Bool) -> Fold m a r -> Fold m a r
takeEndBy a -> Bool
predicate (Fold s -> a -> m (Step s b)
fstep m (Step s b)
finitial s -> m b
fextract s -> m b
ffinal) =
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m 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) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step m (Step s b)
finitial s -> m b
fextract s -> m b
ffinal
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 a. a -> m a
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
ffinal s
s1
Done b
b -> Step s b -> m (Step s b)
forall a. a -> m a
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 duplicate #-}
duplicate :: Monad m => Fold m a b -> Fold m a (Fold m a b)
duplicate :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Fold m a (Fold m a b)
duplicate (Fold s -> a -> m (Step s b)
step1 m (Step s b)
initial1 s -> m b
extract1 s -> m b
final1) =
(s -> a -> m (Step s (Fold m a b)))
-> m (Step s (Fold m a b))
-> (s -> m (Fold m a b))
-> (s -> m (Fold m a b))
-> Fold m a (Fold m a b)
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 (Fold m a b))
forall {m :: * -> *} {a}.
Applicative m =>
s -> a -> m (Step s (Fold m a b))
step m (Step s (Fold m a b))
forall {a}. m (Step s (Fold m a b))
initial s -> m (Fold m a b)
forall {a}. a
extract s -> m (Fold m a b)
forall {f :: * -> *}. Applicative f => s -> f (Fold m a b)
final
where
initial :: m (Step s (Fold m a b))
initial = (b -> Fold m a b) -> Step s b -> Step s (Fold m a b)
forall b c a. (b -> c) -> Step a b -> Step a c
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second b -> Fold m a b
forall (m :: * -> *) b a. Applicative m => b -> Fold m a b
fromPure (Step s b -> Step s (Fold m a b))
-> m (Step s b) -> m (Step s (Fold m a b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (Step s b)
initial1
step :: s -> a -> m (Step s (Fold m a b))
step s
s a
a = (b -> Fold m a b) -> Step s b -> Step s (Fold m a b)
forall b c a. (b -> c) -> Step a b -> Step a c
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second b -> Fold m a b
forall (m :: * -> *) b a. Applicative m => b -> Fold m a b
fromPure (Step s b -> Step s (Fold m a b))
-> m (Step s b) -> m (Step s (Fold m a b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> a -> m (Step s b)
step1 s
s a
a
extract :: a
extract = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"duplicate: scanning may be problematic"
final :: s -> f (Fold m a b)
final s
s = Fold m a b -> f (Fold m a b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Fold m a b -> f (Fold m a b)) -> Fold m a b -> f (Fold m a b)
forall a b. (a -> b) -> a -> b
$ (s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m 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) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step1 (Step s b -> m (Step s b)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (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
extract1 s -> m b
final1
{-# INLINE reduce #-}
reduce :: Monad m => Fold m a b -> m (Fold m a b)
reduce :: forall (m :: * -> *) a b. Monad m => Fold m a b -> m (Fold m a b)
reduce (Fold s -> a -> m (Step s b)
step m (Step s b)
initial s -> m b
extract s -> m b
final) = do
Step s b
i <- m (Step s b)
initial
Fold m a b -> m (Fold m a b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Fold m a b -> m (Fold m a b)) -> Fold m a b -> m (Fold m a b)
forall a b. (a -> b) -> a -> b
$ (s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m 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) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step (Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step s b
i) s -> m b
extract s -> m b
final
{-# INLINE snoclM #-}
snoclM :: Monad m => Fold m a b -> m a -> Fold m a b
snoclM :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> m a -> Fold m a b
snoclM (Fold s -> a -> m (Step s b)
fstep m (Step s b)
finitial s -> m b
fextract s -> m b
ffinal) m a
action =
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m 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) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
fextract s -> m b
ffinal
where
initial :: m (Step s b)
initial = do
Step s b
res <- m (Step s b)
finitial
case Step s b
res of
Partial s
fs -> m a
action m a -> (a -> m (Step s b)) -> m (Step s b)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> a -> m (Step s b)
fstep s
fs
Done b
b -> Step s b -> m (Step s b)
forall a. a -> m a
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 snocl #-}
snocl :: Monad m => Fold m a b -> a -> Fold m a b
snocl :: forall (m :: * -> *) a b. Monad m => Fold m a b -> a -> Fold m a b
snocl (Fold s -> a -> m (Step s b)
fstep m (Step s b)
finitial s -> m b
fextract s -> m b
ffinal) a
a =
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m 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) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
fextract s -> m b
ffinal
where
initial :: m (Step s b)
initial = do
Step s b
res <- m (Step s b)
finitial
case Step s b
res of
Partial s
fs -> s -> a -> m (Step s b)
fstep s
fs a
a
Done b
b -> Step s b -> m (Step s b)
forall a. a -> m a
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 snocM #-}
snocM :: Monad m => Fold m a b -> m a -> m (Fold m a b)
snocM :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> m a -> m (Fold m a b)
snocM (Fold s -> a -> m (Step s b)
step m (Step s b)
initial s -> m b
extract s -> m b
final) m a
action = do
Step s b
res <- m (Step s b)
initial
Step s b
r <- case Step s b
res of
Partial s
fs -> m a
action m a -> (a -> m (Step s b)) -> m (Step s b)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> a -> m (Step s b)
step s
fs
Done b
_ -> Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step s b
res
Fold m a b -> m (Fold m a b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Fold m a b -> m (Fold m a b)) -> Fold m a b -> m (Fold m a b)
forall a b. (a -> b) -> a -> b
$ (s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m 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) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step (Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step s b
r) s -> m b
extract s -> m b
final
{-# INLINE snoc #-}
snoc :: Monad m => Fold m a b -> a -> m (Fold m a b)
snoc :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> a -> m (Fold m a b)
snoc (Fold s -> a -> m (Step s b)
step m (Step s b)
initial s -> m b
extract s -> m b
final) a
a = do
Step s b
res <- m (Step s b)
initial
Step s b
r <- case Step s b
res of
Partial s
fs -> s -> a -> m (Step s b)
step s
fs a
a
Done b
_ -> Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step s b
res
Fold m a b -> m (Fold m a b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Fold m a b -> m (Fold m a b)) -> Fold m a b -> m (Fold m a b)
forall a b. (a -> b) -> a -> b
$ (s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m 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) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
step (Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step s b
r) s -> m b
extract s -> m b
final
{-# INLINE addOne #-}
addOne :: Monad m => a -> Fold m a b -> m (Fold m a b)
addOne :: forall (m :: * -> *) a b.
Monad m =>
a -> Fold m a b -> m (Fold m a b)
addOne = (Fold m a b -> a -> m (Fold m a b))
-> a -> Fold m a b -> m (Fold m a b)
forall a b c. (a -> b -> c) -> b -> a -> c
flip Fold m a b -> a -> m (Fold m a b)
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> a -> m (Fold m a b)
snoc
{-# INLINE extractM #-}
extractM :: Monad m => Fold m a b -> m b
(Fold s -> a -> m (Step s b)
_ m (Step s b)
initial s -> m b
extract s -> m b
_) = do
Step s b
res <- m (Step s b)
initial
case Step s b
res of
Partial s
fs -> s -> m b
extract s
fs
Done b
b -> b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
{-# INLINE close #-}
close :: Monad m => Fold m a b -> Fold m a b
close :: forall (m :: * -> *) a b. Monad m => Fold m a b -> Fold m a b
close (Fold s -> a -> m (Step s b)
_ m (Step s b)
initial1 s -> m b
_ s -> m b
final1) =
(Any -> a -> m (Step Any b))
-> m (Step Any b) -> (Any -> m b) -> (Any -> m b) -> Fold m a b
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 Any -> a -> m (Step Any b)
forall a. HasCallStack => a
undefined m (Step Any b)
forall {s}. m (Step s b)
initial Any -> m b
forall a. HasCallStack => a
undefined Any -> m b
forall a. HasCallStack => a
undefined
where
initial :: m (Step s b)
initial = do
Step s b
res <- m (Step s b)
initial1
case Step s b
res of
Partial s
s -> 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
final1 s
s
Done b
b -> Step s b -> m (Step s b)
forall a. a -> m a
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 isClosed #-}
isClosed :: Monad m => Fold m a b -> m Bool
isClosed :: forall (m :: * -> *) a b. Monad m => Fold m a b -> m Bool
isClosed (Fold s -> a -> m (Step s b)
_ m (Step s b)
initial s -> m b
_ s -> m b
_) = do
Step s b
res <- m (Step s b)
initial
Bool -> m Bool
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ case Step s b
res of
Partial s
_ -> Bool
False
Done b
_ -> Bool
True
{-# ANN type ManyState Fuse #-}
data ManyState s1 s2
= ManyFirst !s1 !s2
| ManyLoop !s1 !s2
{-# INLINE many #-}
many :: Monad m => Fold m a b -> Fold m b c -> Fold m a c
many :: forall (m :: * -> *) a b c.
Monad m =>
Fold m a b -> Fold m b c -> Fold m a c
many
(Fold s -> a -> m (Step s b)
sstep m (Step s b)
sinitial s -> m b
sextract s -> m b
sfinal)
(Fold s -> b -> m (Step s c)
cstep m (Step s c)
cinitial s -> m c
cextract s -> m c
cfinal) =
(ManyState s s -> a -> m (Step (ManyState s s) c))
-> m (Step (ManyState s s) c)
-> (ManyState s s -> m c)
-> (ManyState s 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) -> (s -> m b) -> Fold m a b
Fold ManyState s s -> a -> m (Step (ManyState s s) c)
step m (Step (ManyState s s) c)
initial ManyState s s -> m c
extract ManyState s s -> m c
final
where
{-# INLINE split #-}
split :: (s -> s -> ManyState s s)
-> s -> Step s b -> m (Step (ManyState s s) c)
split s -> s -> ManyState s s
f s
cs Step s b
sres =
case Step s b
sres of
Partial s
ss -> Step (ManyState s s) c -> m (Step (ManyState s s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyState s s) c -> m (Step (ManyState s s) c))
-> Step (ManyState s s) c -> m (Step (ManyState s s) c)
forall a b. (a -> b) -> a -> b
$ ManyState s s -> Step (ManyState s s) c
forall s b. s -> Step s b
Partial (ManyState s s -> Step (ManyState s s) c)
-> ManyState s s -> Step (ManyState s s) c
forall a b. (a -> b) -> a -> b
$ s -> s -> ManyState s s
f s
ss s
cs
Done b
sb -> s -> b -> m (Step s c)
cstep s
cs b
sb m (Step s c)
-> (Step s c -> m (Step (ManyState s s) c))
-> m (Step (ManyState s s) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Step s c -> m (Step (ManyState s s) c)
collect
collect :: Step s c -> m (Step (ManyState s s) c)
collect Step s c
cres =
case Step s c
cres of
Partial s
cs -> m (Step s b)
sinitial m (Step s b)
-> (Step s b -> m (Step (ManyState s s) c))
-> m (Step (ManyState s s) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (s -> s -> ManyState s s)
-> s -> Step s b -> m (Step (ManyState s s) c)
split s -> s -> ManyState s s
forall s1 s2. s1 -> s2 -> ManyState s1 s2
ManyFirst s
cs
Done c
cb -> Step (ManyState s s) c -> m (Step (ManyState s s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyState s s) c -> m (Step (ManyState s s) c))
-> Step (ManyState s s) c -> m (Step (ManyState s s) c)
forall a b. (a -> b) -> a -> b
$ c -> Step (ManyState s s) c
forall s b. b -> Step s b
Done c
cb
initial :: m (Step (ManyState s s) c)
initial = m (Step s c)
cinitial m (Step s c)
-> (Step s c -> m (Step (ManyState s s) c))
-> m (Step (ManyState s s) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Step s c -> m (Step (ManyState s s) c)
collect
{-# INLINE step_ #-}
step_ :: s -> s -> a -> m (Step (ManyState s s) c)
step_ s
ss s
cs a
a = s -> a -> m (Step s b)
sstep s
ss a
a m (Step s b)
-> (Step s b -> m (Step (ManyState s s) c))
-> m (Step (ManyState s s) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (s -> s -> ManyState s s)
-> s -> Step s b -> m (Step (ManyState s s) c)
split s -> s -> ManyState s s
forall s1 s2. s1 -> s2 -> ManyState s1 s2
ManyLoop s
cs
{-# INLINE step #-}
step :: ManyState s s -> a -> m (Step (ManyState s s) c)
step (ManyFirst s
ss s
cs) a
a = s -> s -> a -> m (Step (ManyState s s) c)
step_ s
ss s
cs a
a
step (ManyLoop s
ss s
cs) a
a = s -> s -> a -> m (Step (ManyState s s) c)
step_ s
ss s
cs a
a
extract :: ManyState s s -> m c
extract (ManyFirst s
_ s
cs) = s -> m c
cextract s
cs
extract (ManyLoop s
ss s
cs) = do
Step s c
cres <- s -> m b
sextract s
ss m b -> (b -> m (Step s c)) -> m (Step s c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> b -> m (Step s c)
cstep s
cs
case Step s c
cres of
Partial s
s -> s -> m c
cextract s
s
Done c
b -> c -> m c
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return c
b
final :: ManyState s s -> m c
final (ManyFirst s
ss s
cs) = s -> m b
sfinal s
ss m b -> m c -> m c
forall a b. m a -> m b -> m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> s -> m c
cfinal s
cs
final (ManyLoop s
ss s
cs) = do
Step s c
cres <- s -> m b
sfinal s
ss m b -> (b -> m (Step s c)) -> m (Step s c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> b -> m (Step s c)
cstep s
cs
case Step s c
cres of
Partial s
s -> s -> m c
cfinal s
s
Done c
b -> c -> m c
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return c
b
{-# INLINE manyPost #-}
manyPost :: Monad m => Fold m a b -> Fold m b c -> Fold m a c
manyPost :: forall (m :: * -> *) a b c.
Monad m =>
Fold m a b -> Fold m b c -> Fold m a c
manyPost
(Fold s -> a -> m (Step s b)
sstep m (Step s b)
sinitial s -> m b
sextract s -> m b
sfinal)
(Fold s -> b -> m (Step s c)
cstep m (Step s c)
cinitial s -> m c
cextract s -> m c
cfinal) =
(Tuple' s s -> a -> m (Step (Tuple' s s) c))
-> m (Step (Tuple' s s) c)
-> (Tuple' s s -> m c)
-> (Tuple' s 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) -> (s -> m b) -> Fold m a b
Fold Tuple' s s -> a -> m (Step (Tuple' s s) c)
step m (Step (Tuple' s s) c)
initial Tuple' s s -> m c
extract Tuple' s s -> m c
final
where
{-# INLINE split #-}
split :: s -> Step s b -> m (Step (Tuple' s s) c)
split s
cs Step s b
sres =
case Step s b
sres of
Partial s
ss1 -> Step (Tuple' s s) c -> m (Step (Tuple' s s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple' s s) c -> m (Step (Tuple' s s) c))
-> Step (Tuple' s s) c -> m (Step (Tuple' s s) c)
forall a b. (a -> b) -> a -> b
$ Tuple' s s -> Step (Tuple' s s) c
forall s b. s -> Step s b
Partial (Tuple' s s -> Step (Tuple' s s) c)
-> Tuple' s s -> Step (Tuple' s s) c
forall a b. (a -> b) -> a -> b
$ s -> s -> Tuple' s s
forall a b. a -> b -> Tuple' a b
Tuple' s
ss1 s
cs
Done b
sb -> s -> b -> m (Step s c)
cstep s
cs b
sb m (Step s c)
-> (Step s c -> m (Step (Tuple' s s) c)) -> m (Step (Tuple' s s) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Step s c -> m (Step (Tuple' s s) c)
collect
collect :: Step s c -> m (Step (Tuple' s s) c)
collect Step s c
cres =
case Step s c
cres of
Partial s
cs -> m (Step s b)
sinitial m (Step s b)
-> (Step s b -> m (Step (Tuple' s s) c)) -> m (Step (Tuple' s s) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> Step s b -> m (Step (Tuple' s s) c)
split s
cs
Done c
cb -> Step (Tuple' s s) c -> m (Step (Tuple' s s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple' s s) c -> m (Step (Tuple' s s) c))
-> Step (Tuple' s s) c -> m (Step (Tuple' s s) c)
forall a b. (a -> b) -> a -> b
$ c -> Step (Tuple' s s) c
forall s b. b -> Step s b
Done c
cb
initial :: m (Step (Tuple' s s) c)
initial = m (Step s c)
cinitial m (Step s c)
-> (Step s c -> m (Step (Tuple' s s) c)) -> m (Step (Tuple' s s) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Step s c -> m (Step (Tuple' s s) c)
collect
{-# INLINE step #-}
step :: Tuple' s s -> a -> m (Step (Tuple' s s) c)
step (Tuple' s
ss s
cs) a
a = s -> a -> m (Step s b)
sstep s
ss a
a m (Step s b)
-> (Step s b -> m (Step (Tuple' s s) c)) -> m (Step (Tuple' s s) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> Step s b -> m (Step (Tuple' s s) c)
split s
cs
extract :: Tuple' s s -> m c
extract (Tuple' s
ss s
cs) = do
Step s c
cres <- s -> m b
sextract s
ss m b -> (b -> m (Step s c)) -> m (Step s c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> b -> m (Step s c)
cstep s
cs
case Step s c
cres of
Partial s
s -> s -> m c
cextract s
s
Done c
b -> c -> m c
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return c
b
final :: Tuple' s s -> m c
final (Tuple' s
ss s
cs) = do
Step s c
cres <- s -> m b
sfinal s
ss m b -> (b -> m (Step s c)) -> m (Step s c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> b -> m (Step s c)
cstep s
cs
case Step s c
cres of
Partial s
s -> s -> m c
cfinal s
s
Done c
b -> c -> m c
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return c
b
{-# INLINE groupsOf #-}
groupsOf :: Monad m => Int -> Fold m a b -> Fold m b c -> Fold m a c
groupsOf :: forall (m :: * -> *) a b c.
Monad m =>
Int -> Fold m a b -> Fold m b c -> Fold m a c
groupsOf Int
n Fold m a b
split = Fold m a b -> Fold m b c -> Fold m a c
forall (m :: * -> *) a b c.
Monad m =>
Fold m a b -> Fold m b c -> Fold m a c
many (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
split)
{-# INLINE refoldMany #-}
refoldMany :: Monad m => Fold m a b -> Refold m x b c -> Refold m x a c
refoldMany :: forall (m :: * -> *) a b x c.
Monad m =>
Fold m a b -> Refold m x b c -> Refold m x a c
refoldMany
(Fold s -> a -> m (Step s b)
sstep m (Step s b)
sinitial s -> m b
sextract s -> m b
_sfinal)
(Refold s -> b -> m (Step s c)
cstep x -> m (Step s c)
cinject s -> m c
cextract) =
(Tuple' s (Either s s) -> a -> m (Step (Tuple' s (Either s s)) c))
-> (x -> m (Step (Tuple' s (Either s s)) c))
-> (Tuple' s (Either s s) -> m c)
-> Refold m x a c
forall (m :: * -> *) c a b s.
(s -> a -> m (Step s b))
-> (c -> m (Step s b)) -> (s -> m b) -> Refold m c a b
Refold Tuple' s (Either s s) -> a -> m (Step (Tuple' s (Either s s)) c)
step x -> m (Step (Tuple' s (Either s s)) c)
forall {b}. x -> m (Step (Tuple' s (Either s b)) c)
inject Tuple' s (Either s s) -> m c
forall {a}. Tuple' s (Either a s) -> m c
extract
where
{-# INLINE split #-}
split :: s
-> (s -> Either s b)
-> Step s b
-> m (Step (Tuple' s (Either s b)) c)
split s
cs s -> Either s b
f Step s b
sres =
case Step s b
sres of
Partial s
ss -> Step (Tuple' s (Either s b)) c
-> m (Step (Tuple' s (Either s b)) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple' s (Either s b)) c
-> m (Step (Tuple' s (Either s b)) c))
-> Step (Tuple' s (Either s b)) c
-> m (Step (Tuple' s (Either s b)) c)
forall a b. (a -> b) -> a -> b
$ Tuple' s (Either s b) -> Step (Tuple' s (Either s b)) c
forall s b. s -> Step s b
Partial (Tuple' s (Either s b) -> Step (Tuple' s (Either s b)) c)
-> Tuple' s (Either s b) -> Step (Tuple' s (Either s b)) c
forall a b. (a -> b) -> a -> b
$ s -> Either s b -> Tuple' s (Either s b)
forall a b. a -> b -> Tuple' a b
Tuple' s
cs (s -> Either s b
f s
ss)
Done b
sb -> s -> b -> m (Step s c)
cstep s
cs b
sb m (Step s c)
-> (Step s c -> m (Step (Tuple' s (Either s b)) c))
-> m (Step (Tuple' s (Either s b)) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Step s c -> m (Step (Tuple' s (Either s b)) c)
collect
collect :: Step s c -> m (Step (Tuple' s (Either s b)) c)
collect Step s c
cres =
case Step s c
cres of
Partial s
cs -> m (Step s b)
sinitial m (Step s b)
-> (Step s b -> m (Step (Tuple' s (Either s b)) c))
-> m (Step (Tuple' s (Either s b)) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s
-> (s -> Either s b)
-> Step s b
-> m (Step (Tuple' s (Either s b)) c)
split s
cs s -> Either s b
forall a b. a -> Either a b
Left
Done c
cb -> Step (Tuple' s (Either s b)) c
-> m (Step (Tuple' s (Either s b)) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple' s (Either s b)) c
-> m (Step (Tuple' s (Either s b)) c))
-> Step (Tuple' s (Either s b)) c
-> m (Step (Tuple' s (Either s b)) c)
forall a b. (a -> b) -> a -> b
$ c -> Step (Tuple' s (Either s b)) c
forall s b. b -> Step s b
Done c
cb
inject :: x -> m (Step (Tuple' s (Either s b)) c)
inject x
x = x -> m (Step s c)
cinject x
x m (Step s c)
-> (Step s c -> m (Step (Tuple' s (Either s b)) c))
-> m (Step (Tuple' s (Either s b)) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Step s c -> m (Step (Tuple' s (Either s b)) c)
forall {b}. Step s c -> m (Step (Tuple' s (Either s b)) c)
collect
{-# INLINE step_ #-}
step_ :: s -> s -> a -> m (Step (Tuple' s (Either s s)) c)
step_ s
ss s
cs a
a = s -> a -> m (Step s b)
sstep s
ss a
a m (Step s b)
-> (Step s b -> m (Step (Tuple' s (Either s s)) c))
-> m (Step (Tuple' s (Either s s)) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s
-> (s -> Either s s)
-> Step s b
-> m (Step (Tuple' s (Either s s)) c)
forall {b}.
s
-> (s -> Either s b)
-> Step s b
-> m (Step (Tuple' s (Either s b)) c)
split s
cs s -> Either s s
forall a b. b -> Either a b
Right
{-# INLINE step #-}
step :: Tuple' s (Either s s) -> a -> m (Step (Tuple' s (Either s s)) c)
step (Tuple' s
cs (Left s
ss)) a
a = s -> s -> a -> m (Step (Tuple' s (Either s s)) c)
step_ s
ss s
cs a
a
step (Tuple' s
cs (Right s
ss)) a
a = s -> s -> a -> m (Step (Tuple' s (Either s s)) c)
step_ s
ss s
cs a
a
extract :: Tuple' s (Either a s) -> m c
extract (Tuple' s
cs (Left a
_)) = s -> m c
cextract s
cs
extract (Tuple' s
cs (Right s
ss )) = do
Step s c
cres <- s -> m b
sextract s
ss m b -> (b -> m (Step s c)) -> m (Step s c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> b -> m (Step s c)
cstep s
cs
case Step s c
cres of
Partial s
s -> s -> m c
cextract s
s
Done c
b -> c -> m c
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return c
b
{-# ANN type ConsumeManyState Fuse #-}
data ConsumeManyState x cs ss = ConsumeMany x cs (Either ss ss)
{-# INLINE refoldMany1 #-}
refoldMany1 :: Monad m => Refold m x a b -> Fold m b c -> Refold m x a c
refoldMany1 :: forall (m :: * -> *) x a b c.
Monad m =>
Refold m x a b -> Fold m b c -> Refold m x a c
refoldMany1
(Refold s -> a -> m (Step s b)
sstep x -> m (Step s b)
sinject s -> m b
sextract)
(Fold s -> b -> m (Step s c)
cstep m (Step s c)
cinitial s -> m c
cextract s -> m c
_cfinal) =
(ConsumeManyState x s s
-> a -> m (Step (ConsumeManyState x s s) c))
-> (x -> m (Step (ConsumeManyState x s s) c))
-> (ConsumeManyState x s s -> m c)
-> Refold m x a c
forall (m :: * -> *) c a b s.
(s -> a -> m (Step s b))
-> (c -> m (Step s b)) -> (s -> m b) -> Refold m c a b
Refold ConsumeManyState x s s -> a -> m (Step (ConsumeManyState x s s) c)
step x -> m (Step (ConsumeManyState x s s) c)
inject ConsumeManyState x s s -> m c
forall {x}. ConsumeManyState x s s -> m c
extract
where
{-# INLINE split #-}
split :: x
-> s
-> (s -> Either s s)
-> Step s b
-> m (Step (ConsumeManyState x s s) c)
split x
x s
cs s -> Either s s
f Step s b
sres =
case Step s b
sres of
Partial s
ss -> Step (ConsumeManyState x s s) c
-> m (Step (ConsumeManyState x s s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ConsumeManyState x s s) c
-> m (Step (ConsumeManyState x s s) c))
-> Step (ConsumeManyState x s s) c
-> m (Step (ConsumeManyState x s s) c)
forall a b. (a -> b) -> a -> b
$ ConsumeManyState x s s -> Step (ConsumeManyState x s s) c
forall s b. s -> Step s b
Partial (ConsumeManyState x s s -> Step (ConsumeManyState x s s) c)
-> ConsumeManyState x s s -> Step (ConsumeManyState x s s) c
forall a b. (a -> b) -> a -> b
$ x -> s -> Either s s -> ConsumeManyState x s s
forall x cs ss. x -> cs -> Either ss ss -> ConsumeManyState x cs ss
ConsumeMany x
x s
cs (s -> Either s s
f s
ss)
Done b
sb -> s -> b -> m (Step s c)
cstep s
cs b
sb m (Step s c)
-> (Step s c -> m (Step (ConsumeManyState x s s) c))
-> m (Step (ConsumeManyState x s s) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= x -> Step s c -> m (Step (ConsumeManyState x s s) c)
collect x
x
collect :: x -> Step s c -> m (Step (ConsumeManyState x s s) c)
collect x
x Step s c
cres =
case Step s c
cres of
Partial s
cs -> x -> m (Step s b)
sinject x
x m (Step s b)
-> (Step s b -> m (Step (ConsumeManyState x s s) c))
-> m (Step (ConsumeManyState x s s) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= x
-> s
-> (s -> Either s s)
-> Step s b
-> m (Step (ConsumeManyState x s s) c)
split x
x s
cs s -> Either s s
forall a b. a -> Either a b
Left
Done c
cb -> Step (ConsumeManyState x s s) c
-> m (Step (ConsumeManyState x s s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ConsumeManyState x s s) c
-> m (Step (ConsumeManyState x s s) c))
-> Step (ConsumeManyState x s s) c
-> m (Step (ConsumeManyState x s s) c)
forall a b. (a -> b) -> a -> b
$ c -> Step (ConsumeManyState x s s) c
forall s b. b -> Step s b
Done c
cb
inject :: x -> m (Step (ConsumeManyState x s s) c)
inject x
x = m (Step s c)
cinitial m (Step s c)
-> (Step s c -> m (Step (ConsumeManyState x s s) c))
-> m (Step (ConsumeManyState x s s) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= x -> Step s c -> m (Step (ConsumeManyState x s s) c)
collect x
x
{-# INLINE step_ #-}
step_ :: x -> s -> s -> a -> m (Step (ConsumeManyState x s s) c)
step_ x
x s
ss s
cs a
a = s -> a -> m (Step s b)
sstep s
ss a
a m (Step s b)
-> (Step s b -> m (Step (ConsumeManyState x s s) c))
-> m (Step (ConsumeManyState x s s) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= x
-> s
-> (s -> Either s s)
-> Step s b
-> m (Step (ConsumeManyState x s s) c)
split x
x s
cs s -> Either s s
forall a b. b -> Either a b
Right
{-# INLINE step #-}
step :: ConsumeManyState x s s -> a -> m (Step (ConsumeManyState x s s) c)
step (ConsumeMany x
x s
cs (Left s
ss)) a
a = x -> s -> s -> a -> m (Step (ConsumeManyState x s s) c)
step_ x
x s
ss s
cs a
a
step (ConsumeMany x
x s
cs (Right s
ss)) a
a = x -> s -> s -> a -> m (Step (ConsumeManyState x s s) c)
step_ x
x s
ss s
cs a
a
extract :: ConsumeManyState x s s -> m c
extract (ConsumeMany x
_ s
cs (Left s
_)) = s -> m c
cextract s
cs
extract (ConsumeMany x
_ s
cs (Right s
ss )) = do
Step s c
cres <- s -> m b
sextract s
ss m b -> (b -> m (Step s c)) -> m (Step s c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> b -> m (Step s c)
cstep s
cs
case Step s c
cres of
Partial s
s -> s -> m c
cextract s
s
Done c
b -> c -> m c
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return c
b
{-# INLINE refold #-}
refold :: Monad m => Refold m b a c -> Fold m a b -> Fold m a c
refold :: forall (m :: * -> *) b a c.
Monad m =>
Refold m b a c -> Fold m a b -> Fold m a c
refold (Refold s -> a -> m (Step s c)
step b -> m (Step s c)
inject s -> m c
extract) Fold m a b
f =
(s -> a -> m (Step s c))
-> m (Step s c) -> (s -> m c) -> (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) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s c)
step (Fold m a b -> m b
forall {m :: * -> *} {a} {b}. Monad m => Fold m a b -> m b
extractM Fold m a b
f m b -> (b -> m (Step s c)) -> m (Step s c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> m (Step s c)
inject) s -> m c
extract s -> m c
extract
morphInner :: (forall x. m x -> n x) -> Fold m a b -> Fold n a b
morphInner :: forall (m :: * -> *) (n :: * -> *) a b.
(forall x. m x -> n x) -> Fold m a b -> Fold n a b
morphInner 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 -> m b
final) =
(s -> a -> n (Step s b))
-> n (Step s b) -> (s -> n 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) -> (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) (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
final)
generalizeInner :: Monad m => Fold Identity a b -> Fold m a b
generalizeInner :: forall (m :: * -> *) a b.
Monad m =>
Fold Identity a b -> Fold m a b
generalizeInner = (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
morphInner (x -> m x
forall a. a -> m a
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)