module Optics.Fold
(
Fold
, foldVL
, foldOf
, foldMapOf
, foldrOf
, foldlOf'
, toListOf
, sequenceOf_
, traverseOf_
, forOf_
, folded
, folding
, foldring
, unfolded
, has
, hasn't
, headOf
, lastOf
, andOf
, orOf
, allOf
, anyOf
, noneOf
, productOf
, sumOf
, asumOf
, msumOf
, elemOf
, notElemOf
, lengthOf
, maximumOf
, minimumOf
, maximumByOf
, minimumByOf
, findOf
, findMOf
, lookupOf
, pre
, backwards_
, summing
, failing
, A_Fold
)
where
import Control.Applicative
import Control.Applicative.Backwards
import Control.Monad
import Data.Foldable
import Data.Function
import Data.Monoid
import Data.Profunctor.Indexed
import Optics.AffineFold
import Optics.Internal.Bi
import Optics.Internal.Fold
import Optics.Internal.Optic
import Optics.Internal.Utils
type Fold s a = Optic' A_Fold NoIx s a
foldVL
:: (forall f. Applicative f => (a -> f u) -> s -> f v)
-> Fold s a
foldVL :: (forall (f :: * -> *). Applicative f => (a -> f u) -> s -> f v)
-> Fold s a
foldVL forall (f :: * -> *). Applicative f => (a -> f u) -> s -> f v
f = (forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ A_Fold p i (Curry NoIx i) s s a a)
-> Fold s a
forall k (is :: IxList) s t a b.
(forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ k p i (Curry is i) s t a b)
-> Optic k is s t a b
Optic ((forall (f :: * -> *). Applicative f => (a -> f u) -> s -> f v)
-> Optic__ p i i s s a a
forall (p :: * -> * -> * -> *) a u s v i t b.
(Bicontravariant p, Traversing p) =>
(forall (f :: * -> *). Applicative f => (a -> f u) -> s -> f v)
-> Optic__ p i i s t a b
foldVL__ forall (f :: * -> *). Applicative f => (a -> f u) -> s -> f v
f)
{-# INLINE foldVL #-}
foldOf :: (Is k A_Fold, Monoid a) => Optic' k is s a -> s -> a
foldOf :: Optic' k is s a -> s -> a
foldOf Optic' k is s a
o = Optic' k is s a -> (a -> a) -> s -> a
forall k m (is :: IxList) s a.
(Is k A_Fold, Monoid m) =>
Optic' k is s a -> (a -> m) -> s -> m
foldMapOf Optic' k is s a
o a -> a
forall a. a -> a
id
{-# INLINE foldOf #-}
foldMapOf :: (Is k A_Fold, Monoid m) => Optic' k is s a -> (a -> m) -> s -> m
foldMapOf :: Optic' k is s a -> (a -> m) -> s -> m
foldMapOf Optic' k is s a
o = Forget m (Curry is Any) s s -> s -> m
forall r i a b. Forget r i a b -> a -> r
runForget (Forget m (Curry is Any) s s -> s -> m)
-> (Forget m Any a a -> Forget m (Curry is Any) s s)
-> Forget m Any a a
-> s
-> m
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. Optic A_Fold is s s a a
-> Optic_ A_Fold (Forget m) Any (Curry is Any) s s a a
forall (p :: * -> * -> * -> *) k (is :: IxList) s t a b i.
Profunctor p =>
Optic k is s t a b -> Optic_ k p i (Curry is i) s t a b
getOptic (Optic' k is s a -> Optic A_Fold is s s a a
forall destKind srcKind (is :: IxList) s t a b.
Is srcKind destKind =>
Optic srcKind is s t a b -> Optic destKind is s t a b
castOptic @A_Fold Optic' k is s a
o) (Forget m Any a a -> s -> m)
-> ((a -> m) -> Forget m Any a a) -> (a -> m) -> s -> m
forall a b c. Coercible a b => (b -> c) -> (a -> b) -> a -> c
.# (a -> m) -> Forget m Any a a
forall r i a b. (a -> r) -> Forget r i a b
Forget
{-# INLINE foldMapOf #-}
foldrOf :: Is k A_Fold => Optic' k is s a -> (a -> r -> r) -> r -> s -> r
foldrOf :: Optic' k is s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' k is s a
o = \a -> r -> r
arr r
r s
s -> (\Endo r
e -> Endo r -> r -> r
forall a. Endo a -> a -> a
appEndo Endo r
e r
r) (Endo r -> r) -> Endo r -> r
forall a b. (a -> b) -> a -> b
$ Optic' k is s a -> (a -> Endo r) -> s -> Endo r
forall k m (is :: IxList) s a.
(Is k A_Fold, Monoid m) =>
Optic' k is s a -> (a -> m) -> s -> m
foldMapOf Optic' k is s a
o ((r -> r) -> Endo r
forall a. (a -> a) -> Endo a
Endo ((r -> r) -> Endo r) -> (a -> r -> r) -> a -> Endo r
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. a -> r -> r
arr) s
s
{-# INLINE foldrOf #-}
foldlOf' :: Is k A_Fold => Optic' k is s a -> (r -> a -> r) -> r -> s -> r
foldlOf' :: Optic' k is s a -> (r -> a -> r) -> r -> s -> r
foldlOf' Optic' k is s a
o = \r -> a -> r
rar r
r0 s
s -> Optic' k is s a
-> (a -> (r -> r) -> r -> r) -> (r -> r) -> s -> r -> r
forall k (is :: IxList) s a r.
Is k A_Fold =>
Optic' k is s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' k is s a
o (\a
a r -> r
rr r
r -> r -> r
rr (r -> r) -> r -> r
forall a b. (a -> b) -> a -> b
$! r -> a -> r
rar r
r a
a) r -> r
forall a. a -> a
id s
s r
r0
{-# INLINE foldlOf' #-}
toListOf :: Is k A_Fold => Optic' k is s a -> s -> [a]
toListOf :: Optic' k is s a -> s -> [a]
toListOf Optic' k is s a
o = Optic' k is s a -> (a -> [a] -> [a]) -> [a] -> s -> [a]
forall k (is :: IxList) s a r.
Is k A_Fold =>
Optic' k is s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' k is s a
o (:) []
{-# INLINE toListOf #-}
traverseOf_
:: (Is k A_Fold, Applicative f)
=> Optic' k is s a
-> (a -> f r) -> s -> f ()
traverseOf_ :: Optic' k is s a -> (a -> f r) -> s -> f ()
traverseOf_ Optic' k is s a
o = \a -> f r
f -> Traversed f r -> f ()
forall (f :: * -> *) a. Functor f => Traversed f a -> f ()
runTraversed (Traversed f r -> f ()) -> (s -> Traversed f r) -> s -> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Optic' k is s a -> (a -> Traversed f r) -> s -> Traversed f r
forall k m (is :: IxList) s a.
(Is k A_Fold, Monoid m) =>
Optic' k is s a -> (a -> m) -> s -> m
foldMapOf Optic' k is s a
o (f r -> Traversed f r
forall (f :: * -> *) a. f a -> Traversed f a
Traversed (f r -> Traversed f r) -> (a -> f r) -> a -> Traversed f r
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. a -> f r
f)
{-# INLINE traverseOf_ #-}
forOf_
:: (Is k A_Fold, Applicative f)
=> Optic' k is s a
-> s -> (a -> f r) -> f ()
forOf_ :: Optic' k is s a -> s -> (a -> f r) -> f ()
forOf_ = ((a -> f r) -> s -> f ()) -> s -> (a -> f r) -> f ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip (((a -> f r) -> s -> f ()) -> s -> (a -> f r) -> f ())
-> (Optic' k is s a -> (a -> f r) -> s -> f ())
-> Optic' k is s a
-> s
-> (a -> f r)
-> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Optic' k is s a -> (a -> f r) -> s -> f ()
forall k (f :: * -> *) (is :: IxList) s a r.
(Is k A_Fold, Applicative f) =>
Optic' k is s a -> (a -> f r) -> s -> f ()
traverseOf_
{-# INLINE forOf_ #-}
sequenceOf_
:: (Is k A_Fold, Applicative f)
=> Optic' k is s (f a)
-> s -> f ()
sequenceOf_ :: Optic' k is s (f a) -> s -> f ()
sequenceOf_ Optic' k is s (f a)
o = Traversed f a -> f ()
forall (f :: * -> *) a. Functor f => Traversed f a -> f ()
runTraversed (Traversed f a -> f ()) -> (s -> Traversed f a) -> s -> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Optic' k is s (f a) -> (f a -> Traversed f a) -> s -> Traversed f a
forall k m (is :: IxList) s a.
(Is k A_Fold, Monoid m) =>
Optic' k is s a -> (a -> m) -> s -> m
foldMapOf Optic' k is s (f a)
o f a -> Traversed f a
forall (f :: * -> *) a. f a -> Traversed f a
Traversed
{-# INLINE sequenceOf_ #-}
folded :: Foldable f => Fold (f a) a
folded :: Fold (f a) a
folded = (forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ A_Fold p i (Curry NoIx i) (f a) (f a) a a)
-> Fold (f a) a
forall k (is :: IxList) s t a b.
(forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ k p i (Curry is i) s t a b)
-> Optic k is s t a b
Optic forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ A_Fold p i (Curry NoIx i) (f a) (f a) a a
forall (p :: * -> * -> * -> *) (f :: * -> *) i a b.
(Bicontravariant p, Traversing p, Foldable f) =>
Optic__ p i i (f a) (f b) a b
folded__
{-# INLINE folded #-}
folding :: Foldable f => (s -> f a) -> Fold s a
folding :: (s -> f a) -> Fold s a
folding s -> f a
f = (forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ A_Fold p i (Curry NoIx i) s s a a)
-> Fold s a
forall k (is :: IxList) s t a b.
(forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ k p i (Curry is i) s t a b)
-> Optic k is s t a b
Optic ((s -> f a) -> p i (f a) s -> p i s s
forall (p :: * -> * -> * -> *) b a i c.
Bicontravariant p =>
(b -> a) -> p i a c -> p i b c
contrafirst s -> f a
f (p i (f a) s -> p i s s)
-> (p i a a -> p i (f a) s) -> p i a a -> p i s s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (f :: * -> *).
Applicative f =>
(a -> f Any) -> f a -> f ())
-> p i a a -> p i (f a) s
forall (p :: * -> * -> * -> *) a u s v i t b.
(Bicontravariant p, Traversing p) =>
(forall (f :: * -> *). Applicative f => (a -> f u) -> s -> f v)
-> Optic__ p i i s t a b
foldVL__ forall (f :: * -> *). Applicative f => (a -> f Any) -> f a -> f ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_)
{-# INLINE folding #-}
foldring
:: (forall f. Applicative f => (a -> f u -> f u) -> f v -> s -> f w)
-> Fold s a
foldring :: (forall (f :: * -> *).
Applicative f =>
(a -> f u -> f u) -> f v -> s -> f w)
-> Fold s a
foldring forall (f :: * -> *).
Applicative f =>
(a -> f u -> f u) -> f v -> s -> f w
fr = (forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ A_Fold p i (Curry NoIx i) s s a a)
-> Fold s a
forall k (is :: IxList) s t a b.
(forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ k p i (Curry is i) s t a b)
-> Optic k is s t a b
Optic ((forall (f :: * -> *).
Applicative f =>
(a -> f u -> f u) -> f v -> s -> f w)
-> Optic__ p i i s s a a
forall (p :: * -> * -> * -> *) a u v s w i t b.
(Bicontravariant p, Traversing p) =>
(forall (f :: * -> *).
Applicative f =>
(a -> f u -> f u) -> f v -> s -> f w)
-> Optic__ p i i s t a b
foldring__ forall (f :: * -> *).
Applicative f =>
(a -> f u -> f u) -> f v -> s -> f w
fr)
{-# INLINE foldring #-}
unfolded :: (s -> Maybe (a, s)) -> Fold s a
unfolded :: (s -> Maybe (a, s)) -> Fold s a
unfolded s -> Maybe (a, s)
step = (forall (f :: * -> *). Applicative f => (a -> f Any) -> s -> f ())
-> Fold s a
forall a u s v.
(forall (f :: * -> *). Applicative f => (a -> f u) -> s -> f v)
-> Fold s a
foldVL ((forall (f :: * -> *). Applicative f => (a -> f Any) -> s -> f ())
-> Fold s a)
-> (forall (f :: * -> *).
Applicative f =>
(a -> f Any) -> s -> f ())
-> Fold s a
forall a b. (a -> b) -> a -> b
$ \a -> f Any
f -> ((s -> f ()) -> s -> f ()) -> s -> f ()
forall a. (a -> a) -> a
fix (((s -> f ()) -> s -> f ()) -> s -> f ())
-> ((s -> f ()) -> s -> f ()) -> s -> f ()
forall a b. (a -> b) -> a -> b
$ \s -> f ()
loop s
b ->
case s -> Maybe (a, s)
step s
b of
Just (a
a, s
b') -> a -> f Any
f a
a f Any -> f () -> f ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> s -> f ()
loop s
b'
Maybe (a, s)
Nothing -> () -> f ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
{-# INLINE unfolded #-}
pre :: Is k A_Fold => Optic' k is s a -> AffineFold s a
pre :: Optic' k is s a -> AffineFold s a
pre = (s -> Maybe a) -> AffineFold s a
forall s a. (s -> Maybe a) -> AffineFold s a
afolding ((s -> Maybe a) -> AffineFold s a)
-> (Optic' k is s a -> s -> Maybe a)
-> Optic' k is s a
-> AffineFold s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Optic' k is s a -> s -> Maybe a
forall k (is :: IxList) s a.
Is k A_Fold =>
Optic' k is s a -> s -> Maybe a
headOf
{-# INLINE pre #-}
backwards_
:: Is k A_Fold
=> Optic' k is s a
-> Fold s a
backwards_ :: Optic' k is s a -> Fold s a
backwards_ Optic' k is s a
o = (forall (f :: * -> *). Applicative f => (a -> f Any) -> s -> f ())
-> Fold s a
forall a u s v.
(forall (f :: * -> *). Applicative f => (a -> f u) -> s -> f v)
-> Fold s a
foldVL ((forall (f :: * -> *). Applicative f => (a -> f Any) -> s -> f ())
-> Fold s a)
-> (forall (f :: * -> *).
Applicative f =>
(a -> f Any) -> s -> f ())
-> Fold s a
forall a b. (a -> b) -> a -> b
$ \a -> f Any
f -> Backwards f () -> f ()
forall k (f :: k -> *) (a :: k). Backwards f a -> f a
forwards (Backwards f () -> f ()) -> (s -> Backwards f ()) -> s -> f ()
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. Optic' k is s a -> (a -> Backwards f Any) -> s -> Backwards f ()
forall k (f :: * -> *) (is :: IxList) s a r.
(Is k A_Fold, Applicative f) =>
Optic' k is s a -> (a -> f r) -> s -> f ()
traverseOf_ Optic' k is s a
o (f Any -> Backwards f Any
forall k (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (f Any -> Backwards f Any) -> (a -> f Any) -> a -> Backwards f Any
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. a -> f Any
f)
{-# INLINE backwards_ #-}
summing
:: (Is k A_Fold, Is l A_Fold)
=> Optic' k is s a
-> Optic' l js s a
-> Fold s a
summing :: Optic' k is s a -> Optic' l js s a -> Fold s a
summing Optic' k is s a
a Optic' l js s a
b = (forall (f :: * -> *). Applicative f => (a -> f Any) -> s -> f ())
-> Fold s a
forall a u s v.
(forall (f :: * -> *). Applicative f => (a -> f u) -> s -> f v)
-> Fold s a
foldVL ((forall (f :: * -> *). Applicative f => (a -> f Any) -> s -> f ())
-> Fold s a)
-> (forall (f :: * -> *).
Applicative f =>
(a -> f Any) -> s -> f ())
-> Fold s a
forall a b. (a -> b) -> a -> b
$ \a -> f Any
f s
s -> Optic' k is s a -> (a -> f Any) -> s -> f ()
forall k (f :: * -> *) (is :: IxList) s a r.
(Is k A_Fold, Applicative f) =>
Optic' k is s a -> (a -> f r) -> s -> f ()
traverseOf_ Optic' k is s a
a a -> f Any
f s
s f () -> f () -> f ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Optic' l js s a -> (a -> f Any) -> s -> f ()
forall k (f :: * -> *) (is :: IxList) s a r.
(Is k A_Fold, Applicative f) =>
Optic' k is s a -> (a -> f r) -> s -> f ()
traverseOf_ Optic' l js s a
b a -> f Any
f s
s
infixr 6 `summing`
{-# INLINE summing #-}
failing
:: (Is k A_Fold, Is l A_Fold)
=> Optic' k is s a
-> Optic' l js s a
-> Fold s a
failing :: Optic' k is s a -> Optic' l js s a -> Fold s a
failing Optic' k is s a
a Optic' l js s a
b = (forall (f :: * -> *). Applicative f => (a -> f Any) -> s -> f ())
-> Fold s a
forall a u s v.
(forall (f :: * -> *). Applicative f => (a -> f u) -> s -> f v)
-> Fold s a
foldVL ((forall (f :: * -> *). Applicative f => (a -> f Any) -> s -> f ())
-> Fold s a)
-> (forall (f :: * -> *).
Applicative f =>
(a -> f Any) -> s -> f ())
-> Fold s a
forall a b. (a -> b) -> a -> b
$ \a -> f Any
f s
s ->
let OrT Bool
visited f ()
fu = Optic' k is s a -> (a -> OrT f Any) -> s -> OrT f ()
forall k (f :: * -> *) (is :: IxList) s a r.
(Is k A_Fold, Applicative f) =>
Optic' k is s a -> (a -> f r) -> s -> f ()
traverseOf_ Optic' k is s a
a (f Any -> OrT f Any
forall (f :: * -> *) a. f a -> OrT f a
wrapOrT (f Any -> OrT f Any) -> (a -> f Any) -> a -> OrT f Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> f Any
f) s
s
in if Bool
visited
then f ()
fu
else Optic' l js s a -> (a -> f Any) -> s -> f ()
forall k (f :: * -> *) (is :: IxList) s a r.
(Is k A_Fold, Applicative f) =>
Optic' k is s a -> (a -> f r) -> s -> f ()
traverseOf_ Optic' l js s a
b a -> f Any
f s
s
infixl 3 `failing`
{-# INLINE failing #-}
has :: Is k A_Fold => Optic' k is s a -> s -> Bool
has :: Optic' k is s a -> s -> Bool
has Optic' k is s a
o = Any -> Bool
getAny (Any -> Bool) -> (s -> Any) -> s -> Bool
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. Optic' k is s a -> (a -> Any) -> s -> Any
forall k m (is :: IxList) s a.
(Is k A_Fold, Monoid m) =>
Optic' k is s a -> (a -> m) -> s -> m
foldMapOf Optic' k is s a
o (\a
_ -> Bool -> Any
Any Bool
True)
{-# INLINE has #-}
hasn't :: Is k A_Fold => Optic' k is s a -> s -> Bool
hasn't :: Optic' k is s a -> s -> Bool
hasn't Optic' k is s a
o = All -> Bool
getAll (All -> Bool) -> (s -> All) -> s -> Bool
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. Optic' k is s a -> (a -> All) -> s -> All
forall k m (is :: IxList) s a.
(Is k A_Fold, Monoid m) =>
Optic' k is s a -> (a -> m) -> s -> m
foldMapOf Optic' k is s a
o (\a
_ -> Bool -> All
All Bool
False)
{-# INLINE hasn't #-}
headOf :: Is k A_Fold => Optic' k is s a -> s -> Maybe a
headOf :: Optic' k is s a -> s -> Maybe a
headOf Optic' k is s a
o = Leftmost a -> Maybe a
forall a. Leftmost a -> Maybe a
getLeftmost (Leftmost a -> Maybe a) -> (s -> Leftmost a) -> s -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Optic' k is s a -> (a -> Leftmost a) -> s -> Leftmost a
forall k m (is :: IxList) s a.
(Is k A_Fold, Monoid m) =>
Optic' k is s a -> (a -> m) -> s -> m
foldMapOf Optic' k is s a
o a -> Leftmost a
forall a. a -> Leftmost a
LLeaf
{-# INLINE headOf #-}
lastOf :: Is k A_Fold => Optic' k is s a -> s -> Maybe a
lastOf :: Optic' k is s a -> s -> Maybe a
lastOf Optic' k is s a
o = Rightmost a -> Maybe a
forall a. Rightmost a -> Maybe a
getRightmost (Rightmost a -> Maybe a) -> (s -> Rightmost a) -> s -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Optic' k is s a -> (a -> Rightmost a) -> s -> Rightmost a
forall k m (is :: IxList) s a.
(Is k A_Fold, Monoid m) =>
Optic' k is s a -> (a -> m) -> s -> m
foldMapOf Optic' k is s a
o a -> Rightmost a
forall a. a -> Rightmost a
RLeaf
{-# INLINE lastOf #-}
andOf :: Is k A_Fold => Optic' k is s Bool -> s -> Bool
andOf :: Optic' k is s Bool -> s -> Bool
andOf Optic' k is s Bool
o = All -> Bool
getAll (All -> Bool) -> (s -> All) -> s -> Bool
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. Optic' k is s Bool -> (Bool -> All) -> s -> All
forall k m (is :: IxList) s a.
(Is k A_Fold, Monoid m) =>
Optic' k is s a -> (a -> m) -> s -> m
foldMapOf Optic' k is s Bool
o Bool -> All
All
{-# INLINE andOf #-}
orOf :: Is k A_Fold => Optic' k is s Bool -> s -> Bool
orOf :: Optic' k is s Bool -> s -> Bool
orOf Optic' k is s Bool
o = Any -> Bool
getAny (Any -> Bool) -> (s -> Any) -> s -> Bool
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. Optic' k is s Bool -> (Bool -> Any) -> s -> Any
forall k m (is :: IxList) s a.
(Is k A_Fold, Monoid m) =>
Optic' k is s a -> (a -> m) -> s -> m
foldMapOf Optic' k is s Bool
o Bool -> Any
Any
{-# INLINE orOf #-}
anyOf :: Is k A_Fold => Optic' k is s a -> (a -> Bool) -> s -> Bool
anyOf :: Optic' k is s a -> (a -> Bool) -> s -> Bool
anyOf Optic' k is s a
o = \a -> Bool
f -> Any -> Bool
getAny (Any -> Bool) -> (s -> Any) -> s -> Bool
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. Optic' k is s a -> (a -> Any) -> s -> Any
forall k m (is :: IxList) s a.
(Is k A_Fold, Monoid m) =>
Optic' k is s a -> (a -> m) -> s -> m
foldMapOf Optic' k is s a
o (Bool -> Any
Any (Bool -> Any) -> (a -> Bool) -> a -> Any
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. a -> Bool
f)
{-# INLINE anyOf #-}
allOf :: Is k A_Fold => Optic' k is s a -> (a -> Bool) -> s -> Bool
allOf :: Optic' k is s a -> (a -> Bool) -> s -> Bool
allOf Optic' k is s a
o = \a -> Bool
f -> All -> Bool
getAll (All -> Bool) -> (s -> All) -> s -> Bool
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. Optic' k is s a -> (a -> All) -> s -> All
forall k m (is :: IxList) s a.
(Is k A_Fold, Monoid m) =>
Optic' k is s a -> (a -> m) -> s -> m
foldMapOf Optic' k is s a
o (Bool -> All
All (Bool -> All) -> (a -> Bool) -> a -> All
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. a -> Bool
f)
{-# INLINE allOf #-}
noneOf :: Is k A_Fold => Optic' k is s a -> (a -> Bool) -> s -> Bool
noneOf :: Optic' k is s a -> (a -> Bool) -> s -> Bool
noneOf Optic' k is s a
o = \a -> Bool
f -> Bool -> Bool
not (Bool -> Bool) -> (s -> Bool) -> s -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Optic' k is s a -> (a -> Bool) -> s -> Bool
forall k (is :: IxList) s a.
Is k A_Fold =>
Optic' k is s a -> (a -> Bool) -> s -> Bool
anyOf Optic' k is s a
o a -> Bool
f
{-# INLINE noneOf #-}
productOf :: (Is k A_Fold, Num a) => Optic' k is s a -> s -> a
productOf :: Optic' k is s a -> s -> a
productOf Optic' k is s a
o = Optic' k is s a -> (a -> a -> a) -> a -> s -> a
forall k (is :: IxList) s a r.
Is k A_Fold =>
Optic' k is s a -> (r -> a -> r) -> r -> s -> r
foldlOf' Optic' k is s a
o a -> a -> a
forall a. Num a => a -> a -> a
(*) a
1
{-# INLINE productOf #-}
sumOf :: (Is k A_Fold, Num a) => Optic' k is s a -> s -> a
sumOf :: Optic' k is s a -> s -> a
sumOf Optic' k is s a
o = Optic' k is s a -> (a -> a -> a) -> a -> s -> a
forall k (is :: IxList) s a r.
Is k A_Fold =>
Optic' k is s a -> (r -> a -> r) -> r -> s -> r
foldlOf' Optic' k is s a
o a -> a -> a
forall a. Num a => a -> a -> a
(+) a
0
{-# INLINE sumOf #-}
asumOf :: (Is k A_Fold, Alternative f) => Optic' k is s (f a) -> s -> f a
asumOf :: Optic' k is s (f a) -> s -> f a
asumOf Optic' k is s (f a)
o = Optic' k is s (f a) -> (f a -> f a -> f a) -> f a -> s -> f a
forall k (is :: IxList) s a r.
Is k A_Fold =>
Optic' k is s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' k is s (f a)
o f a -> f a -> f a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) f a
forall (f :: * -> *) a. Alternative f => f a
empty
{-# INLINE asumOf #-}
msumOf :: (Is k A_Fold, MonadPlus m) => Optic' k is s (m a) -> s -> m a
msumOf :: Optic' k is s (m a) -> s -> m a
msumOf Optic' k is s (m a)
o = Optic' k is s (m a) -> (m a -> m a -> m a) -> m a -> s -> m a
forall k (is :: IxList) s a r.
Is k A_Fold =>
Optic' k is s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' k is s (m a)
o m a -> m a -> m a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
mplus m a
forall (m :: * -> *) a. MonadPlus m => m a
mzero
{-# INLINE msumOf #-}
elemOf :: (Is k A_Fold, Eq a) => Optic' k is s a -> a -> s -> Bool
elemOf :: Optic' k is s a -> a -> s -> Bool
elemOf Optic' k is s a
o = Optic' k is s a -> (a -> Bool) -> s -> Bool
forall k (is :: IxList) s a.
Is k A_Fold =>
Optic' k is s a -> (a -> Bool) -> s -> Bool
anyOf Optic' k is s a
o ((a -> Bool) -> s -> Bool) -> (a -> a -> Bool) -> a -> s -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)
{-# INLINE elemOf #-}
notElemOf :: (Is k A_Fold, Eq a) => Optic' k is s a -> a -> s -> Bool
notElemOf :: Optic' k is s a -> a -> s -> Bool
notElemOf Optic' k is s a
o = Optic' k is s a -> (a -> Bool) -> s -> Bool
forall k (is :: IxList) s a.
Is k A_Fold =>
Optic' k is s a -> (a -> Bool) -> s -> Bool
allOf Optic' k is s a
o ((a -> Bool) -> s -> Bool) -> (a -> a -> Bool) -> a -> s -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(/=)
{-# INLINE notElemOf #-}
lengthOf :: Is k A_Fold => Optic' k is s a -> s -> Int
lengthOf :: Optic' k is s a -> s -> Int
lengthOf Optic' k is s a
o = Optic' k is s a -> (Int -> a -> Int) -> Int -> s -> Int
forall k (is :: IxList) s a r.
Is k A_Fold =>
Optic' k is s a -> (r -> a -> r) -> r -> s -> r
foldlOf' Optic' k is s a
o (\ Int
n a
_ -> Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n) Int
0
{-# INLINE lengthOf #-}
maximumOf :: (Is k A_Fold, Ord a) => Optic' k is s a -> s -> Maybe a
maximumOf :: Optic' k is s a -> s -> Maybe a
maximumOf Optic' k is s a
o = Optic' k is s a
-> (Maybe a -> a -> Maybe a) -> Maybe a -> s -> Maybe a
forall k (is :: IxList) s a r.
Is k A_Fold =>
Optic' k is s a -> (r -> a -> r) -> r -> s -> r
foldlOf' Optic' k is s a
o Maybe a -> a -> Maybe a
forall a. Ord a => Maybe a -> a -> Maybe a
mf Maybe a
forall a. Maybe a
Nothing where
mf :: Maybe a -> a -> Maybe a
mf Maybe a
Nothing a
y = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$! a
y
mf (Just a
x) a
y = 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
forall a. Ord a => a -> a -> a
max a
x a
y
{-# INLINE maximumOf #-}
minimumOf :: (Is k A_Fold, Ord a) => Optic' k is s a -> s -> Maybe a
minimumOf :: Optic' k is s a -> s -> Maybe a
minimumOf Optic' k is s a
o = Optic' k is s a
-> (Maybe a -> a -> Maybe a) -> Maybe a -> s -> Maybe a
forall k (is :: IxList) s a r.
Is k A_Fold =>
Optic' k is s a -> (r -> a -> r) -> r -> s -> r
foldlOf' Optic' k is s a
o Maybe a -> a -> Maybe a
forall a. Ord a => Maybe a -> a -> Maybe a
mf Maybe a
forall a. Maybe a
Nothing where
mf :: Maybe a -> a -> Maybe a
mf Maybe a
Nothing a
y = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$! a
y
mf (Just a
x) a
y = 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
forall a. Ord a => a -> a -> a
min a
x a
y
{-# INLINE minimumOf #-}
maximumByOf :: Is k A_Fold => Optic' k is s a -> (a -> a -> Ordering) -> s -> Maybe a
maximumByOf :: Optic' k is s a -> (a -> a -> Ordering) -> s -> Maybe a
maximumByOf Optic' k is s a
o = \a -> a -> Ordering
cmp ->
let mf :: Maybe a -> a -> Maybe a
mf Maybe a
Nothing a
y = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$! a
y
mf (Just a
x) a
y = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$! if a -> a -> Ordering
cmp a
x a
y Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT then a
x else a
y
in Optic' k is s a
-> (Maybe a -> a -> Maybe a) -> Maybe a -> s -> Maybe a
forall k (is :: IxList) s a r.
Is k A_Fold =>
Optic' k is s a -> (r -> a -> r) -> r -> s -> r
foldlOf' Optic' k is s a
o Maybe a -> a -> Maybe a
mf Maybe a
forall a. Maybe a
Nothing
{-# INLINE maximumByOf #-}
minimumByOf :: Is k A_Fold => Optic' k is s a -> (a -> a -> Ordering) -> s -> Maybe a
minimumByOf :: Optic' k is s a -> (a -> a -> Ordering) -> s -> Maybe a
minimumByOf Optic' k is s a
o = \a -> a -> Ordering
cmp ->
let mf :: Maybe a -> a -> Maybe a
mf Maybe a
Nothing a
y = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$! a
y
mf (Just a
x) a
y = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$! if a -> a -> Ordering
cmp a
x a
y Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT then a
y else a
x
in Optic' k is s a
-> (Maybe a -> a -> Maybe a) -> Maybe a -> s -> Maybe a
forall k (is :: IxList) s a r.
Is k A_Fold =>
Optic' k is s a -> (r -> a -> r) -> r -> s -> r
foldlOf' Optic' k is s a
o Maybe a -> a -> Maybe a
mf Maybe a
forall a. Maybe a
Nothing
{-# INLINE minimumByOf #-}
findOf :: Is k A_Fold => Optic' k is s a -> (a -> Bool) -> s -> Maybe a
findOf :: Optic' k is s a -> (a -> Bool) -> s -> Maybe a
findOf Optic' k is s a
o = \a -> Bool
f -> Optic' k is s a
-> (a -> Maybe a -> Maybe a) -> Maybe a -> s -> Maybe a
forall k (is :: IxList) s a r.
Is k A_Fold =>
Optic' k is s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' k is s a
o (\a
a Maybe a
y -> if a -> Bool
f a
a then a -> Maybe a
forall a. a -> Maybe a
Just a
a else Maybe a
y) Maybe a
forall a. Maybe a
Nothing
{-# INLINE findOf #-}
findMOf :: (Is k A_Fold, Monad m) => Optic' k is s a -> (a -> m Bool) -> s -> m (Maybe a)
findMOf :: Optic' k is s a -> (a -> m Bool) -> s -> m (Maybe a)
findMOf Optic' k is s a
o = \a -> m Bool
f -> Optic' k is s a
-> (a -> m (Maybe a) -> m (Maybe a))
-> m (Maybe a)
-> s
-> m (Maybe a)
forall k (is :: IxList) s a r.
Is k A_Fold =>
Optic' k is s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' k is s a
o
(\a
a m (Maybe a)
y -> a -> m Bool
f a
a m Bool -> (Bool -> m (Maybe a)) -> m (Maybe a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Bool
r -> if Bool
r then Maybe a -> m (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Maybe a
forall a. a -> Maybe a
Just a
a) else m (Maybe a)
y)
(Maybe a -> m (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing)
{-# INLINE findMOf #-}
lookupOf :: (Is k A_Fold, Eq a) => Optic' k is s (a, v) -> a -> s -> Maybe v
lookupOf :: Optic' k is s (a, v) -> a -> s -> Maybe v
lookupOf Optic' k is s (a, v)
o a
a = Optic' k is s (a, v)
-> ((a, v) -> Maybe v -> Maybe v) -> Maybe v -> s -> Maybe v
forall k (is :: IxList) s a r.
Is k A_Fold =>
Optic' k is s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' k is s (a, v)
o (\(a
a', v
v) Maybe v
next -> if a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
a' then v -> Maybe v
forall a. a -> Maybe a
Just v
v else Maybe v
next) Maybe v
forall a. Maybe a
Nothing
{-# INLINE lookupOf #-}