| Copyright | (C) 2012-16 Edward Kmett |
|---|---|
| License | BSD-style (see the file LICENSE) |
| Maintainer | Edward Kmett <ekmett@gmail.com> |
| Stability | provisional |
| Portability | Rank2Types |
| Safe Haskell | Trustworthy |
| Language | Haskell2010 |
Control.Lens.Fold
Description
A is a generalization of something Fold s aFoldable. It allows
you to extract multiple results from a container. A Foldable container
can be characterized by the behavior of
.
Since we want to be able to work with monomorphic containers, we could
generalize this signature to foldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> mforall m. ,
and then decorate it with Monoid m => (a -> m) -> s -> mConst to obtain
typeFolds a = forall m.Monoidm =>Gettingm s a
Every Getter is a valid Fold that simply doesn't use the Monoid
it is passed.
In practice the type we use is slightly more complicated to allow for
better error messages and for it to be transformed by certain
Applicative transformers.
Everything you can do with a Foldable container, you can with with a Fold and there are
combinators that generalize the usual Foldable operations here.
Synopsis
- type Fold s a = forall f. (Contravariant f, Applicative f) => (a -> f a) -> s -> f s
- type IndexedFold i s a = forall p f. (Indexable i p, Contravariant f, Applicative f) => p a (f a) -> s -> f s
- (^..) :: s -> Getting (Endo [a]) s a -> [a]
- (^?) :: s -> Getting (First a) s a -> Maybe a
- (^?!) :: HasCallStack => s -> Getting (Endo a) s a -> a
- pre :: Getting (First a) s a -> IndexPreservingGetter s (Maybe a)
- ipre :: IndexedGetting i (First (i, a)) s a -> IndexPreservingGetter s (Maybe (i, a))
- preview :: MonadReader s m => Getting (First a) s a -> m (Maybe a)
- previews :: MonadReader s m => Getting (First r) s a -> (a -> r) -> m (Maybe r)
- ipreview :: MonadReader s m => IndexedGetting i (First (i, a)) s a -> m (Maybe (i, a))
- ipreviews :: MonadReader s m => IndexedGetting i (First r) s a -> (i -> a -> r) -> m (Maybe r)
- preuse :: MonadState s m => Getting (First a) s a -> m (Maybe a)
- preuses :: MonadState s m => Getting (First r) s a -> (a -> r) -> m (Maybe r)
- ipreuse :: MonadState s m => IndexedGetting i (First (i, a)) s a -> m (Maybe (i, a))
- ipreuses :: MonadState s m => IndexedGetting i (First r) s a -> (i -> a -> r) -> m (Maybe r)
- has :: Getting Any s a -> s -> Bool
- hasn't :: Getting All s a -> s -> Bool
- folding :: Foldable f => (s -> f a) -> Fold s a
- ifolding :: (Foldable f, Indexable i p, Contravariant g, Applicative g) => (s -> f (i, a)) -> Over p g s t a b
- foldring :: (Contravariant f, Applicative f) => ((a -> f a -> f a) -> f a -> s -> f a) -> LensLike f s t a b
- ifoldring :: (Indexable i p, Contravariant f, Applicative f) => ((i -> a -> f a -> f a) -> f a -> s -> f a) -> Over p f s t a b
- folded :: Foldable f => IndexedFold Int (f a) a
- folded64 :: Foldable f => IndexedFold Int64 (f a) a
- unfolded :: (b -> Maybe (a, b)) -> Fold b a
- iterated :: Apply f => (a -> a) -> LensLike' f a a
- filtered :: (Choice p, Applicative f) => (a -> Bool) -> Optic' p f a a
- filteredBy :: (Indexable i p, Applicative f) => Getting (First i) a i -> p a (f a) -> a -> f a
- backwards :: (Profunctor p, Profunctor q) => Optical p q (Backwards f) s t a b -> Optical p q f s t a b
- repeated :: Apply f => LensLike' f a a
- replicated :: Int -> Fold a a
- cycled :: Apply f => LensLike f s t a b -> LensLike f s t a b
- 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
- 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
- worded :: Applicative f => IndexedLensLike' Int f String String
- lined :: Applicative f => IndexedLensLike' Int f String String
- foldMapOf :: Getting r s a -> (a -> r) -> s -> r
- foldOf :: Getting a s a -> s -> a
- foldrOf :: Getting (Endo r) s a -> (a -> r -> r) -> r -> s -> r
- foldlOf :: Getting (Dual (Endo r)) s a -> (r -> a -> r) -> r -> s -> r
- toListOf :: Getting (Endo [a]) s a -> s -> [a]
- toNonEmptyOf :: Getting (NonEmptyDList a) s a -> s -> NonEmpty a
- anyOf :: Getting Any s a -> (a -> Bool) -> s -> Bool
- allOf :: Getting All s a -> (a -> Bool) -> s -> Bool
- noneOf :: Getting Any s a -> (a -> Bool) -> s -> Bool
- andOf :: Getting All s Bool -> s -> Bool
- orOf :: Getting Any s Bool -> s -> Bool
- productOf :: Num a => Getting (Endo (Endo a)) s a -> s -> a
- sumOf :: Num a => Getting (Endo (Endo a)) s a -> s -> a
- traverseOf_ :: Functor f => Getting (Traversed r f) s a -> (a -> f r) -> s -> f ()
- forOf_ :: Functor f => Getting (Traversed r f) s a -> s -> (a -> f r) -> f ()
- sequenceAOf_ :: Functor f => Getting (Traversed a f) s (f a) -> s -> f ()
- traverse1Of_ :: Functor f => Getting (TraversedF r f) s a -> (a -> f r) -> s -> f ()
- for1Of_ :: Functor f => Getting (TraversedF r f) s a -> s -> (a -> f r) -> f ()
- sequence1Of_ :: Functor f => Getting (TraversedF a f) s (f a) -> s -> f ()
- mapMOf_ :: Monad m => Getting (Sequenced r m) s a -> (a -> m r) -> s -> m ()
- forMOf_ :: Monad m => Getting (Sequenced r m) s a -> s -> (a -> m r) -> m ()
- sequenceOf_ :: Monad m => Getting (Sequenced a m) s (m a) -> s -> m ()
- asumOf :: Alternative f => Getting (Endo (f a)) s (f a) -> s -> f a
- msumOf :: MonadPlus m => Getting (Endo (m a)) s (m a) -> s -> m a
- concatMapOf :: Getting [r] s a -> (a -> [r]) -> s -> [r]
- concatOf :: Getting [r] s [r] -> s -> [r]
- elemOf :: Eq a => Getting Any s a -> a -> s -> Bool
- notElemOf :: Eq a => Getting All s a -> a -> s -> Bool
- lengthOf :: Getting (Endo (Endo Int)) s a -> s -> Int
- nullOf :: Getting All s a -> s -> Bool
- notNullOf :: Getting Any s a -> s -> Bool
- firstOf :: Getting (Leftmost a) s a -> s -> Maybe a
- first1Of :: Getting (First a) s a -> s -> a
- lastOf :: Getting (Rightmost a) s a -> s -> Maybe a
- last1Of :: Getting (Last a) s a -> s -> a
- maximumOf :: Ord a => Getting (Endo (Endo (Maybe a))) s a -> s -> Maybe a
- maximum1Of :: Ord a => Getting (Max a) s a -> s -> a
- minimumOf :: Ord a => Getting (Endo (Endo (Maybe a))) s a -> s -> Maybe a
- minimum1Of :: Ord a => Getting (Min a) s a -> s -> a
- maximumByOf :: Getting (Endo (Endo (Maybe a))) s a -> (a -> a -> Ordering) -> s -> Maybe a
- minimumByOf :: Getting (Endo (Endo (Maybe a))) s a -> (a -> a -> Ordering) -> s -> Maybe a
- findOf :: Getting (Endo (Maybe a)) s a -> (a -> Bool) -> s -> Maybe a
- findMOf :: Monad m => Getting (Endo (m (Maybe a))) s a -> (a -> m Bool) -> s -> m (Maybe a)
- foldrOf' :: Getting (Dual (Endo (Endo r))) s a -> (a -> r -> r) -> r -> s -> r
- foldlOf' :: Getting (Endo (Endo r)) s a -> (r -> a -> r) -> r -> s -> r
- foldr1Of :: HasCallStack => Getting (Endo (Maybe a)) s a -> (a -> a -> a) -> s -> a
- foldl1Of :: HasCallStack => Getting (Dual (Endo (Maybe a))) s a -> (a -> a -> a) -> s -> a
- foldr1Of' :: HasCallStack => Getting (Dual (Endo (Endo (Maybe a)))) s a -> (a -> a -> a) -> s -> a
- foldl1Of' :: HasCallStack => Getting (Endo (Endo (Maybe a))) s a -> (a -> a -> a) -> s -> a
- foldrMOf :: Monad m => Getting (Dual (Endo (r -> m r))) s a -> (a -> r -> m r) -> r -> s -> m r
- foldlMOf :: Monad m => Getting (Endo (r -> m r)) s a -> (r -> a -> m r) -> r -> s -> m r
- lookupOf :: Eq k => Getting (Endo (Maybe v)) s (k, v) -> k -> s -> Maybe v
- (^@..) :: s -> IndexedGetting i (Endo [(i, a)]) s a -> [(i, a)]
- (^@?) :: s -> IndexedGetting i (Endo (Maybe (i, a))) s a -> Maybe (i, a)
- (^@?!) :: HasCallStack => s -> IndexedGetting i (Endo (i, a)) s a -> (i, a)
- ifoldMapOf :: IndexedGetting i m s a -> (i -> a -> m) -> s -> m
- ifoldrOf :: IndexedGetting i (Endo r) s a -> (i -> a -> r -> r) -> r -> s -> r
- ifoldlOf :: IndexedGetting i (Dual (Endo r)) s a -> (i -> r -> a -> r) -> r -> s -> r
- ianyOf :: IndexedGetting i Any s a -> (i -> a -> Bool) -> s -> Bool
- iallOf :: IndexedGetting i All s a -> (i -> a -> Bool) -> s -> Bool
- inoneOf :: IndexedGetting i Any s a -> (i -> a -> Bool) -> s -> Bool
- itraverseOf_ :: Functor f => IndexedGetting i (Traversed r f) s a -> (i -> a -> f r) -> s -> f ()
- iforOf_ :: Functor f => IndexedGetting i (Traversed r f) s a -> s -> (i -> a -> f r) -> f ()
- imapMOf_ :: Monad m => IndexedGetting i (Sequenced r m) s a -> (i -> a -> m r) -> s -> m ()
- iforMOf_ :: Monad m => IndexedGetting i (Sequenced r m) s a -> s -> (i -> a -> m r) -> m ()
- iconcatMapOf :: IndexedGetting i [r] s a -> (i -> a -> [r]) -> s -> [r]
- ifindOf :: IndexedGetting i (Endo (Maybe a)) s a -> (i -> a -> Bool) -> s -> Maybe a
- ifindMOf :: Monad m => IndexedGetting i (Endo (m (Maybe a))) s a -> (i -> a -> m Bool) -> s -> m (Maybe a)
- ifoldrOf' :: IndexedGetting i (Dual (Endo (r -> r))) s a -> (i -> a -> r -> r) -> r -> s -> r
- ifoldlOf' :: IndexedGetting i (Endo (r -> r)) s a -> (i -> r -> a -> r) -> r -> s -> r
- ifoldrMOf :: Monad m => IndexedGetting i (Dual (Endo (r -> m r))) s a -> (i -> a -> r -> m r) -> r -> s -> m r
- ifoldlMOf :: Monad m => IndexedGetting i (Endo (r -> m r)) s a -> (i -> r -> a -> m r) -> r -> s -> m r
- itoListOf :: IndexedGetting i (Endo [(i, a)]) s a -> s -> [(i, a)]
- elemIndexOf :: Eq a => IndexedGetting i (First i) s a -> a -> s -> Maybe i
- elemIndicesOf :: Eq a => IndexedGetting i (Endo [i]) s a -> a -> s -> [i]
- findIndexOf :: IndexedGetting i (First i) s a -> (a -> Bool) -> s -> Maybe i
- findIndicesOf :: IndexedGetting i (Endo [i]) s a -> (a -> Bool) -> s -> [i]
- ifiltered :: (Indexable i p, Applicative f) => (i -> a -> Bool) -> Optical' p (Indexed i) f a a
- itakingWhile :: (Indexable i p, Profunctor q, Contravariant f, Applicative f) => (i -> a -> Bool) -> Optical' (Indexed i) q (Const (Endo (f s))) s a -> Optical' p q f s a
- 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
- data Leftmost a
- data Rightmost a
- data Traversed a f
- data Sequenced a m
- foldBy :: Foldable t => (a -> a -> a) -> a -> t a -> a
- foldByOf :: Fold s a -> (a -> a -> a) -> a -> s -> a
- foldMapBy :: Foldable t => (r -> r -> r) -> r -> (a -> r) -> t a -> r
- foldMapByOf :: Fold s a -> (r -> r -> r) -> r -> (a -> r) -> s -> r
Folds
type Fold s a = forall f. (Contravariant f, Applicative f) => (a -> f a) -> s -> f s Source #
A Fold describes how to retrieve multiple values in a way that can be composed
with other LensLike constructions.
A provides a structure with operations very similar to those of the Fold s aFoldable
typeclass, see foldMapOf and the other Fold combinators.
By convention, if there exists a foo method that expects a , then there should be a
Foldable (f a)fooOf method that takes a and a value of type Fold s as.
A Getter is a legal Fold that just ignores the supplied Monoid.
Unlike a Traversal a Fold is read-only. Since a Fold cannot be used to write back
there are no Lens laws that apply.
type IndexedFold i s a = forall p f. (Indexable i p, Contravariant f, Applicative f) => p a (f a) -> s -> f s Source #
Every IndexedFold is a valid Fold and can be used for Getting.
Getting Started
(^..) :: s -> Getting (Endo [a]) s a -> [a] infixl 8 Source #
A convenient infix (flipped) version of toListOf.
>>>[[1,2],[3]]^..id[[[1,2],[3]]]>>>[[1,2],[3]]^..traverse[[1,2],[3]]>>>[[1,2],[3]]^..traverse.traverse[1,2,3]
>>>(1,2)^..both[1,2]
toListxs ≡ xs^..folded(^..) ≡fliptoListOf
(^..) :: s ->Getters a -> a :: s ->Folds a -> a :: s ->Lens's a -> a :: s ->Iso's a -> a :: s ->Traversal's a -> a :: s ->Prism's a -> [a]
(^?) :: s -> Getting (First a) s a -> Maybe a infixl 8 Source #
Perform a safe head of a Fold or Traversal or retrieve Just the result
from a Getter or Lens.
When using a Traversal as a partial Lens, or a Fold as a partial Getter this can be a convenient
way to extract the optional value.
Note: if you get stack overflows due to this, you may want to use firstOf instead, which can deal
more gracefully with heavily left-biased trees. This is because ^? works by using the
First monoid, which can occasionally cause space leaks.
>>>Left 4 ^?_LeftJust 4
>>>Right 4 ^?_LeftNothing
>>>"world" ^? ix 3Just 'l'
>>>"world" ^? ix 20Nothing
This operator works as an infix version of preview.
(^?) ≡flippreview
It may be helpful to think of ^? as having one of the following
more specialized types:
(^?) :: s ->Getters a ->Maybea (^?) :: s ->Folds a ->Maybea (^?) :: s ->Lens's a ->Maybea (^?) :: s ->Iso's a ->Maybea (^?) :: s ->Traversal's a ->Maybea
pre :: Getting (First a) s a -> IndexPreservingGetter s (Maybe a) Source #
This converts a Fold to a IndexPreservingGetter that returns the first element, if it
exists, as a Maybe.
pre::Getters a ->IndexPreservingGetters (Maybea)pre::Folds a ->IndexPreservingGetters (Maybea)pre::Traversal's a ->IndexPreservingGetters (Maybea)pre::Lens's a ->IndexPreservingGetters (Maybea)pre::Iso's a ->IndexPreservingGetters (Maybea)pre::Prism's a ->IndexPreservingGetters (Maybea)
ipre :: IndexedGetting i (First (i, a)) s a -> IndexPreservingGetter s (Maybe (i, a)) Source #
This converts an IndexedFold to an IndexPreservingGetter that returns the first index
and element, if they exist, as a Maybe.
ipre::IndexedGetteri s a ->IndexPreservingGetters (Maybe(i, a))ipre::IndexedFoldi s a ->IndexPreservingGetters (Maybe(i, a))ipre::IndexedTraversal'i s a ->IndexPreservingGetters (Maybe(i, a))ipre::IndexedLens'i s a ->IndexPreservingGetters (Maybe(i, a))
preview :: MonadReader s m => Getting (First a) s a -> m (Maybe a) Source #
Retrieve the first value targeted by a Fold or Traversal (or Just the result
from a Getter or Lens). See also firstOf and ^?, which are similar with
some subtle differences (explained below).
listToMaybe.toList≡previewfolded
preview=view.pre
Unlike ^?, this function uses a
MonadReader to read the value to be focused in on.
This allows one to pass the value as the last argument by using the
MonadReader instance for (->) s
However, it may also be used as part of some deeply nested transformer stack.
preview uses a monoidal value to obtain the result.
This means that it generally has good performance, but can occasionally cause space leaks
or even stack overflows on some data types.
There is another function, firstOf, which avoids these issues at the cost of
a slight constant performance cost and a little less flexibility.
It may be helpful to think of preview as having one of the following
more specialized types:
preview::Getters a -> s ->Maybeapreview::Folds a -> s ->Maybeapreview::Lens's a -> s ->Maybeapreview::Iso's a -> s ->Maybeapreview::Traversal's a -> s ->Maybea
preview::MonadReaders m =>Getters a -> m (Maybea)preview::MonadReaders m =>Folds a -> m (Maybea)preview::MonadReaders m =>Lens's a -> m (Maybea)preview::MonadReaders m =>Iso's a -> m (Maybea)preview::MonadReaders m =>Traversal's a -> m (Maybea)
ipreview :: MonadReader s m => IndexedGetting i (First (i, a)) s a -> m (Maybe (i, a)) Source #
Retrieve the first index and value targeted by a Fold or Traversal (or Just the result
from a Getter or Lens). See also (^@?).
ipreview=view.ipre
This is usually applied in the Reader
Monad (->) s.
ipreview::IndexedGetteri s a -> s ->Maybe(i, a)ipreview::IndexedFoldi s a -> s ->Maybe(i, a)ipreview::IndexedLens'i s a -> s ->Maybe(i, a)ipreview::IndexedTraversal'i s a -> s ->Maybe(i, a)
However, it may be useful to think of its full generality when working with
a Monad transformer stack:
ipreview::MonadReaders m =>IndexedGetters a -> m (Maybe(i, a))ipreview::MonadReaders m =>IndexedFolds a -> m (Maybe(i, a))ipreview::MonadReaders m =>IndexedLens's a -> m (Maybe(i, a))ipreview::MonadReaders m =>IndexedTraversal's a -> m (Maybe(i, a))
ipreviews :: MonadReader s m => IndexedGetting i (First r) s a -> (i -> a -> r) -> m (Maybe r) Source #
Retrieve a function of the first index and value targeted by an IndexedFold or
IndexedTraversal (or Just the result from an IndexedGetter or IndexedLens).
See also (^@?).
ipreviews=views.ipre
This is usually applied in the Reader
Monad (->) s.
ipreviews::IndexedGetteri s a -> (i -> a -> r) -> s ->Mayberipreviews::IndexedFoldi s a -> (i -> a -> r) -> s ->Mayberipreviews::IndexedLens'i s a -> (i -> a -> r) -> s ->Mayberipreviews::IndexedTraversal'i s a -> (i -> a -> r) -> s ->Mayber
However, it may be useful to think of its full generality when working with
a Monad transformer stack:
ipreviews::MonadReaders m =>IndexedGetteri s a -> (i -> a -> r) -> m (Mayber)ipreviews::MonadReaders m =>IndexedFoldi s a -> (i -> a -> r) -> m (Mayber)ipreviews::MonadReaders m =>IndexedLens'i s a -> (i -> a -> r) -> m (Mayber)ipreviews::MonadReaders m =>IndexedTraversal'i s a -> (i -> a -> r) -> m (Mayber)
preuse :: MonadState s m => Getting (First a) s a -> m (Maybe a) Source #
Retrieve the first value targeted by a Fold or Traversal (or Just the result
from a Getter or Lens) into the current state.
preuse=use.pre
preuse::MonadStates m =>Getters a -> m (Maybea)preuse::MonadStates m =>Folds a -> m (Maybea)preuse::MonadStates m =>Lens's a -> m (Maybea)preuse::MonadStates m =>Iso's a -> m (Maybea)preuse::MonadStates m =>Traversal's a -> m (Maybea)
preuses :: MonadState s m => Getting (First r) s a -> (a -> r) -> m (Maybe r) Source #
Retrieve a function of the first value targeted by a Fold or
Traversal (or Just the result from a Getter or Lens) into the current state.
preuses=uses.pre
preuses::MonadStates m =>Getters a -> (a -> r) -> m (Mayber)preuses::MonadStates m =>Folds a -> (a -> r) -> m (Mayber)preuses::MonadStates m =>Lens's a -> (a -> r) -> m (Mayber)preuses::MonadStates m =>Iso's a -> (a -> r) -> m (Mayber)preuses::MonadStates m =>Traversal's a -> (a -> r) -> m (Mayber)
ipreuse :: MonadState s m => IndexedGetting i (First (i, a)) s a -> m (Maybe (i, a)) Source #
Retrieve the first index and value targeted by an IndexedFold or IndexedTraversal (or Just the index
and result from an IndexedGetter or IndexedLens) into the current state.
ipreuse=use.ipre
ipreuse::MonadStates m =>IndexedGetteri s a -> m (Maybe(i, a))ipreuse::MonadStates m =>IndexedFoldi s a -> m (Maybe(i, a))ipreuse::MonadStates m =>IndexedLens'i s a -> m (Maybe(i, a))ipreuse::MonadStates m =>IndexedTraversal'i s a -> m (Maybe(i, a))
ipreuses :: MonadState s m => IndexedGetting i (First r) s a -> (i -> a -> r) -> m (Maybe r) Source #
Retrieve a function of the first index and value targeted by an IndexedFold or
IndexedTraversal (or a function of Just the index and result from an IndexedGetter
or IndexedLens) into the current state.
ipreuses=uses.ipre
ipreuses::MonadStates m =>IndexedGetteri s a -> (i -> a -> r) -> m (Mayber)ipreuses::MonadStates m =>IndexedFoldi s a -> (i -> a -> r) -> m (Mayber)ipreuses::MonadStates m =>IndexedLens'i s a -> (i -> a -> r) -> m (Mayber)ipreuses::MonadStates m =>IndexedTraversal'i s a -> (i -> a -> r) -> m (Mayber)
has :: Getting Any s a -> s -> Bool Source #
Check to see if this Fold or Traversal matches 1 or more entries.
>>>has (element 0) []False
>>>has _Left (Left 12)True
>>>has _Right (Left 12)False
This will always return True for a Lens or Getter.
>>>has _1 ("hello","world")True
has::Getters a -> s ->Boolhas::Folds a -> s ->Boolhas::Iso's a -> s ->Boolhas::Lens's a -> s ->Boolhas::Traversal's a -> s ->Bool
Building Folds
ifolding :: (Foldable f, Indexable i p, Contravariant g, Applicative g) => (s -> f (i, a)) -> Over p g s t a b Source #
foldring :: (Contravariant f, Applicative f) => ((a -> f a -> f a) -> f a -> s -> f a) -> LensLike f s t a b Source #
ifoldring :: (Indexable i p, Contravariant f, Applicative f) => ((i -> a -> f a -> f a) -> f a -> s -> f a) -> Over p f s t a b Source #
Obtain FoldWithIndex by lifting ifoldr like function.
filtered :: (Choice p, Applicative f) => (a -> Bool) -> Optic' p f a a Source #
Obtain a Fold that can be composed with to filter another Lens, Iso, Getter, Fold (or Traversal).
Note: This is not a legal Traversal, unless you are very careful not to invalidate the predicate on the target.
Note: This is also not a legal Prism, unless you are very careful not to inject a value that fails the predicate.
As a counter example, consider that given evens = the second filtered evenTraversal law is violated:
overevenssucc.overevenssucc/=overevens (succ.succ)
So, in order for this to qualify as a legal Traversal you can only use it for actions that preserve the result of the predicate!
>>>[1..10]^..folded.filtered even[2,4,6,8,10]
This will preserve an index if it is present.
filteredBy :: (Indexable i p, Applicative f) => Getting (First i) a i -> p a (f a) -> a -> f a Source #
Obtain a potentially empty IndexedTraversal by taking the first element from another,
potentially empty Fold and using it as an index.
The resulting optic can be composed with to filter another Lens, Iso, Getter, Fold (or Traversal).
>>>[(Just 2, 3), (Nothing, 4)] & mapped . filteredBy (_1 . _Just) <. _2 %@~ (*) :: [(Maybe Int, Int)][(Just 2,6),(Nothing,4)]
filteredBy::Folda i ->IndexedTraversal'i a a
Note: As with filtered, this is not a legal IndexedTraversal, unless you are very careful not to invalidate the predicate on the target!
backwards :: (Profunctor p, Profunctor q) => Optical p q (Backwards f) s t a b -> Optical p q f s t a b Source #
This allows you to traverse the elements of a pretty much any LensLike construction in the opposite order.
This will preserve indexes on Indexed types and will give you the elements of a (finite) Fold or Traversal in the opposite order.
This has no practical impact on a Getter, Setter, Lens or Iso.
NB: To write back through an Iso, you want to use from.
Similarly, to write back through an Prism, you want to use re.
replicated :: Int -> Fold a a Source #
A Fold that replicates its input n times.
replicaten ≡toListOf(replicatedn)
>>>5^..replicated 20[5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5]
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 Source #
Obtain a Fold by taking elements from another Fold, Lens, Iso, Getter or Traversal while a predicate holds.
takeWhilep ≡toListOf(takingWhilepfolded)
>>>timingOut $ toListOf (takingWhile (<=3) folded) [1..][1,2,3]
takingWhile:: (a ->Bool) ->Folds a ->Folds atakingWhile:: (a ->Bool) ->Getters a ->Folds atakingWhile:: (a ->Bool) ->Traversal's a ->Folds a -- * See note belowtakingWhile:: (a ->Bool) ->Lens's a ->Folds a -- * See note belowtakingWhile:: (a ->Bool) ->Prism's a ->Folds a -- * See note belowtakingWhile:: (a ->Bool) ->Iso's a ->Folds a -- * See note belowtakingWhile:: (a ->Bool) ->IndexedTraversal'i s a ->IndexedFoldi s a -- * See note belowtakingWhile:: (a ->Bool) ->IndexedLens'i s a ->IndexedFoldi s a -- * See note belowtakingWhile:: (a ->Bool) ->IndexedFoldi s a ->IndexedFoldi s atakingWhile:: (a ->Bool) ->IndexedGetteri s a ->IndexedFoldi s a
Note: When applied to a Traversal, takingWhile yields something that can be used as if it were a Traversal, but
which is not a Traversal per the laws, unless you are careful to ensure that you do not invalidate the predicate when
writing back through it.
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 Source #
Obtain a Fold by dropping elements from another Fold, Lens, Iso, Getter or Traversal while a predicate holds.
dropWhilep ≡toListOf(droppingWhilepfolded)
>>>toListOf (droppingWhile (<=3) folded) [1..6][4,5,6]
>>>toListOf (droppingWhile (<=3) folded) [1,6,1][6,1]
droppingWhile:: (a ->Bool) ->Folds a ->Folds adroppingWhile:: (a ->Bool) ->Getters a ->Folds adroppingWhile:: (a ->Bool) ->Traversal's a ->Folds a -- see notesdroppingWhile:: (a ->Bool) ->Lens's a ->Folds a -- see notesdroppingWhile:: (a ->Bool) ->Prism's a ->Folds a -- see notesdroppingWhile:: (a ->Bool) ->Iso's a ->Folds a -- see notes
droppingWhile:: (a ->Bool) ->IndexPreservingTraversal's a ->IndexPreservingFolds a -- see notesdroppingWhile:: (a ->Bool) ->IndexPreservingLens's a ->IndexPreservingFolds a -- see notesdroppingWhile:: (a ->Bool) ->IndexPreservingGetters a ->IndexPreservingFolds adroppingWhile:: (a ->Bool) ->IndexPreservingFolds a ->IndexPreservingFolds a
droppingWhile:: (a ->Bool) ->IndexedTraversal'i s a ->IndexedFoldi s a -- see notesdroppingWhile:: (a ->Bool) ->IndexedLens'i s a ->IndexedFoldi s a -- see notesdroppingWhile:: (a ->Bool) ->IndexedGetteri s a ->IndexedFoldi s adroppingWhile:: (a ->Bool) ->IndexedFoldi s a ->IndexedFoldi s a
Note: Many uses of this combinator will yield something that meets the types, but not the laws of a valid
Traversal or IndexedTraversal. The Traversal and IndexedTraversal laws are only satisfied if the
new values you assign to the first target also does not pass the predicate! Otherwise subsequent traversals
will visit fewer elements and Traversal fusion is not sound.
So for any traversal t and predicate p, may not be lawful, but
droppingWhile p t( is. For example:dropping 1 . droppingWhile p) t
>>>let l :: Traversal' [Int] Int; l = droppingWhile (<= 1) traverse>>>let l' :: Traversal' [Int] Int; l' = dropping 1 l
l is not a lawful setter because :over l f .
over l g ≢ over l (f . g)
>>>[1,2,3] & l .~ 0 & l .~ 4[1,0,0]>>>[1,2,3] & l .~ 4[1,4,4]
l' on the other hand behaves lawfully:
>>>[1,2,3] & l' .~ 0 & l' .~ 4[1,2,4]>>>[1,2,3] & l' .~ 4[1,2,4]
worded :: Applicative f => IndexedLensLike' Int f String String Source #
A Fold over the individual words of a String.
worded::FoldStringStringworded::Traversal'StringString
worded::IndexedFoldIntStringStringworded::IndexedTraversal'IntStringString
Note: This function type-checks as a Traversal but it doesn't satisfy the laws. It's only valid to use it
when you don't insert any whitespace characters while traversing, and if your original String contains only
isolated space characters (and no other characters that count as space, such as non-breaking spaces).
lined :: Applicative f => IndexedLensLike' Int f String String Source #
A Fold over the individual lines of a String.
lined::FoldStringStringlined::Traversal'StringString
lined::IndexedFoldIntStringStringlined::IndexedTraversal'IntStringString
Note: This function type-checks as a Traversal but it doesn't satisfy the laws. It's only valid to use it
when you don't insert any newline characters while traversing, and if your original String contains only
isolated newline characters.
Folding
foldMapOf :: Getting r s a -> (a -> r) -> s -> r Source #
Map each part of a structure viewed through a Lens, Getter,
Fold or Traversal to a monoid and combine the results.
>>>foldMapOf (folded . both . _Just) Sum [(Just 21, Just 21)]Sum {getSum = 42}
foldMap=foldMapOffolded
foldMapOf≡viewsifoldMapOfl =foldMapOfl.Indexed
foldMapOf::Getters a -> (a -> r) -> s -> rfoldMapOf::Monoidr =>Folds a -> (a -> r) -> s -> rfoldMapOf::Semigroupr =>Fold1s a -> (a -> r) -> s -> rfoldMapOf::Lens's a -> (a -> r) -> s -> rfoldMapOf::Iso's a -> (a -> r) -> s -> rfoldMapOf::Monoidr =>Traversal's a -> (a -> r) -> s -> rfoldMapOf::Semigroupr =>Traversal1's a -> (a -> r) -> s -> rfoldMapOf::Monoidr =>Prism's a -> (a -> r) -> s -> r
foldMapOf::Gettingr s a -> (a -> r) -> s -> r
foldOf :: Getting a s a -> s -> a Source #
Combine the elements of a structure viewed through a Lens, Getter,
Fold or Traversal using a monoid.
>>>foldOf (folded.folded) [[Sum 1,Sum 4],[Sum 8, Sum 8],[Sum 21]]Sum {getSum = 42}
fold=foldOffolded
foldOf≡view
foldOf::Getters m -> s -> mfoldOf::Monoidm =>Folds m -> s -> mfoldOf::Lens's m -> s -> mfoldOf::Iso's m -> s -> mfoldOf::Monoidm =>Traversal's m -> s -> mfoldOf::Monoidm =>Prism's m -> s -> m
foldrOf :: Getting (Endo r) s a -> (a -> r -> r) -> r -> s -> r Source #
Right-associative fold of parts of a structure that are viewed through a Lens, Getter, Fold or Traversal.
foldr≡foldrOffolded
foldrOf::Getters a -> (a -> r -> r) -> r -> s -> rfoldrOf::Folds a -> (a -> r -> r) -> r -> s -> rfoldrOf::Lens's a -> (a -> r -> r) -> r -> s -> rfoldrOf::Iso's a -> (a -> r -> r) -> r -> s -> rfoldrOf::Traversal's a -> (a -> r -> r) -> r -> s -> rfoldrOf::Prism's a -> (a -> r -> r) -> r -> s -> r
ifoldrOfl ≡foldrOfl.Indexed
foldrOf::Getting(Endor) s a -> (a -> r -> r) -> r -> s -> r
foldlOf :: Getting (Dual (Endo r)) s a -> (r -> a -> r) -> r -> s -> r Source #
Left-associative fold of the parts of a structure that are viewed through a Lens, Getter, Fold or Traversal.
foldl≡foldlOffolded
foldlOf::Getters a -> (r -> a -> r) -> r -> s -> rfoldlOf::Folds a -> (r -> a -> r) -> r -> s -> rfoldlOf::Lens's a -> (r -> a -> r) -> r -> s -> rfoldlOf::Iso's a -> (r -> a -> r) -> r -> s -> rfoldlOf::Traversal's a -> (r -> a -> r) -> r -> s -> rfoldlOf::Prism's a -> (r -> a -> r) -> r -> s -> r
toNonEmptyOf :: Getting (NonEmptyDList a) s a -> s -> NonEmpty a Source #
Extract a NonEmpty of the targets of Fold1.
>>>toNonEmptyOf both1 ("hello", "world")"hello" :| ["world"]
toNonEmptyOf::Getters a -> s -> NonEmpty atoNonEmptyOf::Fold1s a -> s -> NonEmpty atoNonEmptyOf::Lens's a -> s -> NonEmpty atoNonEmptyOf::Iso's a -> s -> NonEmpty atoNonEmptyOf::Traversal1's a -> s -> NonEmpty atoNonEmptyOf::Prism's a -> s -> NonEmpty a
anyOf :: Getting Any s a -> (a -> Bool) -> s -> Bool Source #
Returns True if any target of a Fold satisfies a predicate.
>>>anyOf both (=='x') ('x','y')True>>>import Data.Data.Lens>>>anyOf biplate (== "world") (((),2::Int),"hello",("world",11::Int))True
any≡anyOffolded
ianyOfl ≡anyOfl.Indexed
anyOf::Getters a -> (a ->Bool) -> s ->BoolanyOf::Folds a -> (a ->Bool) -> s ->BoolanyOf::Lens's a -> (a ->Bool) -> s ->BoolanyOf::Iso's a -> (a ->Bool) -> s ->BoolanyOf::Traversal's a -> (a ->Bool) -> s ->BoolanyOf::Prism's a -> (a ->Bool) -> s ->Bool
allOf :: Getting All s a -> (a -> Bool) -> s -> Bool Source #
Returns True if every target of a Fold satisfies a predicate.
>>>allOf both (>=3) (4,5)True>>>allOf folded (>=2) [1..10]False
all≡allOffolded
iallOfl =allOfl.Indexed
allOf::Getters a -> (a ->Bool) -> s ->BoolallOf::Folds a -> (a ->Bool) -> s ->BoolallOf::Lens's a -> (a ->Bool) -> s ->BoolallOf::Iso's a -> (a ->Bool) -> s ->BoolallOf::Traversal's a -> (a ->Bool) -> s ->BoolallOf::Prism's a -> (a ->Bool) -> s ->Bool
noneOf :: Getting Any s a -> (a -> Bool) -> s -> Bool Source #
Returns True only if no targets of a Fold satisfy a predicate.
>>>noneOf each (is _Nothing) (Just 3, Just 4, Just 5)True>>>noneOf (folded.folded) (<10) [[13,99,20],[3,71,42]]False
inoneOfl =noneOfl.Indexed
noneOf::Getters a -> (a ->Bool) -> s ->BoolnoneOf::Folds a -> (a ->Bool) -> s ->BoolnoneOf::Lens's a -> (a ->Bool) -> s ->BoolnoneOf::Iso's a -> (a ->Bool) -> s ->BoolnoneOf::Traversal's a -> (a ->Bool) -> s ->BoolnoneOf::Prism's a -> (a ->Bool) -> s ->Bool
andOf :: Getting All s Bool -> s -> Bool Source #
Returns True if every target of a Fold is True.
>>>andOf both (True,False)False>>>andOf both (True,True)True
and≡andOffolded
andOf::GettersBool-> s ->BoolandOf::FoldsBool-> s ->BoolandOf::Lens'sBool-> s ->BoolandOf::Iso'sBool-> s ->BoolandOf::Traversal'sBool-> s ->BoolandOf::Prism'sBool-> s ->Bool
orOf :: Getting Any s Bool -> s -> Bool Source #
Returns True if any target of a Fold is True.
>>>orOf both (True,False)True>>>orOf both (False,False)False
or≡orOffolded
orOf::GettersBool-> s ->BoolorOf::FoldsBool-> s ->BoolorOf::Lens'sBool-> s ->BoolorOf::Iso'sBool-> s ->BoolorOf::Traversal'sBool-> s ->BoolorOf::Prism'sBool-> s ->Bool
productOf :: Num a => Getting (Endo (Endo a)) s a -> s -> a Source #
Calculate the Product of every number targeted by a Fold.
>>>productOf both (4,5)20>>>productOf folded [1,2,3,4,5]120
product≡productOffolded
This operation may be more strict than you would expect. If you
want a lazier version use ala Product . foldMapOf
productOf::Numa =>Getters a -> s -> aproductOf::Numa =>Folds a -> s -> aproductOf::Numa =>Lens's a -> s -> aproductOf::Numa =>Iso's a -> s -> aproductOf::Numa =>Traversal's a -> s -> aproductOf::Numa =>Prism's a -> s -> a
sumOf :: Num a => Getting (Endo (Endo a)) s a -> s -> a Source #
Calculate the Sum of every number targeted by a Fold.
>>>sumOf both (5,6)11>>>sumOf folded [1,2,3,4]10>>>sumOf (folded.both) [(1,2),(3,4)]10>>>import Data.Data.Lens>>>sumOf biplate [(1::Int,[]),(2,[(3::Int,4::Int)])] :: Int10
sum≡sumOffolded
This operation may be more strict than you would expect. If you
want a lazier version use ala Sum . foldMapOf
sumOf_1::Numa => (a, b) -> asumOf(folded._1) :: (Foldablef,Numa) => f (a, b) -> a
sumOf::Numa =>Getters a -> s -> asumOf::Numa =>Folds a -> s -> asumOf::Numa =>Lens's a -> s -> asumOf::Numa =>Iso's a -> s -> asumOf::Numa =>Traversal's a -> s -> asumOf::Numa =>Prism's a -> s -> a
traverseOf_ :: Functor f => Getting (Traversed r f) s a -> (a -> f r) -> s -> f () Source #
Traverse over all of the targets of a Fold (or Getter), computing an Applicative (or Functor)-based answer,
but unlike traverseOf do not construct a new structure. traverseOf_ generalizes
traverse_ to work over any Fold.
When passed a Getter, traverseOf_ can work over any Functor, but when passed a Fold, traverseOf_ requires
an Applicative.
>>>traverseOf_ both putStrLn ("hello","world")hello world
traverse_≡traverseOf_folded
traverseOf__2::Functorf => (c -> f r) -> (d, c) -> f ()traverseOf__Left::Applicativef => (a -> f b) ->Eithera c -> f ()
itraverseOf_l ≡traverseOf_l.Indexed
The rather specific signature of traverseOf_ allows it to be used as if the signature was any of:
traverseOf_::Functorf =>Getters a -> (a -> f r) -> s -> f ()traverseOf_::Applicativef =>Folds a -> (a -> f r) -> s -> f ()traverseOf_::Functorf =>Lens's a -> (a -> f r) -> s -> f ()traverseOf_::Functorf =>Iso's a -> (a -> f r) -> s -> f ()traverseOf_::Applicativef =>Traversal's a -> (a -> f r) -> s -> f ()traverseOf_::Applicativef =>Prism's a -> (a -> f r) -> s -> f ()
forOf_ :: Functor f => Getting (Traversed r f) s a -> s -> (a -> f r) -> f () Source #
Traverse over all of the targets of a Fold (or Getter), computing an Applicative (or Functor)-based answer,
but unlike forOf do not construct a new structure. forOf_ generalizes
for_ to work over any Fold.
When passed a Getter, forOf_ can work over any Functor, but when passed a Fold, forOf_ requires
an Applicative.
for_≡forOf_folded
>>>forOf_ both ("hello","world") putStrLnhello world
The rather specific signature of forOf_ allows it to be used as if the signature was any of:
iforOf_l s ≡forOf_l s.Indexed
forOf_::Functorf =>Getters a -> s -> (a -> f r) -> f ()forOf_::Applicativef =>Folds a -> s -> (a -> f r) -> f ()forOf_::Functorf =>Lens's a -> s -> (a -> f r) -> f ()forOf_::Functorf =>Iso's a -> s -> (a -> f r) -> f ()forOf_::Applicativef =>Traversal's a -> s -> (a -> f r) -> f ()forOf_::Applicativef =>Prism's a -> s -> (a -> f r) -> f ()
sequenceAOf_ :: Functor f => Getting (Traversed a f) s (f a) -> s -> f () Source #
Evaluate each action in observed by a Fold on a structure from left to right, ignoring the results.
sequenceA_≡sequenceAOf_folded
>>>sequenceAOf_ both (putStrLn "hello",putStrLn "world")hello world
sequenceAOf_::Functorf =>Getters (f a) -> s -> f ()sequenceAOf_::Applicativef =>Folds (f a) -> s -> f ()sequenceAOf_::Functorf =>Lens's (f a) -> s -> f ()sequenceAOf_::Functorf =>Iso's (f a) -> s -> f ()sequenceAOf_::Applicativef =>Traversal's (f a) -> s -> f ()sequenceAOf_::Applicativef =>Prism's (f a) -> s -> f ()
traverse1Of_ :: Functor f => Getting (TraversedF r f) s a -> (a -> f r) -> s -> f () Source #
Traverse over all of the targets of a Fold1, computing an Apply based answer.
As long as you have Applicative or Functor effect you are better using traverseOf_.
The traverse1Of_ is useful only when you have genuine Apply effect.
>>>traverse1Of_ both1 (\ks -> Map.fromList [ (k, ()) | k <- ks ]) ("abc", "bcd")fromList [('b',()),('c',())]
traverse1Of_::Applyf =>Fold1s a -> (a -> f r) -> s -> f ()
Since: 4.16
for1Of_ :: Functor f => Getting (TraversedF r f) s a -> s -> (a -> f r) -> f () Source #
See forOf_ and traverse1Of_.
>>>for1Of_ both1 ("abc", "bcd") (\ks -> Map.fromList [ (k, ()) | k <- ks ])fromList [('b',()),('c',())]
for1Of_::Applyf =>Fold1s a -> s -> (a -> f r) -> f ()
Since: 4.16
sequence1Of_ :: Functor f => Getting (TraversedF a f) s (f a) -> s -> f () Source #
mapMOf_ :: Monad m => Getting (Sequenced r m) s a -> (a -> m r) -> s -> m () Source #
Map each target of a Fold on a structure to a monadic action, evaluate these actions from left to right, and ignore the results.
>>>mapMOf_ both putStrLn ("hello","world")hello world
mapM_≡mapMOf_folded
mapMOf_::Monadm =>Getters a -> (a -> m r) -> s -> m ()mapMOf_::Monadm =>Folds a -> (a -> m r) -> s -> m ()mapMOf_::Monadm =>Lens's a -> (a -> m r) -> s -> m ()mapMOf_::Monadm =>Iso's a -> (a -> m r) -> s -> m ()mapMOf_::Monadm =>Traversal's a -> (a -> m r) -> s -> m ()mapMOf_::Monadm =>Prism's a -> (a -> m r) -> s -> m ()
forMOf_ :: Monad m => Getting (Sequenced r m) s a -> s -> (a -> m r) -> m () Source #
forMOf_ is mapMOf_ with two of its arguments flipped.
>>>forMOf_ both ("hello","world") putStrLnhello world
forM_≡forMOf_folded
forMOf_::Monadm =>Getters a -> s -> (a -> m r) -> m ()forMOf_::Monadm =>Folds a -> s -> (a -> m r) -> m ()forMOf_::Monadm =>Lens's a -> s -> (a -> m r) -> m ()forMOf_::Monadm =>Iso's a -> s -> (a -> m r) -> m ()forMOf_::Monadm =>Traversal's a -> s -> (a -> m r) -> m ()forMOf_::Monadm =>Prism's a -> s -> (a -> m r) -> m ()
sequenceOf_ :: Monad m => Getting (Sequenced a m) s (m a) -> s -> m () Source #
Evaluate each monadic action referenced by a Fold on the structure from left to right, and ignore the results.
>>>sequenceOf_ both (putStrLn "hello",putStrLn "world")hello world
sequence_≡sequenceOf_folded
sequenceOf_::Monadm =>Getters (m a) -> s -> m ()sequenceOf_::Monadm =>Folds (m a) -> s -> m ()sequenceOf_::Monadm =>Lens's (m a) -> s -> m ()sequenceOf_::Monadm =>Iso's (m a) -> s -> m ()sequenceOf_::Monadm =>Traversal's (m a) -> s -> m ()sequenceOf_::Monadm =>Prism's (m a) -> s -> m ()
asumOf :: Alternative f => Getting (Endo (f a)) s (f a) -> s -> f a Source #
The sum of a collection of actions, generalizing concatOf.
>>>asumOf both ("hello","world")"helloworld"
>>>asumOf each (Nothing, Just "hello", Nothing)Just "hello"
asum≡asumOffolded
asumOf::Alternativef =>Getters (f a) -> s -> f aasumOf::Alternativef =>Folds (f a) -> s -> f aasumOf::Alternativef =>Lens's (f a) -> s -> f aasumOf::Alternativef =>Iso's (f a) -> s -> f aasumOf::Alternativef =>Traversal's (f a) -> s -> f aasumOf::Alternativef =>Prism's (f a) -> s -> f a
msumOf :: MonadPlus m => Getting (Endo (m a)) s (m a) -> s -> m a Source #
The sum of a collection of actions, generalizing concatOf.
>>>msumOf both ("hello","world")"helloworld"
>>>msumOf each (Nothing, Just "hello", Nothing)Just "hello"
msum≡msumOffolded
msumOf::MonadPlusm =>Getters (m a) -> s -> m amsumOf::MonadPlusm =>Folds (m a) -> s -> m amsumOf::MonadPlusm =>Lens's (m a) -> s -> m amsumOf::MonadPlusm =>Iso's (m a) -> s -> m amsumOf::MonadPlusm =>Traversal's (m a) -> s -> m amsumOf::MonadPlusm =>Prism's (m a) -> s -> m a
concatMapOf :: Getting [r] s a -> (a -> [r]) -> s -> [r] Source #
Map a function over all the targets of a Fold of a container and concatenate the resulting lists.
>>>concatMapOf both (\x -> [x, x + 1]) (1,3)[1,2,3,4]
concatMap≡concatMapOffolded
concatMapOf::Getters a -> (a -> [r]) -> s -> [r]concatMapOf::Folds a -> (a -> [r]) -> s -> [r]concatMapOf::Lens's a -> (a -> [r]) -> s -> [r]concatMapOf::Iso's a -> (a -> [r]) -> s -> [r]concatMapOf::Traversal's a -> (a -> [r]) -> s -> [r]
concatOf :: Getting [r] s [r] -> s -> [r] Source #
Concatenate all of the lists targeted by a Fold into a longer list.
>>>concatOf both ("pan","ama")"panama"
concat≡concatOffoldedconcatOf≡view
concatOf::Getters [r] -> s -> [r]concatOf::Folds [r] -> s -> [r]concatOf::Iso's [r] -> s -> [r]concatOf::Lens's [r] -> s -> [r]concatOf::Traversal's [r] -> s -> [r]
elemOf :: Eq a => Getting Any s a -> a -> s -> Bool Source #
Does the element occur anywhere within a given Fold of the structure?
>>>elemOf both "hello" ("hello","world")True
elem≡elemOffolded
elemOf::Eqa =>Getters a -> a -> s ->BoolelemOf::Eqa =>Folds a -> a -> s ->BoolelemOf::Eqa =>Lens's a -> a -> s ->BoolelemOf::Eqa =>Iso's a -> a -> s ->BoolelemOf::Eqa =>Traversal's a -> a -> s ->BoolelemOf::Eqa =>Prism's a -> a -> s ->Bool
notElemOf :: Eq a => Getting All s a -> a -> s -> Bool Source #
Does the element not occur anywhere within a given Fold of the structure?
>>>notElemOf each 'd' ('a','b','c')True
>>>notElemOf each 'a' ('a','b','c')False
notElem≡notElemOffolded
notElemOf::Eqa =>Getters a -> a -> s ->BoolnotElemOf::Eqa =>Folds a -> a -> s ->BoolnotElemOf::Eqa =>Iso's a -> a -> s ->BoolnotElemOf::Eqa =>Lens's a -> a -> s ->BoolnotElemOf::Eqa =>Traversal's a -> a -> s ->BoolnotElemOf::Eqa =>Prism's a -> a -> s ->Bool
lengthOf :: Getting (Endo (Endo Int)) s a -> s -> Int Source #
Calculate the number of targets there are for a Fold in a given container.
Note: This can be rather inefficient for large containers and just like length,
this will not terminate for infinite folds.
length≡lengthOffolded
>>>lengthOf _1 ("hello",())1
>>>lengthOf traverse [1..10]10
>>>lengthOf (traverse.traverse) [[1,2],[3,4],[5,6]]6
lengthOf(folded.folded) :: (Foldablef,Foldableg) => f (g a) ->Int
lengthOf::Getters a -> s ->IntlengthOf::Folds a -> s ->IntlengthOf::Lens's a -> s ->IntlengthOf::Iso's a -> s ->IntlengthOf::Traversal's a -> s ->Int
nullOf :: Getting All s a -> s -> Bool Source #
Returns True if this Fold or Traversal has no targets in the given container.
Note: nullOf on a valid Iso, Lens or Getter should always return False.
null≡nullOffolded
This may be rather inefficient compared to the null check of many containers.
>>>nullOf _1 (1,2)False
>>>nullOf ignored ()True
>>>nullOf traverse []True
>>>nullOf (element 20) [1..10]True
nullOf(folded._1.folded) :: (Foldablef,Foldableg) => f (g a, b) ->Bool
nullOf::Getters a -> s ->BoolnullOf::Folds a -> s ->BoolnullOf::Iso's a -> s ->BoolnullOf::Lens's a -> s ->BoolnullOf::Traversal's a -> s ->Bool
notNullOf :: Getting Any s a -> s -> Bool Source #
Returns True if this Fold or Traversal has any targets in the given container.
A more "conversational" alias for this combinator is has.
Note: notNullOf on a valid Iso, Lens or Getter should always return True.
not.null≡notNullOffolded
This may be rather inefficient compared to the check of many containers.not . null
>>>notNullOf _1 (1,2)True
>>>notNullOf traverse [1..10]True
>>>notNullOf folded []False
>>>notNullOf (element 20) [1..10]False
notNullOf(folded._1.folded) :: (Foldablef,Foldableg) => f (g a, b) ->Bool
notNullOf::Getters a -> s ->BoolnotNullOf::Folds a -> s ->BoolnotNullOf::Iso's a -> s ->BoolnotNullOf::Lens's a -> s ->BoolnotNullOf::Traversal's a -> s ->Bool
firstOf :: Getting (Leftmost a) s a -> s -> Maybe a Source #
Retrieve the First entry of a Fold or Traversal or retrieve Just the result
from a Getter or Lens.
The answer is computed in a manner that leaks space less than or preview^?'
and gives you back access to the outermost Just constructor more quickly, but does so
in a way that builds an intermediate structure, and thus may have worse
constant factors. This also means that it can not be used in any MonadReader,
but must instead have s passed as its last argument, unlike preview.
Note: this could been named headOf.
>>>firstOf traverse [1..10]Just 1
>>>firstOf both (1,2)Just 1
>>>firstOf ignored ()Nothing
firstOf::Getters a -> s ->MaybeafirstOf::Folds a -> s ->MaybeafirstOf::Lens's a -> s ->MaybeafirstOf::Iso's a -> s ->MaybeafirstOf::Traversal's a -> s ->Maybea
first1Of :: Getting (First a) s a -> s -> a Source #
Retrieve the First entry of a Fold1 or Traversal1 or the result from a Getter or Lens.
>>>first1Of traverse1 (1 :| [2..10])1
>>>first1Of both1 (1,2)1
Note: this is different from ^..
>>>first1Of traverse1 ([1,2] :| [[3,4],[5,6]])[1,2]
>>>([1,2] :| [[3,4],[5,6]]) ^. traverse1[1,2,3,4,5,6]
first1Of::Getters a -> s -> afirst1Of::Fold1s a -> s -> afirst1Of::Lens's a -> s -> afirst1Of::Iso's a -> s -> afirst1Of::Traversal1's a -> s -> a
lastOf :: Getting (Rightmost a) s a -> s -> Maybe a Source #
Retrieve the Last entry of a Fold or Traversal or retrieve Just the result
from a Getter or Lens.
The answer is computed in a manner that leaks space less than
and gives you back access to the outermost ala Last . foldMapOfJust constructor more quickly, but may have worse
constant factors.
>>>lastOf traverse [1..10]Just 10
>>>lastOf both (1,2)Just 2
>>>lastOf ignored ()Nothing
lastOf::Getters a -> s ->MaybealastOf::Folds a -> s ->MaybealastOf::Lens's a -> s ->MaybealastOf::Iso's a -> s ->MaybealastOf::Traversal's a -> s ->Maybea
last1Of :: Getting (Last a) s a -> s -> a Source #
Retrieve the Last entry of a Fold1 or Traversal1 or retrieve the result
from a Getter or Lens.o
>>>last1Of traverse1 (1 :| [2..10])10
>>>last1Of both1 (1,2)2
last1Of::Getters a -> s ->Maybealast1Of::Fold1s a -> s ->Maybealast1Of::Lens's a -> s ->Maybealast1Of::Iso's a -> s ->Maybealast1Of::Traversal1's a -> s ->Maybea
maximumOf :: Ord a => Getting (Endo (Endo (Maybe a))) s a -> s -> Maybe a Source #
Obtain the maximum element (if any) targeted by a Fold or Traversal safely.
Note: maximumOf on a valid Iso, Lens or Getter will always return Just a value.
>>>maximumOf traverse [1..10]Just 10
>>>maximumOf traverse []Nothing
>>>maximumOf (folded.filtered even) [1,4,3,6,7,9,2]Just 6
maximum≡fromMaybe(error"empty").maximumOffolded
In the interest of efficiency, This operation has semantics more strict than strictly necessary.
has lazier semantics but could leak memory.rmap getMax (foldMapOf l Max)
maximumOf::Orda =>Getters a -> s ->MaybeamaximumOf::Orda =>Folds a -> s ->MaybeamaximumOf::Orda =>Iso's a -> s ->MaybeamaximumOf::Orda =>Lens's a -> s ->MaybeamaximumOf::Orda =>Traversal's a -> s ->Maybea
maximum1Of :: Ord a => Getting (Max a) s a -> s -> a Source #
Obtain the maximum element targeted by a Fold1 or Traversal1.
>>>maximum1Of traverse1 (1 :| [2..10])10
maximum1Of::Orda =>Getters a -> s -> amaximum1Of::Orda =>Fold1s a -> s -> amaximum1Of::Orda =>Iso's a -> s -> amaximum1Of::Orda =>Lens's a -> s -> amaximum1Of::Orda =>Traversal1's a -> s -> a
minimumOf :: Ord a => Getting (Endo (Endo (Maybe a))) s a -> s -> Maybe a Source #
Obtain the minimum element (if any) targeted by a Fold or Traversal safely.
Note: minimumOf on a valid Iso, Lens or Getter will always return Just a value.
>>>minimumOf traverse [1..10]Just 1
>>>minimumOf traverse []Nothing
>>>minimumOf (folded.filtered even) [1,4,3,6,7,9,2]Just 2
minimum≡fromMaybe(error"empty").minimumOffolded
In the interest of efficiency, This operation has semantics more strict than strictly necessary.
has lazier semantics but could leak memory.rmap getMin (foldMapOf l Min)
minimumOf::Orda =>Getters a -> s ->MaybeaminimumOf::Orda =>Folds a -> s ->MaybeaminimumOf::Orda =>Iso's a -> s ->MaybeaminimumOf::Orda =>Lens's a -> s ->MaybeaminimumOf::Orda =>Traversal's a -> s ->Maybea
minimum1Of :: Ord a => Getting (Min a) s a -> s -> a Source #
Obtain the minimum element targeted by a Fold1 or Traversal1.
>>>minimum1Of traverse1 (1 :| [2..10])1
minimum1Of::Orda =>Getters a -> s -> aminimum1Of::Orda =>Fold1s a -> s -> aminimum1Of::Orda =>Iso's a -> s -> aminimum1Of::Orda =>Lens's a -> s -> aminimum1Of::Orda =>Traversal1's a -> s -> a
maximumByOf :: Getting (Endo (Endo (Maybe a))) s a -> (a -> a -> Ordering) -> s -> Maybe a Source #
Obtain the maximum element (if any) targeted by a Fold, Traversal, Lens, Iso,
or Getter according to a user supplied Ordering.
>>>maximumByOf traverse (compare `on` length) ["mustard","relish","ham"]Just "mustard"
In the interest of efficiency, This operation has semantics more strict than strictly necessary.
maximumBycmp ≡fromMaybe(error"empty").maximumByOffoldedcmp
maximumByOf::Getters a -> (a -> a ->Ordering) -> s ->MaybeamaximumByOf::Folds a -> (a -> a ->Ordering) -> s ->MaybeamaximumByOf::Iso's a -> (a -> a ->Ordering) -> s ->MaybeamaximumByOf::Lens's a -> (a -> a ->Ordering) -> s ->MaybeamaximumByOf::Traversal's a -> (a -> a ->Ordering) -> s ->Maybea
minimumByOf :: Getting (Endo (Endo (Maybe a))) s a -> (a -> a -> Ordering) -> s -> Maybe a Source #
Obtain the minimum element (if any) targeted by a Fold, Traversal, Lens, Iso
or Getter according to a user supplied Ordering.
In the interest of efficiency, This operation has semantics more strict than strictly necessary.
>>>minimumByOf traverse (compare `on` length) ["mustard","relish","ham"]Just "ham"
minimumBycmp ≡fromMaybe(error"empty").minimumByOffoldedcmp
minimumByOf::Getters a -> (a -> a ->Ordering) -> s ->MaybeaminimumByOf::Folds a -> (a -> a ->Ordering) -> s ->MaybeaminimumByOf::Iso's a -> (a -> a ->Ordering) -> s ->MaybeaminimumByOf::Lens's a -> (a -> a ->Ordering) -> s ->MaybeaminimumByOf::Traversal's a -> (a -> a ->Ordering) -> s ->Maybea
findOf :: Getting (Endo (Maybe a)) s a -> (a -> Bool) -> s -> Maybe a Source #
The findOf function takes a Lens (or Getter, Iso, Fold, or Traversal),
a predicate and a structure and returns the leftmost element of the structure
matching the predicate, or Nothing if there is no such element.
>>>findOf each even (1,3,4,6)Just 4
>>>findOf folded even [1,3,5,7]Nothing
findOf::Getters a -> (a ->Bool) -> s ->MaybeafindOf::Folds a -> (a ->Bool) -> s ->MaybeafindOf::Iso's a -> (a ->Bool) -> s ->MaybeafindOf::Lens's a -> (a ->Bool) -> s ->MaybeafindOf::Traversal's a -> (a ->Bool) -> s ->Maybea
find≡findOffoldedifindOfl ≡findOfl.Indexed
A simpler version that didn't permit indexing, would be:
findOf::Getting(Endo(Maybea)) s a -> (a ->Bool) -> s ->MaybeafindOfl p =foldrOfl (a y -> if p a thenJusta else y)Nothing
findMOf :: Monad m => Getting (Endo (m (Maybe a))) s a -> (a -> m Bool) -> s -> m (Maybe a) Source #
The findMOf function takes a Lens (or Getter, Iso, Fold, or Traversal),
a monadic predicate and a structure and returns in the monad the leftmost element of the structure
matching the predicate, or Nothing if there is no such element.
>>>findMOf each ( \x -> print ("Checking " ++ show x) >> return (even x)) (1,3,4,6)"Checking 1" "Checking 3" "Checking 4" Just 4
>>>findMOf each ( \x -> print ("Checking " ++ show x) >> return (even x)) (1,3,5,7)"Checking 1" "Checking 3" "Checking 5" "Checking 7" Nothing
findMOf:: (Monadm,Getters a) -> (a -> mBool) -> s -> m (Maybea)findMOf:: (Monadm,Folds a) -> (a -> mBool) -> s -> m (Maybea)findMOf:: (Monadm,Iso's a) -> (a -> mBool) -> s -> m (Maybea)findMOf:: (Monadm,Lens's a) -> (a -> mBool) -> s -> m (Maybea)findMOf:: (Monadm,Traversal's a) -> (a -> mBool) -> s -> m (Maybea)
findMOffolded:: (Monad m, Foldable f) => (a -> m Bool) -> f a -> m (Maybe a)ifindMOfl ≡findMOfl.Indexed
A simpler version that didn't permit indexing, would be:
findMOf:: Monad m =>Getting(Endo(m (Maybea))) s a -> (a -> mBool) -> s -> m (Maybea)findMOfl p =foldrOfl (a y -> p a >>= x -> if x then return (Justa) else y) $ returnNothing
foldrOf' :: Getting (Dual (Endo (Endo r))) s a -> (a -> r -> r) -> r -> s -> r Source #
Strictly fold right over the elements of a structure.
foldr'≡foldrOf'folded
foldrOf'::Getters a -> (a -> r -> r) -> r -> s -> rfoldrOf'::Folds a -> (a -> r -> r) -> r -> s -> rfoldrOf'::Iso's a -> (a -> r -> r) -> r -> s -> rfoldrOf'::Lens's a -> (a -> r -> r) -> r -> s -> rfoldrOf'::Traversal's a -> (a -> r -> r) -> r -> s -> r
foldlOf' :: Getting (Endo (Endo r)) s a -> (r -> a -> r) -> r -> s -> r Source #
Fold over the elements of a structure, associating to the left, but strictly.
foldl'≡foldlOf'folded
foldlOf'::Getters a -> (r -> a -> r) -> r -> s -> rfoldlOf'::Folds a -> (r -> a -> r) -> r -> s -> rfoldlOf'::Iso's a -> (r -> a -> r) -> r -> s -> rfoldlOf'::Lens's a -> (r -> a -> r) -> r -> s -> rfoldlOf'::Traversal's a -> (r -> a -> r) -> r -> s -> r
foldr1Of :: HasCallStack => Getting (Endo (Maybe a)) s a -> (a -> a -> a) -> s -> a Source #
A variant of foldrOf that has no base case and thus may only be applied
to lenses and structures such that the Lens views at least one element of
the structure.
>>>foldr1Of each (+) (1,2,3,4)10
foldr1Ofl f ≡foldr1f.toListOflfoldr1≡foldr1Offolded
foldr1Of::Getters a -> (a -> a -> a) -> s -> afoldr1Of::Folds a -> (a -> a -> a) -> s -> afoldr1Of::Iso's a -> (a -> a -> a) -> s -> afoldr1Of::Lens's a -> (a -> a -> a) -> s -> afoldr1Of::Traversal's a -> (a -> a -> a) -> s -> a
foldl1Of :: HasCallStack => Getting (Dual (Endo (Maybe a))) s a -> (a -> a -> a) -> s -> a Source #
A variant of foldlOf that has no base case and thus may only be applied to lenses and structures such
that the Lens views at least one element of the structure.
>>>foldl1Of each (+) (1,2,3,4)10
foldl1Ofl f ≡foldl1f.toListOflfoldl1≡foldl1Offolded
foldl1Of::Getters a -> (a -> a -> a) -> s -> afoldl1Of::Folds a -> (a -> a -> a) -> s -> afoldl1Of::Iso's a -> (a -> a -> a) -> s -> afoldl1Of::Lens's a -> (a -> a -> a) -> s -> afoldl1Of::Traversal's a -> (a -> a -> a) -> s -> a
foldr1Of' :: HasCallStack => Getting (Dual (Endo (Endo (Maybe a)))) s a -> (a -> a -> a) -> s -> a Source #
A variant of foldrOf' that has no base case and thus may only be applied
to folds and structures such that the fold views at least one element of the
structure.
foldr1Ofl f ≡foldr1f.toListOfl
foldr1Of'::Getters a -> (a -> a -> a) -> s -> afoldr1Of'::Folds a -> (a -> a -> a) -> s -> afoldr1Of'::Iso's a -> (a -> a -> a) -> s -> afoldr1Of'::Lens's a -> (a -> a -> a) -> s -> afoldr1Of'::Traversal's a -> (a -> a -> a) -> s -> a
foldl1Of' :: HasCallStack => Getting (Endo (Endo (Maybe a))) s a -> (a -> a -> a) -> s -> a Source #
A variant of foldlOf' that has no base case and thus may only be applied
to folds and structures such that the fold views at least one element of
the structure.
foldl1Of'l f ≡foldl1'f.toListOfl
foldl1Of'::Getters a -> (a -> a -> a) -> s -> afoldl1Of'::Folds a -> (a -> a -> a) -> s -> afoldl1Of'::Iso's a -> (a -> a -> a) -> s -> afoldl1Of'::Lens's a -> (a -> a -> a) -> s -> afoldl1Of'::Traversal's a -> (a -> a -> a) -> s -> a
foldrMOf :: Monad m => Getting (Dual (Endo (r -> m r))) s a -> (a -> r -> m r) -> r -> s -> m r Source #
Monadic fold over the elements of a structure, associating to the right, i.e. from right to left.
foldrM≡foldrMOffolded
foldrMOf::Monadm =>Getters a -> (a -> r -> m r) -> r -> s -> m rfoldrMOf::Monadm =>Folds a -> (a -> r -> m r) -> r -> s -> m rfoldrMOf::Monadm =>Iso's a -> (a -> r -> m r) -> r -> s -> m rfoldrMOf::Monadm =>Lens's a -> (a -> r -> m r) -> r -> s -> m rfoldrMOf::Monadm =>Traversal's a -> (a -> r -> m r) -> r -> s -> m r
foldlMOf :: Monad m => Getting (Endo (r -> m r)) s a -> (r -> a -> m r) -> r -> s -> m r Source #
Monadic fold over the elements of a structure, associating to the left, i.e. from left to right.
foldlM≡foldlMOffolded
foldlMOf::Monadm =>Getters a -> (r -> a -> m r) -> r -> s -> m rfoldlMOf::Monadm =>Folds a -> (r -> a -> m r) -> r -> s -> m rfoldlMOf::Monadm =>Iso's a -> (r -> a -> m r) -> r -> s -> m rfoldlMOf::Monadm =>Lens's a -> (r -> a -> m r) -> r -> s -> m rfoldlMOf::Monadm =>Traversal's a -> (r -> a -> m r) -> r -> s -> m r
lookupOf :: Eq k => Getting (Endo (Maybe v)) s (k, v) -> k -> s -> Maybe v Source #
The lookupOf function takes a Fold (or Getter, Traversal,
Lens, Iso, etc.), a key, and a structure containing key/value pairs.
It returns the first value corresponding to the given key. This function
generalizes lookup to work on an arbitrary Fold instead of lists.
>>>lookupOf folded 4 [(2, 'a'), (4, 'b'), (4, 'c')]Just 'b'
>>>lookupOf each 2 [(2, 'a'), (4, 'b'), (4, 'c')]Just 'a'
lookupOf::Eqk =>Folds (k,v) -> k -> s ->Maybev
Indexed Folds
(^@..) :: s -> IndexedGetting i (Endo [(i, a)]) s a -> [(i, a)] infixl 8 Source #
An infix version of itoListOf.
(^@?) :: s -> IndexedGetting i (Endo (Maybe (i, a))) s a -> Maybe (i, a) infixl 8 Source #
Perform a safe head (with index) of an IndexedFold or IndexedTraversal or retrieve Just the index and result
from an IndexedGetter or IndexedLens.
When using a IndexedTraversal as a partial IndexedLens, or an IndexedFold as a partial IndexedGetter this can be a convenient
way to extract the optional value.
(^@?) :: s ->IndexedGetteri s a ->Maybe(i, a) (^@?) :: s ->IndexedFoldi s a ->Maybe(i, a) (^@?) :: s ->IndexedLens'i s a ->Maybe(i, a) (^@?) :: s ->IndexedTraversal'i s a ->Maybe(i, a)
(^@?!) :: HasCallStack => s -> IndexedGetting i (Endo (i, a)) s a -> (i, a) infixl 8 Source #
Perform an *UNSAFE* head (with index) of an IndexedFold or IndexedTraversal assuming that it is there.
(^@?!) :: s ->IndexedGetteri s a -> (i, a) (^@?!) :: s ->IndexedFoldi s a -> (i, a) (^@?!) :: s ->IndexedLens'i s a -> (i, a) (^@?!) :: s ->IndexedTraversal'i s a -> (i, a)
Indexed Folding
ifoldMapOf :: IndexedGetting i m s a -> (i -> a -> m) -> s -> m Source #
Fold an IndexedFold or IndexedTraversal by mapping indices and values to an arbitrary Monoid with access
to the i.
When you don't need access to the index then foldMapOf is more flexible in what it accepts.
foldMapOfl ≡ifoldMapOfl.const
ifoldMapOf::IndexedGetteri s a -> (i -> a -> m) -> s -> mifoldMapOf::Monoidm =>IndexedFoldi s a -> (i -> a -> m) -> s -> mifoldMapOf::IndexedLens'i s a -> (i -> a -> m) -> s -> mifoldMapOf::Monoidm =>IndexedTraversal'i s a -> (i -> a -> m) -> s -> m
ifoldrOf :: IndexedGetting i (Endo r) s a -> (i -> a -> r -> r) -> r -> s -> r Source #
Right-associative fold of parts of a structure that are viewed through an IndexedFold or IndexedTraversal with
access to the i.
When you don't need access to the index then foldrOf is more flexible in what it accepts.
foldrOfl ≡ifoldrOfl.const
ifoldrOf::IndexedGetteri s a -> (i -> a -> r -> r) -> r -> s -> rifoldrOf::IndexedFoldi s a -> (i -> a -> r -> r) -> r -> s -> rifoldrOf::IndexedLens'i s a -> (i -> a -> r -> r) -> r -> s -> rifoldrOf::IndexedTraversal'i s a -> (i -> a -> r -> r) -> r -> s -> r
ifoldlOf :: IndexedGetting i (Dual (Endo r)) s a -> (i -> r -> a -> r) -> r -> s -> r Source #
Left-associative fold of the parts of a structure that are viewed through an IndexedFold or IndexedTraversal with
access to the i.
When you don't need access to the index then foldlOf is more flexible in what it accepts.
foldlOfl ≡ifoldlOfl.const
ifoldlOf::IndexedGetteri s a -> (i -> r -> a -> r) -> r -> s -> rifoldlOf::IndexedFoldi s a -> (i -> r -> a -> r) -> r -> s -> rifoldlOf::IndexedLens'i s a -> (i -> r -> a -> r) -> r -> s -> rifoldlOf::IndexedTraversal'i s a -> (i -> r -> a -> r) -> r -> s -> r
ianyOf :: IndexedGetting i Any s a -> (i -> a -> Bool) -> s -> Bool Source #
Return whether or not any element viewed through an IndexedFold or IndexedTraversal
satisfy a predicate, with access to the i.
When you don't need access to the index then anyOf is more flexible in what it accepts.
anyOfl ≡ianyOfl.const
ianyOf::IndexedGetteri s a -> (i -> a ->Bool) -> s ->BoolianyOf::IndexedFoldi s a -> (i -> a ->Bool) -> s ->BoolianyOf::IndexedLens'i s a -> (i -> a ->Bool) -> s ->BoolianyOf::IndexedTraversal'i s a -> (i -> a ->Bool) -> s ->Bool
iallOf :: IndexedGetting i All s a -> (i -> a -> Bool) -> s -> Bool Source #
Return whether or not all elements viewed through an IndexedFold or IndexedTraversal
satisfy a predicate, with access to the i.
When you don't need access to the index then allOf is more flexible in what it accepts.
allOfl ≡iallOfl.const
iallOf::IndexedGetteri s a -> (i -> a ->Bool) -> s ->BooliallOf::IndexedFoldi s a -> (i -> a ->Bool) -> s ->BooliallOf::IndexedLens'i s a -> (i -> a ->Bool) -> s ->BooliallOf::IndexedTraversal'i s a -> (i -> a ->Bool) -> s ->Bool
inoneOf :: IndexedGetting i Any s a -> (i -> a -> Bool) -> s -> Bool Source #
Return whether or not none of the elements viewed through an IndexedFold or IndexedTraversal
satisfy a predicate, with access to the i.
When you don't need access to the index then noneOf is more flexible in what it accepts.
noneOfl ≡inoneOfl.const
inoneOf::IndexedGetteri s a -> (i -> a ->Bool) -> s ->BoolinoneOf::IndexedFoldi s a -> (i -> a ->Bool) -> s ->BoolinoneOf::IndexedLens'i s a -> (i -> a ->Bool) -> s ->BoolinoneOf::IndexedTraversal'i s a -> (i -> a ->Bool) -> s ->Bool
itraverseOf_ :: Functor f => IndexedGetting i (Traversed r f) s a -> (i -> a -> f r) -> s -> f () Source #
Traverse the targets of an IndexedFold or IndexedTraversal with access to the i, discarding the results.
When you don't need access to the index then traverseOf_ is more flexible in what it accepts.
traverseOf_l ≡itraverseOfl.const
itraverseOf_::Functorf =>IndexedGetteri s a -> (i -> a -> f r) -> s -> f ()itraverseOf_::Applicativef =>IndexedFoldi s a -> (i -> a -> f r) -> s -> f ()itraverseOf_::Functorf =>IndexedLens'i s a -> (i -> a -> f r) -> s -> f ()itraverseOf_::Applicativef =>IndexedTraversal'i s a -> (i -> a -> f r) -> s -> f ()
iforOf_ :: Functor f => IndexedGetting i (Traversed r f) s a -> s -> (i -> a -> f r) -> f () Source #
Traverse the targets of an IndexedFold or IndexedTraversal with access to the index, discarding the results
(with the arguments flipped).
iforOf_≡flip.itraverseOf_
When you don't need access to the index then forOf_ is more flexible in what it accepts.
forOf_l a ≡iforOf_l a.const
iforOf_::Functorf =>IndexedGetteri s a -> s -> (i -> a -> f r) -> f ()iforOf_::Applicativef =>IndexedFoldi s a -> s -> (i -> a -> f r) -> f ()iforOf_::Functorf =>IndexedLens'i s a -> s -> (i -> a -> f r) -> f ()iforOf_::Applicativef =>IndexedTraversal'i s a -> s -> (i -> a -> f r) -> f ()
imapMOf_ :: Monad m => IndexedGetting i (Sequenced r m) s a -> (i -> a -> m r) -> s -> m () Source #
Run monadic actions for each target of an IndexedFold or IndexedTraversal with access to the index,
discarding the results.
When you don't need access to the index then mapMOf_ is more flexible in what it accepts.
mapMOf_l ≡imapMOfl.const
imapMOf_::Monadm =>IndexedGetteri s a -> (i -> a -> m r) -> s -> m ()imapMOf_::Monadm =>IndexedFoldi s a -> (i -> a -> m r) -> s -> m ()imapMOf_::Monadm =>IndexedLens'i s a -> (i -> a -> m r) -> s -> m ()imapMOf_::Monadm =>IndexedTraversal'i s a -> (i -> a -> m r) -> s -> m ()
iforMOf_ :: Monad m => IndexedGetting i (Sequenced r m) s a -> s -> (i -> a -> m r) -> m () Source #
Run monadic actions for each target of an IndexedFold or IndexedTraversal with access to the index,
discarding the results (with the arguments flipped).
iforMOf_≡flip.imapMOf_
When you don't need access to the index then forMOf_ is more flexible in what it accepts.
forMOf_l a ≡iforMOfl a.const
iforMOf_::Monadm =>IndexedGetteri s a -> s -> (i -> a -> m r) -> m ()iforMOf_::Monadm =>IndexedFoldi s a -> s -> (i -> a -> m r) -> m ()iforMOf_::Monadm =>IndexedLens'i s a -> s -> (i -> a -> m r) -> m ()iforMOf_::Monadm =>IndexedTraversal'i s a -> s -> (i -> a -> m r) -> m ()
iconcatMapOf :: IndexedGetting i [r] s a -> (i -> a -> [r]) -> s -> [r] Source #
Concatenate the results of a function of the elements of an IndexedFold or IndexedTraversal
with access to the index.
When you don't need access to the index then concatMapOf is more flexible in what it accepts.
concatMapOfl ≡iconcatMapOfl.consticoncatMapOf≡ifoldMapOf
iconcatMapOf::IndexedGetteri s a -> (i -> a -> [r]) -> s -> [r]iconcatMapOf::IndexedFoldi s a -> (i -> a -> [r]) -> s -> [r]iconcatMapOf::IndexedLens'i s a -> (i -> a -> [r]) -> s -> [r]iconcatMapOf::IndexedTraversal'i s a -> (i -> a -> [r]) -> s -> [r]
ifindOf :: IndexedGetting i (Endo (Maybe a)) s a -> (i -> a -> Bool) -> s -> Maybe a Source #
The ifindOf function takes an IndexedFold or IndexedTraversal, a predicate that is also
supplied the index, a structure and returns the left-most element of the structure
matching the predicate, or Nothing if there is no such element.
When you don't need access to the index then findOf is more flexible in what it accepts.
findOfl ≡ifindOfl.const
ifindOf::IndexedGetteri s a -> (i -> a ->Bool) -> s ->MaybeaifindOf::IndexedFoldi s a -> (i -> a ->Bool) -> s ->MaybeaifindOf::IndexedLens'i s a -> (i -> a ->Bool) -> s ->MaybeaifindOf::IndexedTraversal'i s a -> (i -> a ->Bool) -> s ->Maybea
ifindMOf :: Monad m => IndexedGetting i (Endo (m (Maybe a))) s a -> (i -> a -> m Bool) -> s -> m (Maybe a) Source #
The ifindMOf function takes an IndexedFold or IndexedTraversal, a monadic predicate that is also
supplied the index, a structure and returns in the monad the left-most element of the structure
matching the predicate, or Nothing if there is no such element.
When you don't need access to the index then findMOf is more flexible in what it accepts.
findMOfl ≡ifindMOfl.const
ifindMOf::Monadm =>IndexedGetteri s a -> (i -> a -> mBool) -> s -> m (Maybea)ifindMOf::Monadm =>IndexedFoldi s a -> (i -> a -> mBool) -> s -> m (Maybea)ifindMOf::Monadm =>IndexedLens'i s a -> (i -> a -> mBool) -> s -> m (Maybea)ifindMOf::Monadm =>IndexedTraversal'i s a -> (i -> a -> mBool) -> s -> m (Maybea)
ifoldrOf' :: IndexedGetting i (Dual (Endo (r -> r))) s a -> (i -> a -> r -> r) -> r -> s -> r Source #
Strictly fold right over the elements of a structure with an index.
When you don't need access to the index then foldrOf' is more flexible in what it accepts.
foldrOf'l ≡ifoldrOf'l.const
ifoldrOf'::IndexedGetteri s a -> (i -> a -> r -> r) -> r -> s -> rifoldrOf'::IndexedFoldi s a -> (i -> a -> r -> r) -> r -> s -> rifoldrOf'::IndexedLens'i s a -> (i -> a -> r -> r) -> r -> s -> rifoldrOf'::IndexedTraversal'i s a -> (i -> a -> r -> r) -> r -> s -> r
ifoldlOf' :: IndexedGetting i (Endo (r -> r)) s a -> (i -> r -> a -> r) -> r -> s -> r Source #
Fold over the elements of a structure with an index, associating to the left, but strictly.
When you don't need access to the index then foldlOf' is more flexible in what it accepts.
foldlOf'l ≡ifoldlOf'l.const
ifoldlOf'::IndexedGetteri s a -> (i -> r -> a -> r) -> r -> s -> rifoldlOf'::IndexedFoldi s a -> (i -> r -> a -> r) -> r -> s -> rifoldlOf'::IndexedLens'i s a -> (i -> r -> a -> r) -> r -> s -> rifoldlOf'::IndexedTraversal'i s a -> (i -> r -> a -> r) -> r -> s -> r
ifoldrMOf :: Monad m => IndexedGetting i (Dual (Endo (r -> m r))) s a -> (i -> a -> r -> m r) -> r -> s -> m r Source #
Monadic fold right over the elements of a structure with an index.
When you don't need access to the index then foldrMOf is more flexible in what it accepts.
foldrMOfl ≡ifoldrMOfl.const
ifoldrMOf::Monadm =>IndexedGetteri s a -> (i -> a -> r -> m r) -> r -> s -> m rifoldrMOf::Monadm =>IndexedFoldi s a -> (i -> a -> r -> m r) -> r -> s -> m rifoldrMOf::Monadm =>IndexedLens'i s a -> (i -> a -> r -> m r) -> r -> s -> m rifoldrMOf::Monadm =>IndexedTraversal'i s a -> (i -> a -> r -> m r) -> r -> s -> m r
ifoldlMOf :: Monad m => IndexedGetting i (Endo (r -> m r)) s a -> (i -> r -> a -> m r) -> r -> s -> m r Source #
Monadic fold over the elements of a structure with an index, associating to the left.
When you don't need access to the index then foldlMOf is more flexible in what it accepts.
foldlMOfl ≡ifoldlMOfl.const
ifoldlMOf::Monadm =>IndexedGetteri s a -> (i -> r -> a -> m r) -> r -> s -> m rifoldlMOf::Monadm =>IndexedFoldi s a -> (i -> r -> a -> m r) -> r -> s -> m rifoldlMOf::Monadm =>IndexedLens'i s a -> (i -> r -> a -> m r) -> r -> s -> m rifoldlMOf::Monadm =>IndexedTraversal'i s a -> (i -> r -> a -> m r) -> r -> s -> m r
itoListOf :: IndexedGetting i (Endo [(i, a)]) s a -> s -> [(i, a)] Source #
Extract the key-value pairs from a structure.
When you don't need access to the indices in the result, then toListOf is more flexible in what it accepts.
toListOfl ≡mapsnd.itoListOfl
itoListOf::IndexedGetteri s a -> s -> [(i,a)]itoListOf::IndexedFoldi s a -> s -> [(i,a)]itoListOf::IndexedLens'i s a -> s -> [(i,a)]itoListOf::IndexedTraversal'i s a -> s -> [(i,a)]
elemIndexOf :: Eq a => IndexedGetting i (First i) s a -> a -> s -> Maybe i Source #
Retrieve the index of the first value targeted by a IndexedFold or IndexedTraversal which is equal to a given value.
elemIndex≡elemIndexOffolded
elemIndexOf::Eqa =>IndexedFoldi s a -> a -> s ->MaybeielemIndexOf::Eqa =>IndexedTraversal'i s a -> a -> s ->Maybei
elemIndicesOf :: Eq a => IndexedGetting i (Endo [i]) s a -> a -> s -> [i] Source #
Retrieve the indices of the values targeted by a IndexedFold or IndexedTraversal which are equal to a given value.
elemIndices≡elemIndicesOffolded
elemIndicesOf::Eqa =>IndexedFoldi s a -> a -> s -> [i]elemIndicesOf::Eqa =>IndexedTraversal'i s a -> a -> s -> [i]
findIndexOf :: IndexedGetting i (First i) s a -> (a -> Bool) -> s -> Maybe i Source #
Retrieve the index of the first value targeted by a IndexedFold or IndexedTraversal which satisfies a predicate.
findIndex≡findIndexOffolded
findIndexOf::IndexedFoldi s a -> (a ->Bool) -> s ->MaybeifindIndexOf::IndexedTraversal'i s a -> (a ->Bool) -> s ->Maybei
findIndicesOf :: IndexedGetting i (Endo [i]) s a -> (a -> Bool) -> s -> [i] Source #
Retrieve the indices of the values targeted by a IndexedFold or IndexedTraversal which satisfy a predicate.
findIndices≡findIndicesOffolded
findIndicesOf::IndexedFoldi s a -> (a ->Bool) -> s -> [i]findIndicesOf::IndexedTraversal'i s a -> (a ->Bool) -> s -> [i]
Building Indexed Folds
ifiltered :: (Indexable i p, Applicative f) => (i -> a -> Bool) -> Optical' p (Indexed i) f a a Source #
Filter an IndexedFold or IndexedGetter, obtaining an IndexedFold.
>>>[0,0,0,5,5,5]^..traversed.ifiltered (\i a -> i <= a)[0,5,5,5]
Compose with ifiltered to filter another IndexedLens, IndexedIso, IndexedGetter, IndexedFold (or IndexedTraversal) with
access to both the value and the index.
Note: As with filtered, this is not a legal IndexedTraversal, unless you are very careful not to invalidate the predicate on the target!
itakingWhile :: (Indexable i p, Profunctor q, Contravariant f, Applicative f) => (i -> a -> Bool) -> Optical' (Indexed i) q (Const (Endo (f s))) s a -> Optical' p q f s a Source #
Obtain an IndexedFold by taking elements from another
IndexedFold, IndexedLens, IndexedGetter or IndexedTraversal while a predicate holds.
itakingWhile:: (i -> a ->Bool) ->IndexedFoldi s a ->IndexedFoldi s aitakingWhile:: (i -> a ->Bool) ->IndexedTraversal'i s a ->IndexedFoldi s aitakingWhile:: (i -> a ->Bool) ->IndexedLens'i s a ->IndexedFoldi s aitakingWhile:: (i -> a ->Bool) ->IndexedGetteri s a ->IndexedFoldi s a
Note: Applying itakingWhile to an IndexedLens or IndexedTraversal will still allow you to use it as a
pseudo-IndexedTraversal, but if you change the value of any target to one where the predicate returns
False, then you will break the Traversal laws and Traversal fusion will no longer be sound.
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 Source #
Obtain an IndexedFold by dropping elements from another IndexedFold, IndexedLens, IndexedGetter or IndexedTraversal while a predicate holds.
idroppingWhile:: (i -> a ->Bool) ->IndexedFoldi s a ->IndexedFoldi s aidroppingWhile:: (i -> a ->Bool) ->IndexedTraversal'i s a ->IndexedFoldi s a -- see notesidroppingWhile:: (i -> a ->Bool) ->IndexedLens'i s a ->IndexedFoldi s a -- see notesidroppingWhile:: (i -> a ->Bool) ->IndexedGetteri s a ->IndexedFoldi s a
Note: As with droppingWhile applying idroppingWhile to an IndexedLens or IndexedTraversal will still
allow you to use it as a pseudo-IndexedTraversal, but if you change the value of the first target to one
where the predicate returns True, then you will break the Traversal laws and Traversal fusion will
no longer be sound.
Internal types
Used internally by traverseOf_ and the like.
The argument a of the result should not be used!
Instances
| Applicative f => Semigroup (Traversed a f) Source # | |
| Applicative f => Monoid (Traversed a f) Source # | |
Used internally by mapM_ and the like.
The argument a of the result should not be used!
See 4.16 Changelog entry for the explanation of "why not Apply f =>"?
Fold with Reified Monoid
foldByOf :: Fold s a -> (a -> a -> a) -> a -> s -> a Source #
Fold a value using a specified Fold and Monoid operations.
This is like foldBy where the Foldable instance can be
manually specified.
foldByOffolded≡foldBy
foldByOf::Getters a -> (a -> a -> a) -> a -> s -> afoldByOf::Folds a -> (a -> a -> a) -> a -> s -> afoldByOf::Lens's a -> (a -> a -> a) -> a -> s -> afoldByOf::Traversal's a -> (a -> a -> a) -> a -> s -> afoldByOf::Iso's a -> (a -> a -> a) -> a -> s -> a
>>>foldByOf both (++) [] ("hello","world")"helloworld"
foldMapByOf :: Fold s a -> (r -> r -> r) -> r -> (a -> r) -> s -> r Source #
Fold a value using a specified Fold and Monoid operations.
This is like foldMapBy where the Foldable instance can be
manually specified.
foldMapByOffolded≡foldMapBy
foldMapByOf::Getters a -> (r -> r -> r) -> r -> (a -> r) -> s -> rfoldMapByOf::Folds a -> (r -> r -> r) -> r -> (a -> r) -> s -> rfoldMapByOf::Traversal's a -> (r -> r -> r) -> r -> (a -> r) -> s -> rfoldMapByOf::Lens's a -> (r -> r -> r) -> r -> (a -> r) -> s -> rfoldMapByOf::Iso's a -> (r -> r -> r) -> r -> (a -> r) -> s -> r
>>>foldMapByOf both (+) 0 length ("hello","world")10