{-# LANGUAGE CPP #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
#ifdef TRUSTWORTHY
{-# LANGUAGE Trustworthy #-}
#endif
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Control.Lens.Fold
(
Fold
, IndexedFold
, (^..)
, (^?)
, (^?!)
, pre, ipre
, preview, previews, ipreview, ipreviews
, preuse, preuses, ipreuse, ipreuses
, has, hasn't
, folding, ifolding
, folded
, folded64
, unfolded
, iterated
, filtered
, backwards
, repeated
, replicated
, cycled
, takingWhile
, droppingWhile
, worded, lined
, foldMapOf, foldOf
, foldrOf, foldlOf
, toListOf
, anyOf, allOf, noneOf
, andOf, orOf
, productOf, sumOf
, traverseOf_, forOf_, sequenceAOf_
, mapMOf_, forMOf_, sequenceOf_
, asumOf, msumOf
, concatMapOf, concatOf
, elemOf, notElemOf
, lengthOf
, nullOf, notNullOf
, firstOf, lastOf
, maximumOf, minimumOf
, maximumByOf, minimumByOf
, findOf
, findMOf
, foldrOf', foldlOf'
, foldr1Of, foldl1Of
, foldr1Of', foldl1Of'
, foldrMOf, foldlMOf
, (^@..)
, (^@?)
, (^@?!)
, ifoldMapOf
, ifoldrOf
, ifoldlOf
, ianyOf
, iallOf
, inoneOf
, itraverseOf_
, iforOf_
, imapMOf_
, iforMOf_
, iconcatMapOf
, ifindOf
, ifindMOf
, ifoldrOf'
, ifoldlOf'
, ifoldrMOf
, ifoldlMOf
, itoListOf
, ifiltered
, itakingWhile
, idroppingWhile
, headOf
, Leftmost
, Rightmost
, Traversed
, Sequenced
, foldBy
, foldByOf
, foldMapBy
, foldMapByOf
) where
import Control.Applicative as Applicative
import Control.Applicative.Backwards
import Control.Comonad
import Control.Lens.Getter
import Control.Lens.Internal.Fold
import Control.Lens.Internal.Getter
import Control.Lens.Internal.Indexed
import Control.Lens.Internal.Magma
import Control.Lens.Type
import Control.Monad as Monad
import Control.Monad.Reader
import Control.Monad.State
import Data.Foldable as Foldable
import Data.Functor.Apply
import Data.Functor.Compose
import Data.Int (Int64)
import Data.List (intercalate)
import Data.Maybe
import Data.Monoid
import Data.Profunctor
import Data.Profunctor.Rep
import Data.Profunctor.Unsafe
import Data.Traversable
#ifdef HLINT
{-# ANN module "HLint: ignore Eta reduce" #-}
{-# ANN module "HLint: ignore Use camelCase" #-}
{-# ANN module "HLint: ignore Use curry" #-}
#endif
infixl 8 ^.., ^?, ^?!, ^@.., ^@?, ^@?!
folding :: (Foldable f, Contravariant g, Applicative g) => (s -> f a) -> LensLike g s t a b
folding sfa agb = coerce . traverse_ agb . sfa
{-# INLINE folding #-}
ifolding :: (Foldable f, Indexable i p, Contravariant g, Applicative g) => (s -> f (i, a)) -> Over p g s t a b
ifolding sfa f = coerce . traverse_ (coerce . uncurry (indexed f)) . sfa
{-# INLINE ifolding #-}
folded :: Foldable f => IndexedFold Int (f a) a
folded = conjoined folded' (indexing folded')
{-# INLINE folded #-}
folded64 :: Foldable f => IndexedFold Int64 (f a) a
folded64 = conjoined folded' (indexing64 folded')
{-# INLINE folded64 #-}
folded' :: Foldable f => Fold (f a) a
folded' f = coerce . getFolding . foldMap (Folding #. f)
{-# INLINE folded' #-}
repeated :: Fold1 a a
repeated f a = as where as = f a .> as
{-# INLINE repeated #-}
replicated :: Int -> Fold a a
replicated n0 f a = go n0 where
m = f a
go 0 = noEffect
go n = m *> go (n - 1)
{-# INLINE replicated #-}
cycled :: (Contravariant f, Apply f) => LensLike f s t a b -> LensLike f s t a b
cycled l f a = as where as = l f a .> as
{-# INLINE cycled #-}
unfolded :: (b -> Maybe (a, b)) -> Fold b a
unfolded f g b0 = go b0 where
go b = case f b of
Just (a, b') -> g a *> go b'
Nothing -> noEffect
{-# INLINE unfolded #-}
iterated :: (a -> a) -> Fold1 a a
iterated f g a0 = go a0 where
go a = g a .> go (f a)
{-# INLINE iterated #-}
filtered :: (Choice p, Applicative f) => (a -> Bool) -> Optic' p f a a
filtered p = dimap (\x -> if p x then Right x else Left x) (either pure id) . right'
{-# INLINE filtered #-}
takingWhile :: (Conjoined p, Applicative f) => (a -> Bool) -> Over p (TakingWhile p f a a) s t a a -> Over p f s t a a
takingWhile p l pafb = fmap runMagma . traverse (corep pafb) . runTakingWhile . l flag where
flag = cotabulate $ \wa -> let a = extract wa; r = p a in TakingWhile r a $ \pr ->
if pr && r then Magma () wa else MagmaPure a
{-# INLINE takingWhile #-}
droppingWhile :: (Conjoined p, Profunctor q, Applicative f)
=> (a -> Bool)
-> Optical p q (Compose (State Bool) f) s t a a
-> Optical p q f s t a a
droppingWhile p l f = (flip evalState True .# getCompose) `rmap` l g where
g = cotabulate $ \wa -> Compose $ state $ \b -> let
a = extract wa
b' = b && p a
in (if b' then pure a else corep f wa, b')
{-# INLINE droppingWhile #-}
worded :: Applicative f => IndexedLensLike' Int f String String
worded f = fmap unwords . conjoined traverse (indexing traverse) f . words
{-# INLINE worded #-}
lined :: Applicative f => IndexedLensLike' Int f String String
lined f = fmap (intercalate "\n") . conjoined traverse (indexing traverse) f . lines
{-# INLINE lined #-}
foldMapOf :: Profunctor p => Accessing p r s a -> p a r -> s -> r
foldMapOf l f = getConst #. l (Const #. f)
{-# INLINE foldMapOf #-}
foldOf :: Getting a s a -> s -> a
foldOf l = getConst #. l Const
{-# INLINE foldOf #-}
foldrOf :: Profunctor p => Accessing p (Endo r) s a -> p a (r -> r) -> r -> s -> r
foldrOf l f z = flip appEndo z `rmap` foldMapOf l (Endo #. f)
{-# INLINE foldrOf #-}
foldlOf :: Getting (Dual (Endo r)) s a -> (r -> a -> r) -> r -> s -> r
foldlOf l f z = (flip appEndo z .# getDual) `rmap` foldMapOf l (Dual #. Endo #. flip f)
{-# INLINE foldlOf #-}
toListOf :: Getting (Endo [a]) s a -> s -> [a]
toListOf l = foldrOf l (:) []
{-# INLINE toListOf #-}
(^..) :: s -> Getting (Endo [a]) s a -> [a]
s ^.. l = toListOf l s
{-# INLINE (^..) #-}
andOf :: Getting All s Bool -> s -> Bool
andOf l = getAll #. foldMapOf l All
{-# INLINE andOf #-}
orOf :: Getting Any s Bool -> s -> Bool
orOf l = getAny #. foldMapOf l Any
{-# INLINE orOf #-}
anyOf :: Profunctor p => Accessing p Any s a -> p a Bool -> s -> Bool
anyOf l f = getAny #. foldMapOf l (Any #. f)
{-# INLINE anyOf #-}
allOf :: Profunctor p => Accessing p All s a -> p a Bool -> s -> Bool
allOf l f = getAll #. foldMapOf l (All #. f)
{-# INLINE allOf #-}
noneOf :: Profunctor p => Accessing p Any s a -> p a Bool -> s -> Bool
noneOf l f = not . anyOf l f
{-# INLINE noneOf #-}
productOf :: Num a => Getting (Endo (Endo a)) s a -> s -> a
productOf l = foldlOf' l (*) 1
{-# INLINE productOf #-}
sumOf :: Num a => Getting (Endo (Endo a)) s a -> s -> a
sumOf l = foldlOf' l (+) 0
{-# INLINE sumOf #-}
traverseOf_ :: (Profunctor p, Functor f) => Accessing p (Traversed r f) s a -> p a (f r) -> s -> f ()
traverseOf_ l f = void . getTraversed #. foldMapOf l (Traversed #. f)
{-# INLINE traverseOf_ #-}
forOf_ :: (Profunctor p, Functor f) => Accessing p (Traversed r f) s a -> s -> p a (f r) -> f ()
forOf_ = flip . traverseOf_
{-# INLINE forOf_ #-}
sequenceAOf_ :: Functor f => Getting (Traversed a f) s (f a) -> s -> f ()
sequenceAOf_ l = void . getTraversed #. foldMapOf l Traversed
{-# INLINE sequenceAOf_ #-}
mapMOf_ :: (Profunctor p, Monad m) => Accessing p (Sequenced r m) s a -> p a (m r) -> s -> m ()
mapMOf_ l f = liftM skip . getSequenced #. foldMapOf l (Sequenced #. f)
{-# INLINE mapMOf_ #-}
forMOf_ :: (Profunctor p, Monad m) => Accessing p (Sequenced r m) s a -> s -> p a (m r) -> m ()
forMOf_ = flip . mapMOf_
{-# INLINE forMOf_ #-}
sequenceOf_ :: Monad m => Getting (Sequenced a m) s (m a) -> s -> m ()
sequenceOf_ l = liftM skip . getSequenced #. foldMapOf l Sequenced
{-# INLINE sequenceOf_ #-}
asumOf :: Alternative f => Getting (Endo (f a)) s (f a) -> s -> f a
asumOf l = foldrOf l (<|>) Applicative.empty
{-# INLINE asumOf #-}
msumOf :: MonadPlus m => Getting (Endo (m a)) s (m a) -> s -> m a
msumOf l = foldrOf l mplus mzero
{-# INLINE msumOf #-}
elemOf :: Eq a => Getting Any s a -> a -> s -> Bool
elemOf l = anyOf l . (==)
{-# INLINE elemOf #-}
notElemOf :: Eq a => Getting All s a -> a -> s -> Bool
notElemOf l = allOf l . (/=)
{-# INLINE notElemOf #-}
concatMapOf :: Profunctor p => Accessing p [r] s a -> p a [r] -> s -> [r]
concatMapOf l ces = getConst #. l (Const #. ces)
{-# INLINE concatMapOf #-}
concatOf :: Getting [r] s [r] -> s -> [r]
concatOf l = getConst #. l Const
{-# INLINE concatOf #-}
lengthOf :: Getting (Endo (Endo Int)) s a -> s -> Int
lengthOf l = foldlOf' l (\a _ -> a + 1) 0
{-# INLINE lengthOf #-}
(^?) :: s -> Getting (First a) s a -> Maybe a
s ^? l = getFirst (foldMapOf l (First #. Just) s)
{-# INLINE (^?) #-}
(^?!) :: s -> Getting (Endo a) s a -> a
s ^?! l = foldrOf l const (error "(^?!): empty Fold") s
{-# INLINE (^?!) #-}
firstOf :: Getting (Leftmost a) s a -> s -> Maybe a
firstOf l = getLeftmost . foldMapOf l LLeaf
{-# INLINE firstOf #-}
lastOf :: Getting (Rightmost a) s a -> s -> Maybe a
lastOf l = getRightmost . foldMapOf l RLeaf
{-# INLINE lastOf #-}
nullOf :: Getting All s a -> s -> Bool
nullOf = hasn't
{-# INLINE nullOf #-}
notNullOf :: Getting Any s a -> s -> Bool
notNullOf = has
{-# INLINE notNullOf #-}
maximumOf :: Ord a => Getting (Endo (Endo (Maybe a))) s a -> s -> Maybe a
maximumOf l = foldlOf' l mf Nothing where
mf Nothing y = Just $! y
mf (Just x) y = Just $! max x y
{-# INLINE maximumOf #-}
minimumOf :: Ord a => Getting (Endo (Endo (Maybe a))) s a -> s -> Maybe a
minimumOf l = foldlOf' l mf Nothing where
mf Nothing y = Just $! y
mf (Just x) y = Just $! min x y
{-# INLINE minimumOf #-}
maximumByOf :: Getting (Endo (Endo (Maybe a))) s a -> (a -> a -> Ordering) -> s -> Maybe a
maximumByOf l cmp = foldlOf' l mf Nothing where
mf Nothing y = Just $! y
mf (Just x) y = Just $! if cmp x y == GT then x else y
{-# INLINE maximumByOf #-}
minimumByOf :: Getting (Endo (Endo (Maybe a))) s a -> (a -> a -> Ordering) -> s -> Maybe a
minimumByOf l cmp = foldlOf' l mf Nothing where
mf Nothing y = Just $! y
mf (Just x) y = Just $! if cmp x y == GT then y else x
{-# INLINE minimumByOf #-}
findOf :: Conjoined p => Accessing p (Endo (Maybe a)) s a -> p a Bool -> s -> Maybe a
findOf l p = foldrOf l (cotabulate $ \wa y -> if corep p wa then Just (extract wa) else y) Nothing
{-# INLINE findOf #-}
findMOf :: (Monad m, Conjoined p) => Accessing p (Endo (m (Maybe a))) s a -> p a (m Bool) -> s -> m (Maybe a)
findMOf l p = foldrOf l (cotabulate $ \wa y -> corep p wa >>= \r -> if r then return (Just (extract wa)) else y) $ return Nothing
{-# INLINE findMOf #-}
foldr1Of :: Getting (Endo (Maybe a)) s a -> (a -> a -> a) -> s -> a
foldr1Of l f xs = fromMaybe (error "foldr1Of: empty structure")
(foldrOf l mf Nothing xs) where
mf x my = Just $ case my of
Nothing -> x
Just y -> f x y
{-# INLINE foldr1Of #-}
foldl1Of :: Getting (Dual (Endo (Maybe a))) s a -> (a -> a -> a) -> s -> a
foldl1Of l f xs = fromMaybe (error "foldl1Of: empty structure") (foldlOf l mf Nothing xs) where
mf mx y = Just $ case mx of
Nothing -> y
Just x -> f x y
{-# INLINE foldl1Of #-}
foldrOf' :: Getting (Dual (Endo (Endo r))) s a -> (a -> r -> r) -> r -> s -> r
foldrOf' l f z0 xs = foldlOf l f' (Endo id) xs `appEndo` z0
where f' (Endo k) x = Endo $ \ z -> k $! f x z
{-# INLINE foldrOf' #-}
foldlOf' :: Getting (Endo (Endo r)) s a -> (r -> a -> r) -> r -> s -> r
foldlOf' l f z0 xs = foldrOf l f' (Endo id) xs `appEndo` z0
where f' x (Endo k) = Endo $ \z -> k $! f z x
{-# INLINE foldlOf' #-}
foldr1Of' :: Getting (Dual (Endo (Endo (Maybe a)))) s a -> (a -> a -> a) -> s -> a
foldr1Of' l f xs = fromMaybe (error "foldr1Of': empty structure") (foldrOf' l mf Nothing xs) where
mf x Nothing = Just $! x
mf x (Just y) = Just $! f x y
{-# INLINE foldr1Of' #-}
foldl1Of' :: Getting (Endo (Endo (Maybe a))) s a -> (a -> a -> a) -> s -> a
foldl1Of' l f xs = fromMaybe (error "foldl1Of': empty structure") (foldlOf' l mf Nothing xs) where
mf Nothing y = Just $! y
mf (Just x) y = Just $! f x y
{-# INLINE foldl1Of' #-}
foldrMOf :: Monad m
=> Getting (Dual (Endo (r -> m r))) s a
-> (a -> r -> m r) -> r -> s -> m r
foldrMOf l f z0 xs = foldlOf l f' return xs z0
where f' k x z = f x z >>= k
{-# INLINE foldrMOf #-}
foldlMOf :: Monad m
=> Getting (Endo (r -> m r)) s a
-> (r -> a -> m r) -> r -> s -> m r
foldlMOf l f z0 xs = foldrOf l f' return xs z0
where f' x k z = f z x >>= k
{-# INLINE foldlMOf #-}
has :: Getting Any s a -> s -> Bool
has l = getAny #. foldMapOf l (\_ -> Any True)
{-# INLINE has #-}
hasn't :: Getting All s a -> s -> Bool
hasn't l = getAll #. foldMapOf l (\_ -> All False)
{-# INLINE hasn't #-}
pre :: Getting (First a) s a -> IndexPreservingGetter s (Maybe a)
pre l = dimap (getFirst . getConst #. l (Const #. First #. Just)) coerce
{-# INLINE pre #-}
ipre :: IndexedGetting i (First (i, a)) s a -> IndexPreservingGetter s (Maybe (i, a))
ipre l = dimap (getFirst . getConst #. l (Indexed $ \i a -> Const (First (Just (i, a))))) coerce
{-# INLINE ipre #-}
preview :: MonadReader s m => Getting (First a) s a -> m (Maybe a)
preview l = asks (getFirst #. foldMapOf l (First #. Just))
{-# INLINE preview #-}
ipreview :: MonadReader s m => IndexedGetting i (First (i, a)) s a -> m (Maybe (i, a))
ipreview l = asks (getFirst #. ifoldMapOf l (\i a -> First (Just (i, a))))
{-# INLINE ipreview #-}
previews :: MonadReader s m => Getting (First r) s a -> (a -> r) -> m (Maybe r)
previews l f = asks (getFirst . foldMapOf l (First #. Just . f))
{-# INLINE previews #-}
ipreviews :: MonadReader s m => IndexedGetting i (First r) s a -> (i -> a -> r) -> m (Maybe r)
ipreviews l f = asks (getFirst . ifoldMapOf l (\i -> First #. Just . f i))
{-# INLINE ipreviews #-}
preuse :: MonadState s m => Getting (First a) s a -> m (Maybe a)
preuse l = gets (preview l)
{-# INLINE preuse #-}
ipreuse :: MonadState s m => IndexedGetting i (First (i, a)) s a -> m (Maybe (i, a))
ipreuse l = gets (ipreview l)
{-# INLINE ipreuse #-}
preuses :: MonadState s m => Getting (First r) s a -> (a -> r) -> m (Maybe r)
preuses l f = gets (previews l f)
{-# INLINE preuses #-}
ipreuses :: MonadState s m => IndexedGetting i (First r) s a -> (i -> a -> r) -> m (Maybe r)
ipreuses l f = gets (ipreviews l f)
{-# INLINE ipreuses #-}
backwards :: (Profunctor p, Profunctor q) => Optical p q (Backwards f) s t a b -> Optical p q f s t a b
backwards l f = forwards #. l (Backwards #. f)
{-# INLINE backwards #-}
ifoldMapOf :: IndexedGetting i m s a -> (i -> a -> m) -> s -> m
ifoldMapOf l = foldMapOf l .# Indexed
{-# INLINE ifoldMapOf #-}
ifoldrOf :: IndexedGetting i (Endo r) s a -> (i -> a -> r -> r) -> r -> s -> r
ifoldrOf l = foldrOf l .# Indexed
{-# INLINE ifoldrOf #-}
ifoldlOf :: IndexedGetting i (Dual (Endo r)) s a -> (i -> r -> a -> r) -> r -> s -> r
ifoldlOf l f z = (flip appEndo z .# getDual) `rmap` ifoldMapOf l (\i -> Dual #. Endo #. flip (f i))
{-# INLINE ifoldlOf #-}
ianyOf :: IndexedGetting i Any s a -> (i -> a -> Bool) -> s -> Bool
ianyOf l = anyOf l .# Indexed
{-# INLINE ianyOf #-}
iallOf :: IndexedGetting i All s a -> (i -> a -> Bool) -> s -> Bool
iallOf l = allOf l .# Indexed
{-# INLINE iallOf #-}
inoneOf :: IndexedGetting i Any s a -> (i -> a -> Bool) -> s -> Bool
inoneOf l = noneOf l .# Indexed
{-# INLINE inoneOf #-}
itraverseOf_ :: Functor f => IndexedGetting i (Traversed r f) s a -> (i -> a -> f r) -> s -> f ()
itraverseOf_ l = traverseOf_ l .# Indexed
{-# INLINE itraverseOf_ #-}
iforOf_ :: Functor f => IndexedGetting i (Traversed r f) s a -> s -> (i -> a -> f r) -> f ()
iforOf_ = flip . itraverseOf_
{-# INLINE iforOf_ #-}
imapMOf_ :: Monad m => IndexedGetting i (Sequenced r m) s a -> (i -> a -> m r) -> s -> m ()
imapMOf_ l = mapMOf_ l .# Indexed
{-# INLINE imapMOf_ #-}
iforMOf_ :: Monad m => IndexedGetting i (Sequenced r m) s a -> s -> (i -> a -> m r) -> m ()
iforMOf_ = flip . imapMOf_
{-# INLINE iforMOf_ #-}
iconcatMapOf :: IndexedGetting i [r] s a -> (i -> a -> [r]) -> s -> [r]
iconcatMapOf = ifoldMapOf
{-# INLINE iconcatMapOf #-}
ifindOf :: IndexedGetting i (Endo (Maybe a)) s a -> (i -> a -> Bool) -> s -> Maybe a
ifindOf l = findOf l .# Indexed
{-# INLINE ifindOf #-}
ifindMOf :: Monad m => IndexedGetting i (Endo (m (Maybe a))) s a -> (i -> a -> m Bool) -> s -> m (Maybe a)
ifindMOf l = findMOf l .# Indexed
{-# INLINE ifindMOf #-}
ifoldrOf' :: IndexedGetting i (Dual (Endo (r -> r))) s a -> (i -> a -> r -> r) -> r -> s -> r
ifoldrOf' l f z0 xs = ifoldlOf l f' id xs z0
where f' i k x z = k $! f i x z
{-# INLINE ifoldrOf' #-}
ifoldlOf' :: IndexedGetting i (Endo (r -> r)) s a -> (i -> r -> a -> r) -> r -> s -> r
ifoldlOf' l f z0 xs = ifoldrOf l f' id xs z0
where f' i x k z = k $! f i z x
{-# INLINE ifoldlOf' #-}
ifoldrMOf :: Monad m => IndexedGetting i (Dual (Endo (r -> m r))) s a -> (i -> a -> r -> m r) -> r -> s -> m r
ifoldrMOf l f z0 xs = ifoldlOf l f' return xs z0
where f' i k x z = f i x z >>= k
{-# INLINE ifoldrMOf #-}
ifoldlMOf :: Monad m => IndexedGetting i (Endo (r -> m r)) s a -> (i -> r -> a -> m r) -> r -> s -> m r
ifoldlMOf l f z0 xs = ifoldrOf l f' return xs z0
where f' i x k z = f i z x >>= k
{-# INLINE ifoldlMOf #-}
itoListOf :: IndexedGetting i (Endo [(i,a)]) s a -> s -> [(i,a)]
itoListOf l = ifoldrOf l (\i a -> ((i,a):)) []
{-# INLINE itoListOf #-}
(^@..) :: s -> IndexedGetting i (Endo [(i,a)]) s a -> [(i,a)]
s ^@.. l = ifoldrOf l (\i a -> ((i,a):)) [] s
{-# INLINE (^@..) #-}
(^@?) :: s -> IndexedGetting i (Endo (Maybe (i, a))) s a -> Maybe (i, a)
s ^@? l = ifoldrOf l (\i x _ -> Just (i,x)) Nothing s
{-# INLINE (^@?) #-}
(^@?!) :: s -> IndexedGetting i (Endo (i, a)) s a -> (i, a)
s ^@?! l = ifoldrOf l (\i x _ -> (i,x)) (error "(^@?!): empty Fold") s
{-# INLINE (^@?!) #-}
ifiltered :: (Indexable i p, Applicative f) => (i -> a -> Bool) -> Optical' p (Indexed i) f a a
ifiltered p f = Indexed $ \i a -> if p i a then indexed f i a else pure a
{-# INLINE ifiltered #-}
itakingWhile :: (Indexable i p, Profunctor q, Contravariant f, Applicative f)
=> (i -> a -> Bool)
-> Optical (Indexed i) q (Const (Endo (f s))) s s a a
-> Optical p q f s s a a
itakingWhile p l f = (flip appEndo noEffect .# getConst) `rmap` l g where
g = Indexed $ \i a -> Const . Endo $ if p i a then (indexed f i a *>) else const noEffect
{-# INLINE itakingWhile #-}
idroppingWhile :: (Indexable i p, Profunctor q, Applicative f)
=> (i -> a -> Bool)
-> Optical (Indexed i) q (Compose (State Bool) f) s t a a
-> Optical p q f s t a a
idroppingWhile p l f = (flip evalState True .# getCompose) `rmap` l g where
g = Indexed $ \ i a -> Compose $ state $ \b -> let
b' = b && p i a
in (if b' then pure a else indexed f i a, b')
{-# INLINE idroppingWhile #-}
headOf :: Getting (First a) s a -> s -> Maybe a
headOf l = getFirst #. foldMapOf l (First #. Just)
{-# INLINE headOf #-}
{-# DEPRECATED headOf "`headOf' will be removed after GHC 7.8 is released. (Use `preview' or `firstOf')" #-}
skip :: a -> ()
skip _ = ()
{-# INLINE skip #-}
foldBy :: Foldable t => (a -> a -> a) -> a -> t a -> a
foldBy f z = reifyFold f z (foldMap M)
foldByOf :: (forall i. Getting (M a i) s a) -> (a -> a -> a) -> a -> s -> a
foldByOf l f z = reifyFold f z (foldMapOf l M)
foldMapBy :: Foldable t => (r -> r -> r) -> r -> (a -> r) -> t a -> r
foldMapBy f z g = reifyFold f z (foldMap (M #. g))
foldMapByOf :: (forall s. Getting (M r s) t a) -> (r -> r -> r) -> r -> (a -> r) -> t -> r
foldMapByOf l f z g = reifyFold f z (foldMapOf l (M #. g))