module Optics.AffineFold
(
AffineFold
, afolding
, preview
, previews
, afoldVL
, filtered
, atraverseOf_
, isn't
, afailing
, An_AffineFold
) where
import Data.Maybe
import Data.Profunctor.Indexed
import Optics.Internal.Bi
import Optics.Internal.Optic
type AffineFold s a = Optic' An_AffineFold NoIx s a
afoldVL
:: (forall f. Functor f => (forall r. r -> f r) -> (a -> f u) -> s -> f v)
-> AffineFold s a
afoldVL :: forall a u s v.
(forall (f :: * -> *).
Functor f =>
(forall r. r -> f r) -> (a -> f u) -> s -> f v)
-> AffineFold s a
afoldVL forall (f :: * -> *).
Functor f =>
(forall r. r -> f r) -> (a -> f u) -> s -> f v
f = 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 c a b.
(Profunctor p, Bicontravariant p) =>
p i c a -> p i c b
rphantom forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> * -> *) i s t a b.
Visiting p =>
(forall (f :: * -> *).
Functor f =>
(forall r. r -> f r) -> (a -> f b) -> s -> f t)
-> p i a b -> p i s t
visit forall (f :: * -> *).
Functor f =>
(forall r. r -> f r) -> (a -> f u) -> s -> f v
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> * -> *) i c a b.
(Profunctor p, Bicontravariant p) =>
p i c a -> p i c b
rphantom)
{-# INLINE afoldVL #-}
preview :: Is k An_AffineFold => Optic' k is s a -> s -> Maybe a
preview :: forall k (is :: IxList) s a.
Is k An_AffineFold =>
Optic' k is s a -> s -> Maybe a
preview Optic' k is s a
o = forall k (is :: IxList) s a r.
Is k An_AffineFold =>
Optic' k is s a -> (a -> r) -> s -> Maybe r
previews Optic' k is s a
o forall a. a -> a
id
{-# INLINE preview #-}
previews :: Is k An_AffineFold => Optic' k is s a -> (a -> r) -> s -> Maybe r
previews :: forall k (is :: IxList) s a r.
Is k An_AffineFold =>
Optic' k is s a -> (a -> r) -> s -> Maybe r
previews Optic' k is s a
o = \a -> r
f -> forall r i a b. ForgetM r i a b -> a -> Maybe r
runForgetM forall a b. (a -> b) -> a -> b
$
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 (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 @An_AffineFold Optic' k is s a
o) forall a b. (a -> b) -> a -> b
$ forall r i a b. (a -> Maybe r) -> ForgetM r i a b
ForgetM (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> r
f)
{-# INLINE previews #-}
atraverseOf_
:: (Is k An_AffineFold, Functor f)
=> Optic' k is s a
-> (forall r. r -> f r) -> (a -> f u) -> s -> f ()
atraverseOf_ :: forall k (f :: * -> *) (is :: IxList) s a u.
(Is k An_AffineFold, Functor f) =>
Optic' k is s a -> (forall r. r -> f r) -> (a -> f u) -> s -> f ()
atraverseOf_ Optic' k is s a
o forall r. r -> f r
point a -> f u
f s
s = case forall k (is :: IxList) s a.
Is k An_AffineFold =>
Optic' k is s a -> s -> Maybe a
preview Optic' k is s a
o s
s of
Just a
a -> () forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ a -> f u
f a
a
Maybe a
Nothing -> forall r. r -> f r
point ()
{-# INLINE atraverseOf_ #-}
afolding :: (s -> Maybe a) -> AffineFold s a
afolding :: forall s a. (s -> Maybe a) -> AffineFold s a
afolding s -> Maybe a
f = 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 :: * -> * -> * -> *) b a d c i.
Bicontravariant p =>
(b -> a) -> (d -> c) -> p i a c -> p i b d
contrabimap (\s
s -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. a -> Either a b
Left s
s) forall a b. b -> Either a b
Right (s -> Maybe a
f s
s)) forall a b. a -> Either a b
Left forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> * -> *) i a b c.
Choice p =>
p i a b -> p i (Either c a) (Either c b)
right')
{-# INLINE afolding #-}
filtered :: (a -> Bool) -> AffineFold a a
filtered :: forall a. (a -> Bool) -> AffineFold a a
filtered a -> Bool
p = forall a u s v.
(forall (f :: * -> *).
Functor f =>
(forall r. r -> f r) -> (a -> f u) -> s -> f v)
-> AffineFold s a
afoldVL (\forall r. r -> f r
point a -> f a
f a
a -> if a -> Bool
p a
a then a -> f a
f a
a else forall r. r -> f r
point a
a)
{-# INLINE filtered #-}
afailing
:: (Is k An_AffineFold, Is l An_AffineFold)
=> Optic' k is s a
-> Optic' l js s a
-> AffineFold s a
afailing :: forall k l (is :: IxList) s a (js :: IxList).
(Is k An_AffineFold, Is l An_AffineFold) =>
Optic' k is s a -> Optic' l js s a -> AffineFold s a
afailing Optic' k is s a
a Optic' l js s a
b = forall s a. (s -> Maybe a) -> AffineFold s a
afolding forall a b. (a -> b) -> a -> b
$ \s
s -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall k (is :: IxList) s a.
Is k An_AffineFold =>
Optic' k is s a -> s -> Maybe a
preview Optic' l js s a
b s
s) forall a. a -> Maybe a
Just (forall k (is :: IxList) s a.
Is k An_AffineFold =>
Optic' k is s a -> s -> Maybe a
preview Optic' k is s a
a s
s)
infixl 3 `afailing`
{-# INLINE afailing #-}
isn't :: Is k An_AffineFold => Optic' k is s a -> s -> Bool
isn't :: forall k (is :: IxList) s a.
Is k An_AffineFold =>
Optic' k is s a -> s -> Bool
isn't Optic' k is s a
k s
s = forall a. Maybe a -> Bool
isNothing (forall k (is :: IxList) s a.
Is k An_AffineFold =>
Optic' k is s a -> s -> Maybe a
preview Optic' k is s a
k s
s)
{-# INLINE isn't #-}