HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulelens-5.3.5Haskell2010

Control.Lens.Combinators

This lets the subset of users who vociferously disagree about the full scope and set of operators that should be exported from lens to not have to look at any operator with which they disagree.

import Control.Lens.Combinators
  • 125 types
  • 52 classes
  • 480 values
  • Packagelens-5.3.5
  • Exports670
  • LanguageHaskell2010
  • LicenceBSD-2-Clause
  • SourceCombinators.hs
typetype Lens s t a b = forall (f :: Type -> Type). Functor f => (a -> f b) -> s -> f t
#

A Lens is actually a lens family as described in http://comonad.com/reader/2012/mirrored-lenses/.

With great power comes great responsibility and a Lens is subject to the three common sense Lens laws:

1) You get back what you put in:

view l (set l v s)  ≡ v

2) Putting back what you got doesn't change anything:

set l (view l s) s  ≡ s

3) Setting twice is the same as setting once:

set l v' (set l v s) ≡ set l v' s

These laws are strong enough that the 4 type parameters of a Lens cannot vary fully independently. For more on how they interact, read the "Why is it a Lens Family?" section of http://comonad.com/reader/2012/mirrored-lenses/.

There are some emergent properties of these laws:

1) set l s must be injective for every s This is a consequence of law #1

2) set l must be surjective, because of law #2, which indicates that it is possible to obtain any v from some s such that set s v = s

3) Given just the first two laws you can prove a weaker form of law #3 where the values v that you are setting match:

set l v (set l v s) ≡ set l v s

Every Lens can be used directly as a Control.Lens.Setter.Setter or Traversal.

You can also use a Lens for Getting as if it were a Fold or Getter.

Since every Lens is a valid Traversal, the Traversal laws are required of any Lens you create:

l pure ≡ pure
fmap (l f) . l g ≡ getCompose . l (Compose . fmap f . g)
type Lens s t a b = forall f. Functor f => LensLike f s t a b
classclass Ixed m => At m where
#

At provides a Lens that can be used to read, write or delete the value associated with a key in a Map-like container on an ad hoc basis.

An instance of At should satisfy:

ix k ≡ at k . traverse

Methods

  • at :: Index m -> Lens' m (Maybe (IxValue m))
    Example1 expression
    Map.fromList [(1,"world")] ^.at 1Just "world"
    Example1 expression
    at 1 ?~ "hello" $ Map.emptyfromList [(1,"hello")]

    Note: Map-like containers form a reasonable instance, but not Array-like ones, where you cannot satisfy the Lens laws.

Instances7At, …
  • At IntSetDefined in lens-5.3.5 · Control.Lens.At
  • Ord k => At (Set k)Defined in lens-5.3.5 · Control.Lens.At
  • At (IntMap a)Defined in lens-5.3.5 · Control.Lens.At
  • At (Maybe a)Defined in lens-5.3.5 · Control.Lens.At
  • (Eq k, Hashable k) => At (HashSet k)Defined in lens-5.3.5 · Control.Lens.At
  • Ord k => At (Map k a)Defined in lens-5.3.5 · Control.Lens.At
  • (Eq k, Hashable k) => At (HashMap k a)Defined in lens-5.3.5 · Control.Lens.At
classclass Cons s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

This class provides a way to attach or detach elements on the left side of a structure in a flexible manner.

Methods

Instances13Cons, …
classclass Each s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Extract each element of a (potentially monomorphic) container.

Notably, when applied to a tuple, this generalizes both to arbitrary homogeneous tuples.

Example1 expression
(1,2,3) & each *~ 10(10,20,30)

It can also be used on monomorphic containers like Text or ByteString.

Example1 expression
over each Char.toUpper ("hello"^.Text.packed)"HELLO"
Example1 expression
("hello","world") & each.each %~ Char.toUpper("HELLO","WORLD")

Methods

Instances35Each, …
typetype Equality (s :: k1) (t :: k2) (a :: k1) (b :: k2) = forall k3 (p :: k1 -> k3 -> Type) (f :: k2 -> k3). p a (f b) -> p s (f t)
#

A witness that (a ~ s, b ~ t).

Note: Composition with an Equality is index-preserving.

typetype Fold s a = forall (f :: Type -> Type). (Contravariant f, Applicative f) => (a -> f a) -> s -> f s
#

A Fold describes how to retrieve multiple values in a way that can be composed with other LensLike constructions.

A Fold s a provides a structure with operations very similar to those of the Foldable typeclass, see foldMapOf and the other Fold combinators.

By convention, if there exists a foo method that expects a Foldable (f a), then there should be a fooOf method that takes a Fold s a and a value of type s.

A Getter is a legal Fold that just ignores the supplied Monoid.

Unlike a Control.Lens.Traversal.Traversal a Fold is read-only. Since a Fold cannot be used to write back there are no Lens laws that apply.

newtypenewtype ReifiedFold s a
#

Reify a Fold so it can be stored safely in a container.

This can also be useful for creatively combining folds as ReifiedFold s is isomorphic to ReaderT s [] and provides similar instances.

Example1 expression
("hello","world")^..runFold ((,) <$> Fold _2 <*> Fold both)[("world","hello"),("world","world")]

Constructors

Instances22Arrow, ArrowApply, ArrowChoice, Choice, Strong, Profunctor, …
typetype Getter s a = forall (f :: Type -> Type). (Contravariant f, Functor f) => (a -> f a) -> s -> f s
#

A Getter describes how to retrieve a single value in a way that can be composed with other LensLike constructions.

Unlike a Lens a Getter is read-only. Since a Getter cannot be used to write back there are no Lens laws that can be applied to it. In fact, it is isomorphic to an arbitrary function from (s -> a).

Moreover, a Getter can be used directly as a Control.Lens.Fold.Fold, since it just ignores the Applicative.

newtypenewtype ReifiedGetter s a
#

Reify a Getter so it can be stored safely in a container.

This can also be useful when combining getters in novel ways, as ReifiedGetter is isomorphic to (->) and provides similar instances.

Example1 expression
("hello","world","!!!")^.runGetter ((,) <$> Getter _2 <*> Getter (_1.to length))("world",5)

Constructors

Instances27Arrow, ArrowApply, ArrowChoice, ArrowLoop, Choice, Closed, …
newtypenewtype Indexed i a b
#

A function with access to a index. This constructor may be useful when you need to store an Indexable in a container to avoid ImpredicativeTypes.

index :: Indexed i a b -> i -> a -> b

Constructors

Instances27Category, Indexable, Arrow, ArrowApply, ArrowChoice, ArrowLoop, …
typetype Iso s t a b = forall (p :: Type -> Type -> Type) (f :: Type -> Type). (Profunctor p, Functor f) => p a (f b) -> p s (f t)
#

Isomorphism families can be composed with another Lens using (.) and id.

Since every Iso is both a valid Lens and a valid Prism, the laws for those types imply the following laws for an Iso f:

f . from f ≡ id
from f . f ≡ id

Note: Composition with an Iso is index- and measure- preserving.

newtypenewtype ReifiedIso s t a b
#

Reify an Iso so it can be stored safely in a container.

Constructors

datadata Level i a
#

This data type represents a path-compressed copy of one level of a source data structure. We can safely use path-compression because we know the depth of the tree.

Path compression is performed by viewing a Level as a PATRICIA trie of the paths into the structure to leaves at a given depth, similar in many ways to a Data.IntMap.IntMap, but unlike a regular PATRICIA trie we do not need to store the mask bits merely the depth of the fork.

One invariant of this structure is that underneath a Two node you will not find any Zero nodes, so Zero can only occur at the root.

Instances10FoldableWithIndex, FunctorWithIndex, TraversableWithIndex, Functor, Foldable, Traversable, …
classclass Plated a where
#

A Plated type is one where we know how to extract its immediate self-similar children.

Example 1:

import Control.Applicative
import Control.Lens
import Control.Lens.Plated
import Data.Data
import Data.Data.Lens (uniplate)
data Expr
  = Val Int
  | Neg Expr
  | Add Expr Expr
  deriving (Eq,Ord,Show,Read,Data)
instance Plated Expr where
  plate f (Neg e) = Neg <$> f e
  plate f (Add a b) = Add <$> f a <*> f b
  plate _ a = pure a

or

instance Plated Expr where
  plate = uniplate

Example 2:

import Control.Applicative
import Control.Lens
import Control.Lens.Plated
import Data.Data
import Data.Data.Lens (uniplate)
data Tree a
  = Bin (Tree a) (Tree a)
  | Tip a
  deriving (Eq,Ord,Show,Read,Data)
instance Plated (Tree a) where
  plate f (Bin l r) = Bin <$> f l <*> f r
  plate _ t = pure t

or

instance Data a => Plated (Tree a) where
  plate = uniplate

Note the big distinction between these two implementations.

The former will only treat children directly in this tree as descendents, the latter will treat trees contained in the values under the tips also as descendants!

When in doubt, pick a Traversal and just use the various ...Of combinators rather than pollute Plated with orphan instances!

If you want to find something unplated and non-recursive with biplate use the ...OnOf variant with ignored, though those usecases are much better served in most cases by using the existing Lens combinators! e.g.

toListOf biplate ≡ universeOnOf biplate ignored

This same ability to explicitly pass the Traversal in question is why there is no analogue to uniplate's Biplate.

Moreover, since we can allow custom traversals, we implement reasonable defaults for polymorphic data types, that only Control.Traversable.traverse into themselves, and not their polymorphic arguments.

Methods

  • plate :: Traversal' a a

    Traversal of the immediate children of this structure.

    If you're using GHC 7.2 or newer and your type has a Data instance, plate will default to uniplate and you can choose to not override it with your own definition.

Instances13Plated, …
typetype Prism s t a b = forall (p :: Type -> Type -> Type) (f :: Type -> Type). (Choice p, Applicative f) => p a (f b) -> p s (f t)
#

A Prism l is a Traversal that can also be turned around with re to obtain a Getter in the opposite direction.

There are three laws that a Prism should satisfy:

First, if I re or review a value with a Prism and then preview or use (^?), I will get it back:

preview l (review l b) ≡ Just b

Second, if you can extract a value a using a Prism l from a value s, then the value s is completely described by l and a:

preview l s ≡ Just a ⟹ review l a ≡ s

Third, if you get non-match t, you can convert it result back to s:

Control.Lens.Combinators.matching l s ≡ Left t ⟹ Control.Lens.Combinators.matching l t ≡ Left s

The first two laws imply that the Traversal laws hold for every Prism and that we traverse at most 1 element:

lengthOf l x <= 1

It may help to think of this as an Iso that can be partial in one direction.

Every Prism is a valid Traversal.

Every Iso is a valid Prism.

For example, you might have a Prism' Integer Numeric.Natural.Natural allows you to always go from a Numeric.Natural.Natural to an Integer, and provide you with tools to check if an Integer is a Numeric.Natural.Natural and/or to edit one if it is.

nat :: Prism' Integer Numeric.Natural.Natural
nat = prism toInteger $ \ i ->
   if i < 0
   then Left i
   else Right (fromInteger i)

Now we can ask if an Integer is a Numeric.Natural.Natural.

Example1 expression
5^?natJust 5
Example1 expression
(-5)^?natNothing

We can update the ones that are:

Example1 expression
(-3,4) & both.nat *~ 2(-3,8)

And we can then convert from a Numeric.Natural.Natural to an Integer.

Example1 expression
5 ^. re nat -- :: Natural5

Similarly we can use a Prism to traverse the Left half of an Either:

Example1 expression
Left "hello" & _Left %~ lengthLeft 5

or to construct an Either:

Example1 expression
5^.re _LeftLeft 5

such that if you query it with the Prism, you will get your original input back.

Example1 expression
5^.re _Left ^? _LeftJust 5

Another interesting way to think of a Prism is as the categorical dual of a Lens -- a co-Lens, so to speak. This is what permits the construction of outside.

Note: Composition with a Prism is index-preserving.

typetype Setter s t a b = forall (f :: Type -> Type). Settable f => (a -> f b) -> s -> f t
#

The only LensLike law that can apply to a Setter l is that

set l y (set l x a) ≡ set l y a

You can't view a Setter in general, so the other two laws are irrelevant.

However, two Functor laws apply to a Setter:

over l id ≡ id
over l f . over l g ≡ over l (f . g)

These can be stated more directly:

l pure ≡ pure
l f . untainted . l g ≡ l (f . untainted . g)

You can compose a Setter with a Lens or a Traversal using (.) from the Prelude and the result is always only a Setter and nothing more.

Example1 expression
over traverse f [a,b,c,d][f a,f b,f c,f d]
Example1 expression
over _1 f (a,b)(f a,b)
Example1 expression
over (traverse._1) f [(a,b),(c,d)][(f a,b),(f c,d)]
Example1 expression
over both f (a,b)(f a,f b)
Example1 expression
over (traverse.both) f [(a,b),(c,d)][(f a,f b),(f c,f d)]
typetype Traversal s t a b = forall (f :: Type -> Type). Applicative f => (a -> f b) -> s -> f t
#

A Traversal can be used directly as a Control.Lens.Setter.Setter or a Fold (but not as a Lens) and provides the ability to both read and update multiple fields, subject to some relatively weak Traversal laws.

These have also been known as multilenses, but they have the signature and spirit of

traverse :: Traversable f => Traversal (f a) (f b) a b

and the more evocative name suggests their application.

Most of the time the Traversal you will want to use is just traverse, but you can also pass any Lens or Iso as a Traversal, and composition of a Traversal (or Lens or Iso) with a Traversal (or Lens or Iso) using (.) forms a valid Traversal.

The laws for a Traversal t follow from the laws for Traversable as stated in "The Essence of the Iterator Pattern".

t pure ≡ pure
fmap (t f) . t g ≡ getCompose . t (Compose . fmap f . g)

One consequence of this requirement is that a Traversal needs to leave the same number of elements as a candidate for subsequent Traversal that it started with. Another testament to the strength of these laws is that the caveat expressed in section 5.5 of the "Essence of the Iterator Pattern" about exotic Traversable instances that traverse the same entry multiple times was actually already ruled out by the second law in that same paper!

classclass Wrapped s where
#

Wrapped provides isomorphisms to wrap and unwrap newtypes or data types with one constructor.

Associated types

Methods

Instances149Wrapped, …
classclass (MonadState s m, MonadState t n) => Zoom (m :: Type -> Type) (n :: Type -> Type) s t | m -> s, n -> t, m t -> n, n s -> m where
#

This class allows us to use zoom in, changing the State supplied by many different Monad transformers, potentially quite deep in a Monad transformer stack.

Methods

  • zoom :: LensLike' (Zoomed m c) t s -> m c -> n cinfixr 2

    Run a monadic action in a larger State than it was defined in, using a Lens' or Control.Lens.Traversal.Traversal'.

    This is commonly used to lift actions in a simpler State Monad into a State Monad with a larger State type.

    When applied to a Control.Lens.Traversal.Traversal' over multiple values, the actions for each target are executed sequentially and the results are aggregated.

    This can be used to edit pretty much any Monad transformer stack with a State in it!

    Example1 expression
    flip State.evalState (a,b) $ zoom _1 $ use ida
    Example1 expression
    flip State.execState (a,b) $ zoom _1 $ id .= c(c,b)
    Example1 expression
    flip State.execState [(a,b),(c,d)] $ zoom traverse $ _2 %= f[(a,f b),(c,f d)]
    Example1 expression
    flip State.runState [(a,b),(c,d)] $ zoom traverse $ _2 <%= f(f b <> f d <> mempty,[(a,f b),(c,f d)])
    Example1 expression
    flip State.evalState (a,b) $ zoom both (use id)a <> b
    zoom :: Monad m             => Lens' s t      -> StateT t m a -> StateT s m a
    zoom :: (Monad m, Monoid c) => Control.Lens.Traversal.Traversal' s t -> StateT t m c -> StateT s m c
    zoom :: (Monad m, Monoid w)             => Lens' s t      -> RWST r w t m c -> RWST r w s m c
    zoom :: (Monad m, Monoid w, Monoid c) => Control.Lens.Traversal.Traversal' s t -> RWST r w t m c -> RWST r w s m c
    zoom :: (Monad m, Monoid w, Error e)  => Lens' s t      -> ErrorT e (RWST r w t m) c -> ErrorT e (RWST r w s m) c
    zoom :: (Monad m, Monoid w, Monoid c, Error e) => Control.Lens.Traversal.Traversal' s t -> ErrorT e (RWST r w t m) c -> ErrorT e (RWST r w s m) c
    ...
    
Instances11Zoom, …
valuesans :: At m => Index m -> m -> m
#

Delete the value associated with a key in a Map-like container

sans k = at k .~ Nothing
valueiat :: At m => Index m -> IndexedLens' (Index m) m (Maybe (IxValue m))
#

An indexed version of at.

Example1 expression
Map.fromList [(1,"world")] ^@. iat 1(1,Just "world")
Example1 expression
iat 1 %@~ (\i x -> if odd i then Just "hello" else Nothing) $ Map.emptyfromList [(1,"hello")]
Example1 expression
iat 2 %@~ (\i x -> if odd i then Just "hello" else Nothing) $ Map.emptyfromList []
familytype family Index s
#
Instances33Index, …
  • type Index ByteString = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index ByteString = Int64Defined in lens-5.3.5 · Control.Lens.At
  • type Index IntSet = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index Text = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index Text = Int64Defined in lens-5.3.5 · Control.Lens.At
  • type Index (UArray i e) = iDefined in lens-5.3.5 · Control.Lens.At
  • type Index (Complex a) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (IntMap a) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (Map k a) = kDefined in lens-5.3.5 · Control.Lens.At
  • type Index (Seq a) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (Set a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type Index (Tree a) = [Int]Defined in lens-5.3.5 · Control.Lens.At
  • type Index (Array i e) = iDefined in lens-5.3.5 · Control.Lens.At
  • type Index (NonEmpty a) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (Identity a) = ()Defined in lens-5.3.5 · Control.Lens.At
  • type Index (Maybe a) = ()Defined in lens-5.3.5 · Control.Lens.At
  • type Index (HashMap k a) = kDefined in lens-5.3.5 · Control.Lens.At
  • type Index (HashSet a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type Index (Vector a) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (Vector a) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (Vector a) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (Vector a) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (Vector a) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (a, b) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (a, b, c) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (a, b, c, d) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (a, b, c, d, e) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (a, b, c, d, e, f) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (a, b, c, d, e, f, g) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (a, b, c, d, e, f, g, h) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (a, b, c, d, e, f, g, h, i) = IntDefined in lens-5.3.5 · Control.Lens.At
  • type Index (e -> a) = eDefined in lens-5.3.5 · Control.Lens.At
  • type Index [a] = IntDefined in lens-5.3.5 · Control.Lens.At
familytype family IxValue m
#

This provides a common notion of a value at an index that is shared by both Ixed and At.

Instances32IxValue, …
  • type IxValue ByteString = Word8Defined in lens-5.3.5 · Control.Lens.At
  • type IxValue ByteString = Word8Defined in lens-5.3.5 · Control.Lens.At
  • type IxValue IntSet = ()Defined in lens-5.3.5 · Control.Lens.At
  • type IxValue Text = CharDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue Text = CharDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (UArray i e) = eDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (IntMap a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (Map k a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (Seq a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (Set k) = ()Defined in lens-5.3.5 · Control.Lens.At
  • type IxValue (Tree a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (Array i e) = eDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (NonEmpty a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (Identity a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (Maybe a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (HashMap k a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (HashSet k) = ()Defined in lens-5.3.5 · Control.Lens.At
  • type IxValue (Vector a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (Vector a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (Vector a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (Vector a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (Vector a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue (a, a2) = aDefined in lens-5.3.5 · Control.Lens.At
    ix :: Int -> Traversal' (a,a) a
  • type IxValue (a, a2, a3) = aDefined in lens-5.3.5 · Control.Lens.At
    ix :: Int -> Traversal' (a,a,a) a
  • type IxValue (a, a2, a3, a4) = aDefined in lens-5.3.5 · Control.Lens.At
    ix :: Int -> Traversal' (a,a,a,a) a
  • type IxValue (a, a2, a3, a4, a5) = aDefined in lens-5.3.5 · Control.Lens.At
    ix :: Int -> Traversal' (a,a,a,a,a) a
  • type IxValue (a, a2, a3, a4, a5, a6) = aDefined in lens-5.3.5 · Control.Lens.At
    ix :: Int -> Traversal' (a,a,a,a,a,a) a
  • type IxValue (a, a2, a3, a4, a5, a6, a7) = aDefined in lens-5.3.5 · Control.Lens.At
    ix :: Int -> Traversal' (a,a,a,a,a,a,a) a
  • type IxValue (a, a2, a3, a4, a5, a6, a7, a8) = aDefined in lens-5.3.5 · Control.Lens.At
    ix :: Int -> Traversal' (a,a,a,a,a,a,a,a) a
  • type IxValue (a, a2, a3, a4, a5, a6, a7, a8, a9) = aDefined in lens-5.3.5 · Control.Lens.At
    ix :: Int -> Traversal' (a,a,a,a,a,a,a,a,a) a
  • type IxValue (e -> a) = aDefined in lens-5.3.5 · Control.Lens.At
  • type IxValue [a] = aDefined in lens-5.3.5 · Control.Lens.At
classclass Ixed m where
#

Provides a simple Traversal lets you traverse the value at a given key in a Map or element at an ordinal position in a list or Seq.

Methods

  • ix :: Index m -> Traversal' m (IxValue m)

    NB: Setting the value of this Traversal will only set the value in at if it is already present.

    If you want to be able to insert missing values, you want at.

    Example1 expression
    Seq.fromList [a,b,c,d] & ix 2 %~ ffromList [a,b,f c,d]
    Example1 expression
    Seq.fromList [a,b,c,d] & ix 2 .~ efromList [a,b,e,d]
    Example1 expression
    Seq.fromList [a,b,c,d] ^? ix 2Just c
    Example1 expression
    Seq.fromList [] ^? ix 2Nothing
Instances32Ixed, …
  • Ixed ByteStringDefined in lens-5.3.5 · Control.Lens.At
  • Ixed ByteStringDefined in lens-5.3.5 · Control.Lens.At
  • Ixed IntSetDefined in lens-5.3.5 · Control.Lens.At
  • Ixed TextDefined in lens-5.3.5 · Control.Lens.At
  • Ixed TextDefined in lens-5.3.5 · Control.Lens.At
  • Storable a => Ixed (Vector a)Defined in lens-5.3.5 · Control.Lens.At
  • Ord k => Ixed (Set k)Defined in lens-5.3.5 · Control.Lens.At
  • Ixed (IntMap a)Defined in lens-5.3.5 · Control.Lens.At
  • Ixed (Seq a)Defined in lens-5.3.5 · Control.Lens.At
  • Ixed (Tree a)Defined in lens-5.3.5 · Control.Lens.At
  • Ixed (NonEmpty a)Defined in lens-5.3.5 · Control.Lens.At
  • Ixed (Identity a)Defined in lens-5.3.5 · Control.Lens.At
  • Ixed (Maybe a)Defined in lens-5.3.5 · Control.Lens.At
  • Ixed (Vector a)Defined in lens-5.3.5 · Control.Lens.At
  • Ixed (Vector a)Defined in lens-5.3.5 · Control.Lens.At
  • Ixed [a]Defined in lens-5.3.5 · Control.Lens.At
  • Prim a => Ixed (Vector a)Defined in lens-5.3.5 · Control.Lens.At
  • Unbox a => Ixed (Vector a)Defined in lens-5.3.5 · Control.Lens.At
  • (Eq k, Hashable k) => Ixed (HashSet k)Defined in lens-5.3.5 · Control.Lens.At
  • Ix i => Ixed (Array i e)Defined in lens-5.3.5 · Control.Lens.At
    arr ! i ≡ arr ^. ix i
    arr // [(i,e)] ≡ ix i .~ e $ arr
    
  • Eq e => Ixed (e -> a)Defined in lens-5.3.5 · Control.Lens.At
  • Ord k => Ixed (Map k a)Defined in lens-5.3.5 · Control.Lens.At
  • (IArray UArray e, Ix i) => Ixed (UArray i e)Defined in lens-5.3.5 · Control.Lens.At
    arr ! i ≡ arr ^. ix i
    arr // [(i,e)] ≡ ix i .~ e $ arr
    
  • (Eq k, Hashable k) => Ixed (HashMap k a)Defined in lens-5.3.5 · Control.Lens.At
  • a ~ a2 => Ixed (a, a2)Defined in lens-5.3.5 · Control.Lens.At
  • (a ~ a2, a ~ a3) => Ixed (a, a2, a3)Defined in lens-5.3.5 · Control.Lens.At
  • (a ~ a2, a ~ a3, a ~ a4) => Ixed (a, a2, a3, a4)Defined in lens-5.3.5 · Control.Lens.At
  • (a ~ a2, a ~ a3, a ~ a4, a ~ a5) => Ixed (a, a2, a3, a4, a5)Defined in lens-5.3.5 · Control.Lens.At
  • (a ~ a2, a ~ a3, a ~ a4, a ~ a5, a ~ a6) => Ixed (a, a2, a3, a4, a5, a6)Defined in lens-5.3.5 · Control.Lens.At
  • (a ~ a2, a ~ a3, a ~ a4, a ~ a5, a ~ a6, a ~ a7) => Ixed (a, a2, a3, a4, a5, a6, a7)Defined in lens-5.3.5 · Control.Lens.At
  • (a ~ a2, a ~ a3, a ~ a4, a ~ a5, a ~ a6, a ~ a7, a ~ a8) => Ixed (a, a2, a3, a4, a5, a6, a7, a8)Defined in lens-5.3.5 · Control.Lens.At
  • (a ~ a2, a ~ a3, a ~ a4, a ~ a5, a ~ a6, a ~ a7, a ~ a8, a ~ a9) => Ixed (a, a2, a3, a4, a5, a6, a7, a8, a9)Defined in lens-5.3.5 · Control.Lens.At
valueiix :: Ixed m => Index m -> IndexedTraversal' (Index m) m (IxValue m)
#

An indexed version of ix.

Example1 expression
Seq.fromList [a,b,c,d] & iix 2 %@~ f'fromList [a,b,f' 2 c,d]
Example1 expression
Seq.fromList [a,b,c,d] & iix 2 .@~ hfromList [a,b,h 2,d]
Example1 expression
Seq.fromList [a,b,c,d] ^@? iix 2Just (2,c)
Example1 expression
Seq.fromList [] ^@? iix 2Nothing
classclass Contains m where
#

This class provides a simple Lens that lets you view (and modify) information about whether or not a container contains a given Index.

Methods

  • contains :: Index m -> Lens' m Bool
    Example1 expression
    IntSet.fromList [1,2,3,4] ^. contains 3True
    Example1 expression
    IntSet.fromList [1,2,3,4] ^. contains 5False
    Example1 expression
    IntSet.fromList [1,2,3,4] & contains 3 .~ FalsefromList [1,2,4]
Instances3Contains
valueicontains :: Contains m => Index m -> IndexedLens' (Index m) m Bool
#

An indexed version of contains.

Example1 expression
IntSet.fromList [1,2,3,4] ^@. icontains 3(3,True)
Example1 expression
IntSet.fromList [1,2,3,4] ^@. icontains 5(5,False)
Example1 expression
IntSet.fromList [1,2,3,4] & icontains 3 %@~ \i x -> if odd i then not x else xfromList [1,2,4]
Example1 expression
IntSet.fromList [1,2,3,4] & icontains 3 %@~ \i x -> if even i then not x else xfromList [1,2,3,4]
valuecons :: Cons s s a a => a -> s -> s
#

cons an element onto a container.

Example1 expression
cons a [][a]
Example1 expression
cons a [b, c][a,b,c]
Example1 expression
cons a (Seq.fromList [])fromList [a]
Example1 expression
cons a (Seq.fromList [b, c])fromList [a,b,c]
valueuncons :: Cons s s a a => s -> Maybe (a, s)
#

Attempt to extract the left-most element from a container, and a version of the container without that element.

Example1 expression
uncons []Nothing
Example1 expression
uncons [a, b, c]Just (a,[b,c])
value_head :: Cons s s a a => Traversal' s a
#

A Traversal reading and writing to the head of a non-empty container.

Example1 expression
[a,b,c]^? _headJust a
Example1 expression
[a,b,c] & _head .~ d[d,b,c]
Example1 expression
[a,b,c] & _head %~ f[f a,b,c]
Example1 expression
[] & _head %~ f[]
Example1 expression
[1,2,3]^?!_head1
Example1 expression
[]^?_headNothing
Example1 expression
[1,2]^?_headJust 1
Example1 expression
[] & _head .~ 1[]
Example1 expression
[0] & _head .~ 2[2]
Example1 expression
[0,1] & _head .~ 2[2,1]

This isn't limited to lists.

For instance you can also traverse the head of a Seq:

Example1 expression
Seq.fromList [a,b,c,d] & _head %~ ffromList [f a,b,c,d]
Example1 expression
Seq.fromList [] ^? _headNothing
Example1 expression
Seq.fromList [a,b,c,d] ^? _headJust a
_head :: Traversal' [a] a
_head :: Traversal' (Seq a) a
_head :: Traversal' (Vector a) a
value_tail :: Cons s s a a => Traversal' s s
#

A Traversal reading and writing to the tail of a non-empty container.

Example1 expression
[a,b] & _tail .~ [c,d,e][a,c,d,e]
Example1 expression
[] & _tail .~ [a,b][]
Example1 expression
[a,b,c,d,e] & _tail.traverse %~ f[a,f b,f c,f d,f e]
Example1 expression
[1,2] & _tail .~ [3,4,5][1,3,4,5]
Example1 expression
[] & _tail .~ [1,2][]
Example1 expression
[a,b,c]^?_tailJust [b,c]
Example1 expression
[1,2]^?!_tail[2]
Example1 expression
"hello"^._tail"ello"
Example1 expression
""^._tail""

This isn't limited to lists. For instance you can also Control.Traversable.traverse the tail of a Seq.

Example1 expression
Seq.fromList [a,b] & _tail .~ Seq.fromList [c,d,e]fromList [a,c,d,e]
Example1 expression
Seq.fromList [a,b,c] ^? _tailJust (fromList [b,c])
Example1 expression
Seq.fromList [] ^? _tailNothing
_tail :: Traversal' [a] [a]
_tail :: Traversal' (Seq a) (Seq a)
_tail :: Traversal' (Vector a) (Vector a)
value(<<<|~) :: Cons b b a a => LensLike' (Tuple2 b) s b -> a -> s -> (b, s)
#

(<|) a value onto the target of a Lens and return the old result.

When you do not need the result of the operation, (<|~) is more flexible.

value(<<<|=)
  1. :: (MonadState s m, Cons b b a a)
  2. => LensLike (Tuple2 b) s s b b
  3. -> a
  4. -> m b
#

(<|) a value onto the target of a Lens into your Monad's state and return the old result.

When you do not need the result of the operation, (<|=) is more flexible.

classclass Snoc s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

This class provides a way to attach or detach elements on the right side of a structure in a flexible manner.

Methods

Instances12Snoc, …
valuesnoc :: Snoc s s a a => s -> a -> s
#

snoc an element onto a container.

Example1 expression
snoc (Seq.fromList []) afromList [a]
Example1 expression
snoc (Seq.fromList [b, c]) afromList [b,c,a]
Example1 expression
snoc (LazyT.pack "hello") '!'"hello!"
valueunsnoc :: Snoc s s a a => s -> Maybe (s, a)
#

Attempt to extract the right-most element from a container, and a version of the container without that element.

Example1 expression
unsnoc (LazyT.pack "hello!")Just ("hello",'!')
Example1 expression
unsnoc (LazyT.pack "")Nothing
Example1 expression
unsnoc (Seq.fromList [b,c,a])Just (fromList [b,c],a)
Example1 expression
unsnoc (Seq.fromList [])Nothing
value_init :: Snoc s s a a => Traversal' s s
#

A Traversal reading and replacing all but the a last element of a non-empty container.

Example1 expression
[a,b,c,d]^?_initJust [a,b,c]
Example1 expression
[]^?_initNothing
Example1 expression
[a,b] & _init .~ [c,d,e][c,d,e,b]
Example1 expression
[] & _init .~ [a,b][]
Example1 expression
[a,b,c,d] & _init.traverse %~ f[f a,f b,f c,d]
Example1 expression
[1,2,3]^?_initJust [1,2]
Example1 expression
[1,2,3,4]^?!_init[1,2,3]
Example1 expression
"hello"^._init"hell"
Example1 expression
""^._init""
_init :: Traversal' [a] [a]
_init :: Traversal' (Seq a) (Seq a)
_init :: Traversal' (Vector a) (Vector a)
value_last :: Snoc s s a a => Traversal' s a
#

A Traversal reading and writing to the last element of a non-empty container.

Example1 expression
[a,b,c]^?!_lastc
Example1 expression
[]^?_lastNothing
Example1 expression
[a,b,c] & _last %~ f[a,b,f c]
Example1 expression
[1,2]^?_lastJust 2
Example1 expression
[] & _last .~ 1[]
Example1 expression
[0] & _last .~ 2[2]
Example1 expression
[0,1] & _last .~ 2[0,2]

This Traversal is not limited to lists, however. We can also work with other containers, such as a Vector.

Example1 expression
Vector.fromList "abcde" ^? _lastJust 'e'
Example1 expression
Vector.empty ^? _lastNothing
Example1 expression
(Vector.fromList "abcde" & _last .~ 'Q') == Vector.fromList "abcdQ"True
_last :: Traversal' [a] a
_last :: Traversal' (Seq a) a
_last :: Traversal' (Vector a) a
value(<<|>~) :: Snoc b b p p => LensLike' (Tuple2 b) s b -> p -> s -> (b, s)
#

(|>) a value onto the target of a Lens and return the old result.

When you do not need the result of the operation, (|>~) is more flexible.

value(<<|>=)
  1. :: (MonadState s m, Snoc b b p p)
  2. => LensLike (Tuple2 b) s s b b
  3. -> p
  4. -> m b
#

(|>) a value onto the target of a Lens into your Monad's state and return the old result.

When you do not need the result of the operation, (|>=) is more flexible.

valuesimply :: (Optic' p f s a -> r) -> Optic' p f s a -> r
#

This is an adverb that can be used to modify many other Lens combinators to make them require simple lenses, simple traversals, simple prisms or simple isos as input.

classclass AsEmpty a where
#

Methods

  • _Empty :: Prism' a ()
    Example1 expression
    isn't _Empty [1,2,3]True
Instances31AsEmpty, …
datadata (:~:) (a :: k) (b :: k) where
#

Propositional equality. If a :~: b is inhabited by some terminating value, then the type a is the same as the type b. To use this equality in practice, pattern-match on the a :~: b to get out the Refl constructor; in the body of the pattern-match, the compiler knows that a ~ b.

Constructors

Instances15Category, Groupoid, Semigroupoid, TestCoercion, TestEquality, NFData2, …
  • Category (:~:)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.Category
  • Groupoid (:~:)Defined in semigroupoids-6.0.1 · Data.Groupoid
  • Semigroupoid (:~:)Defined in semigroupoids-6.0.1 · Data.Semigroupoid
  • TestCoercion ((:~:) a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Type.Coercion
  • TestEquality ((:~:) a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Type.Equality
  • NFData2 (:~:)Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • NFData1 ((:~:) a)Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • a ~ b => Bounded (a :~: b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Type.Equality
  • a ~ b => Enum (a :~: b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Type.Equality
  • Eq (a :~: b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Type.Equality
  • (a ~ b, Data a) => Data (a :~: b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Ord (a :~: b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Type.Equality
  • a ~ b => Read (a :~: b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Type.Equality
  • Show (a :~: b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Type.Equality
  • NFData (a :~: b)Defined in deepseq-1.5.0.0 · Control.DeepSeq
valuesimple :: p a (f a) -> p a (f a)
#

Composition with this isomorphism is occasionally useful when your Lens, Control.Lens.Traversal.Traversal or Iso has a constraint on an unused argument to force that argument to agree with the type of a used argument and avoid ScopedTypeVariables or other ugliness.

valuefromLeibniz :: (Identical a b a b -> Identical a b s t) -> Equality s t a b
#

Convert a "profunctor lens" form of equality to an equality. Reverses overEquality.

The type should be understood as

fromLeibniz :: (forall p. p a b -> p s t) -> Equality s t a b
valuefromLeibniz' :: (s :~: s -> s :~: a) -> Equality' s a
#

Convert Leibniz equality to equality. Reverses mapEq in Simple cases.

The type should be understood as

fromLeibniz' :: (forall f. f s -> f a) -> Equality' s a
datadata Identical (a :: k) (b :: k1) (s :: k) (t :: k1) where
#

Provides witness that (s ~ a, b ~ t) holds.

Constructors

newtypenewtype ReifiedIndexedFold i s a
#
Instances10Strong, Profunctor, Representable, Sieve, Functor, Alt, …
valuepre :: Getting (First a) s a -> IndexPreservingGetter s (Maybe a)
#

This converts a Fold to a IndexPreservingGetter that returns the first element, if it exists, as a Maybe.

pre :: Getter s a     -> IndexPreservingGetter s (Maybe a)
pre :: Fold s a       -> IndexPreservingGetter s (Maybe a)
pre :: Traversal' s a -> IndexPreservingGetter s (Maybe a)
pre :: Lens' s a      -> IndexPreservingGetter s (Maybe a)
pre :: Iso' s a       -> IndexPreservingGetter s (Maybe a)
pre :: Prism' s a     -> IndexPreservingGetter s (Maybe a)
valueipre
  1. :: IndexedGetting i (First (i, a)) s a
  2. -> IndexPreservingGetter s (Maybe (i, a))
#

This converts an IndexedFold to an IndexPreservingGetter that returns the first index and element, if they exist, as a Maybe.

ipre :: IndexedGetter i s a     -> IndexPreservingGetter s (Maybe (i, a))
ipre :: IndexedFold i s a       -> IndexPreservingGetter s (Maybe (i, a))
ipre :: IndexedTraversal' i s a -> IndexPreservingGetter s (Maybe (i, a))
ipre :: IndexedLens' i s a      -> IndexPreservingGetter s (Maybe (i, a))
valuepreview :: MonadReader s m => Getting (First a) s a -> m (Maybe a)
#

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 ≡ preview folded
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 :: Getter s a     -> s -> Maybe a
preview :: Fold s a       -> s -> Maybe a
preview :: Lens' s a      -> s -> Maybe a
preview :: Iso' s a       -> s -> Maybe a
preview :: Traversal' s a -> s -> Maybe a
preview :: MonadReader s m => Getter s a     -> m (Maybe a)
preview :: MonadReader s m => Fold s a       -> m (Maybe a)
preview :: MonadReader s m => Lens' s a      -> m (Maybe a)
preview :: MonadReader s m => Iso' s a       -> m (Maybe a)
preview :: MonadReader s m => Traversal' s a -> m (Maybe a)

valueipreview
  1. :: MonadReader s m
  2. => IndexedGetting i (First (i, a)) s a
  3. -> m (Maybe (i, a))
#

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 :: IndexedGetter i s a     -> s -> Maybe (i, a)
ipreview :: IndexedFold i 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 :: MonadReader s m => IndexedGetter s a     -> m (Maybe (i, a))
ipreview :: MonadReader s m => IndexedFold s a       -> m (Maybe (i, a))
ipreview :: MonadReader s m => IndexedLens' s a      -> m (Maybe (i, a))
ipreview :: MonadReader s m => IndexedTraversal' s a -> m (Maybe (i, a))
valueipreviews
  1. :: MonadReader s m
  2. => IndexedGetting i (First r) s a
  3. -> i -> a -> r
  4. -> m (Maybe r)
#

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 :: IndexedGetter i s a     -> (i -> a -> r) -> s -> Maybe r
ipreviews :: IndexedFold i s a       -> (i -> a -> r) -> s -> Maybe r
ipreviews :: IndexedLens' i s a      -> (i -> a -> r) -> s -> Maybe r
ipreviews :: IndexedTraversal' i s a -> (i -> a -> r) -> s -> Maybe r

However, it may be useful to think of its full generality when working with a Monad transformer stack:

ipreviews :: MonadReader s m => IndexedGetter i s a     -> (i -> a -> r) -> m (Maybe r)
ipreviews :: MonadReader s m => IndexedFold i s a       -> (i -> a -> r) -> m (Maybe r)
ipreviews :: MonadReader s m => IndexedLens' i s a      -> (i -> a -> r) -> m (Maybe r)
ipreviews :: MonadReader s m => IndexedTraversal' i s a -> (i -> a -> r) -> m (Maybe r)
valuepreuses
  1. :: MonadState s m
  2. => Getting (First r) s a
  3. -> a -> r
  4. -> m (Maybe r)
#

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 :: MonadState s m => Getter s a     -> (a -> r) -> m (Maybe r)
preuses :: MonadState s m => Fold s a       -> (a -> r) -> m (Maybe r)
preuses :: MonadState s m => Lens' s a      -> (a -> r) -> m (Maybe r)
preuses :: MonadState s m => Iso' s a       -> (a -> r) -> m (Maybe r)
preuses :: MonadState s m => Traversal' s a -> (a -> r) -> m (Maybe r)
valueipreuse
  1. :: MonadState s m
  2. => IndexedGetting i (First (i, a)) s a
  3. -> m (Maybe (i, a))
#

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 :: MonadState s m => IndexedGetter i s a     -> m (Maybe (i, a))
ipreuse :: MonadState s m => IndexedFold i s a       -> m (Maybe (i, a))
ipreuse :: MonadState s m => IndexedLens' i s a      -> m (Maybe (i, a))
ipreuse :: MonadState s m => IndexedTraversal' i s a -> m (Maybe (i, a))
valueipreuses
  1. :: MonadState s m
  2. => IndexedGetting i (First r) s a
  3. -> i -> a -> r
  4. -> m (Maybe r)
#

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 :: MonadState s m => IndexedGetter i s a     -> (i -> a -> r) -> m (Maybe r)
ipreuses :: MonadState s m => IndexedFold i s a       -> (i -> a -> r) -> m (Maybe r)
ipreuses :: MonadState s m => IndexedLens' i s a      -> (i -> a -> r) -> m (Maybe r)
ipreuses :: MonadState s m => IndexedTraversal' i s a -> (i -> a -> r) -> m (Maybe r)
valuehas :: Getting Any s a -> s -> Bool
#

Check to see if this Fold or Traversal matches 1 or more entries.

Example1 expression
has (element 0) []False
Example1 expression
has _Left (Left 12)True
Example1 expression
has _Right (Left 12)False

This will always return True for a Lens or Getter.

Example1 expression
has _1 ("hello","world")True
has :: Getter s a     -> s -> Bool
has :: Fold s a       -> s -> Bool
has :: Iso' s a       -> s -> Bool
has :: Lens' s a      -> s -> Bool
has :: Traversal' s a -> s -> Bool
valuehasn't :: Getting All s a -> s -> Bool
#

Check to see if this Fold or Traversal has no matches.

Example1 expression
hasn't _Left (Right 12)True
Example1 expression
hasn't _Left (Left 12)False
valuefolding :: Foldable f => (s -> f a) -> Fold s a
#

Obtain a Fold by lifting an operation that returns a Foldable result.

This can be useful to lift operations from Data.List and elsewhere into a Fold.

Example1 expression
[1,2,3,4]^..folding reverse[4,3,2,1]
valuefolded :: Foldable f => IndexedFold Int (f a) a
#

Obtain a Fold from any Foldable indexed by ordinal position.

Example1 expression
Just 3^..folded[3]
Example1 expression
Nothing^..folded[]
Example1 expression
[(1,2),(3,4)]^..folded.both[1,2,3,4]
valueunfolded :: (b -> Maybe (a, b)) -> Fold b a
#

Build a Fold that unfolds its values from a seed.

Prelude.unfoldr ≡ toListOf . unfolded
Example1 expression
10^..unfolded (\b -> if b == 0 then Nothing else Just (b, b-1))[10,9,8,7,6,5,4,3,2,1]
valuefiltered :: (Choice p, Applicative f) => (a -> Bool) -> Optic' p f a a
#

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 = filtered even the second Traversal law is violated:

over evens succ . over evens succ /= over evens (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!

Example1 expression
[1..10]^..folded.filtered even[2,4,6,8,10]

This will preserve an index if it is present.

valuefilteredBy
  1. :: (Indexable i p, Applicative f)
  2. => Getting (First i) a i
  3. -> p a (f a)
  4. -> a
  5. -> f a
#

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).

Example1 expression
[(Just 2, 3), (Nothing, 4)] & mapped . filteredBy (_1 . _Just) <. _2 %@~ (*) :: [(Maybe Int, Int)][(Just 2,6),(Nothing,4)]
filteredBy :: Fold a 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!

valuebackwards
  1. :: (Profunctor p, Profunctor q)
  2. => Optical p q (Backwards f) s t a b
  3. -> Optical p q f s t a b
#

This allows you to Control.Traversable.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 Control.Lens.Isomorphic.from. Similarly, to write back through an Prism, you want to use re.

valuecycled :: Apply f => LensLike f s t a b -> LensLike f s t a b
#

Transform a non-empty Fold into a Fold1 that loops over its elements over and over.

Example1 expression
timingOut $ [1,2,3]^..taking 7 (cycled traverse)[1,2,3,1,2,3,1]
cycled :: Fold1 s a -> Fold1 s a
valuetakingWhile
  1. :: (Conjoined p, Applicative f)
  2. => a -> Bool
  3. -> Over p (TakingWhile p f a a) s t a a
  4. -> Over p f s t a a
#

Obtain a Fold by taking elements from another Fold, Lens, Iso, Getter or Traversal while a predicate holds.

takeWhile p ≡ toListOf (takingWhile p folded)
Example1 expression
timingOut $ toListOf (takingWhile (<=3) folded) [1..][1,2,3]
takingWhile :: (a -> Bool) -> Fold s a                         -> Fold s a
takingWhile :: (a -> Bool) -> Getter s a                       -> Fold s a
takingWhile :: (a -> Bool) -> Traversal' s a                   -> Fold s a -- * See note below
takingWhile :: (a -> Bool) -> Lens' s a                        -> Fold s a -- * See note below
takingWhile :: (a -> Bool) -> Prism' s a                       -> Fold s a -- * See note below
takingWhile :: (a -> Bool) -> Iso' s a                         -> Fold s a -- * See note below
takingWhile :: (a -> Bool) -> IndexedTraversal' i s a          -> IndexedFold i s a -- * See note below
takingWhile :: (a -> Bool) -> IndexedLens' i s a               -> IndexedFold i s a -- * See note below
takingWhile :: (a -> Bool) -> IndexedFold i s a                -> IndexedFold i s a
takingWhile :: (a -> Bool) -> IndexedGetter i s a              -> IndexedFold i 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.

valuedroppingWhile
  1. :: (Conjoined p, Profunctor q, Applicative f)
  2. => a -> Bool
  3. -> Optical p q (Compose (State Bool) f) s t a a
  4. -> Optical p q f s t a a
#

Obtain a Fold by dropping elements from another Fold, Lens, Iso, Getter or Traversal while a predicate holds.

dropWhile p ≡ toListOf (droppingWhile p folded)
Example1 expression
toListOf (droppingWhile (<=3) folded) [1..6][4,5,6]
Example1 expression
toListOf (droppingWhile (<=3) folded) [1,6,1][6,1]
droppingWhile :: (a -> Bool) -> Fold s a                         -> Fold s a
droppingWhile :: (a -> Bool) -> Getter s a                       -> Fold s a
droppingWhile :: (a -> Bool) -> Traversal' s a                   -> Fold s a                -- see notes
droppingWhile :: (a -> Bool) -> Lens' s a                        -> Fold s a                -- see notes
droppingWhile :: (a -> Bool) -> Prism' s a                       -> Fold s a                -- see notes
droppingWhile :: (a -> Bool) -> Iso' s a                         -> Fold s a                -- see notes
droppingWhile :: (a -> Bool) -> IndexPreservingTraversal' s a    -> IndexPreservingFold s a -- see notes
droppingWhile :: (a -> Bool) -> IndexPreservingLens' s a         -> IndexPreservingFold s a -- see notes
droppingWhile :: (a -> Bool) -> IndexPreservingGetter s a        -> IndexPreservingFold s a
droppingWhile :: (a -> Bool) -> IndexPreservingFold s a          -> IndexPreservingFold s a
droppingWhile :: (a -> Bool) -> IndexedTraversal' i s a          -> IndexedFold i s a       -- see notes
droppingWhile :: (a -> Bool) -> IndexedLens' i s a               -> IndexedFold i s a       -- see notes
droppingWhile :: (a -> Bool) -> IndexedGetter i s a              -> IndexedFold i s a
droppingWhile :: (a -> Bool) -> IndexedFold i s a                -> IndexedFold i 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, droppingWhile p t may not be lawful, but (dropping 1 . droppingWhile p) t is. For example:

Example2 expressions
let l  :: Traversal' [Int] Int; l  = droppingWhile (<= 1) traverselet 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):

Example2 expressions
[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:

Example2 expressions
[1,2,3] & l' .~ 0 & l' .~ 4[1,2,4][1,2,3] & l' .~ 4[1,2,4]

A Fold over the individual words of a String.

worded :: Fold String String
worded :: Traversal' String String
worded :: IndexedFold Int String String
worded :: IndexedTraversal' Int String String

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).

A Fold over the individual lines of a String.

lined :: Fold String String
lined :: Traversal' String String
lined :: IndexedFold Int String String
lined :: IndexedTraversal' Int String String

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.

valuefoldMapOf :: Getting r s a -> (a -> r) -> s -> r
#

Map each part of a structure viewed through a Lens, Getter, Fold or Traversal to a monoid and combine the results.

Example1 expression
foldMapOf (folded . both . _Just) Sum [(Just 21, Just 21)]Sum {getSum = 42}
foldMap = foldMapOf folded
foldMapOf ≡ views
ifoldMapOf l = foldMapOf l . Indexed
foldMapOf ::                Getter s a      -> (a -> r) -> s -> r
foldMapOf :: Monoid r    => Fold s a        -> (a -> r) -> s -> r
foldMapOf :: Semigroup r => Fold1 s a       -> (a -> r) -> s -> r
foldMapOf ::                Lens' s a       -> (a -> r) -> s -> r
foldMapOf ::                Iso' s a        -> (a -> r) -> s -> r
foldMapOf :: Monoid r    => Traversal' s a  -> (a -> r) -> s -> r
foldMapOf :: Semigroup r => Traversal1' s a -> (a -> r) -> s -> r
foldMapOf :: Monoid r    => Prism' s a      -> (a -> r) -> s -> r
foldMapOf :: Getting r s a -> (a -> r) -> s -> r
valuefoldOf :: Getting a s a -> s -> a
#

Combine the elements of a structure viewed through a Lens, Getter, Fold or Traversal using a monoid.

Example1 expression
foldOf (folded.folded) [[Sum 1,Sum 4],[Sum 8, Sum 8],[Sum 21]]Sum {getSum = 42}
fold = foldOf folded
foldOf ≡ view
foldOf ::             Getter s m     -> s -> m
foldOf :: Monoid m => Fold s m       -> s -> m
foldOf ::             Lens' s m      -> s -> m
foldOf ::             Iso' s m       -> s -> m
foldOf :: Monoid m => Traversal' s m -> s -> m
foldOf :: Monoid m => Prism' s m     -> s -> m
valuefoldrOf :: Getting (Endo r) s a -> (a -> r -> r) -> r -> s -> r
#

Right-associative fold of parts of a structure that are viewed through a Lens, Getter, Fold or Traversal.

foldr ≡ foldrOf folded
foldrOf :: Getter s a     -> (a -> r -> r) -> r -> s -> r
foldrOf :: Fold s a       -> (a -> r -> r) -> r -> s -> r
foldrOf :: Lens' s a      -> (a -> r -> r) -> r -> s -> r
foldrOf :: Iso' s a       -> (a -> r -> r) -> r -> s -> r
foldrOf :: Traversal' s a -> (a -> r -> r) -> r -> s -> r
foldrOf :: Prism' s a     -> (a -> r -> r) -> r -> s -> r
ifoldrOf l ≡ foldrOf l . Indexed
foldrOf :: Getting (Endo r) s a -> (a -> r -> r) -> r -> s -> r
valuefoldlOf :: Getting (Dual (Endo r)) s a -> (r -> a -> r) -> r -> s -> r
#

Left-associative fold of the parts of a structure that are viewed through a Lens, Getter, Fold or Traversal.

foldl ≡ foldlOf folded
foldlOf :: Getter s a     -> (r -> a -> r) -> r -> s -> r
foldlOf :: Fold s a       -> (r -> a -> r) -> r -> s -> r
foldlOf :: Lens' s a      -> (r -> a -> r) -> r -> s -> r
foldlOf :: Iso' s a       -> (r -> a -> r) -> r -> s -> r
foldlOf :: Traversal' s a -> (r -> a -> r) -> r -> s -> r
foldlOf :: Prism' s a     -> (r -> a -> r) -> r -> s -> r
valuealtOf :: Applicative f => Getting (Alt f a) s a -> s -> f a
#

Calls pure on the target of a Lens, Getter, or Iso.

Calls pure on the targets of a Traversal, Fold, or Prism, and combines them with <|> (or empty if none). Intuitively, it collects targets into an Alternative until the container fills up or it runs out of targets, whichever comes first.

Generalizes toListOf and (^?).

Example2 expressions
altOf both ("hello", "world") :: [String]["hello","world"]altOf both ("hello", "world") :: Maybe StringJust "hello"
altOf :: Applicative f => Lens' s a      -> s -> f a
altOf :: Applicative f => Getter s a     -> s -> f a
altOf :: Applicative f => Iso' s a       -> s -> f a

altOf :: Alternative f => Traversal' s a -> s -> f a
altOf :: Alternative f => Fold s a       -> s -> f a
altOf :: Alternative f => Prism' s a     -> s -> f a
valueanyOf :: Getting Any s a -> (a -> Bool) -> s -> Bool
#

Returns True if any target of a Fold satisfies a predicate.

Example3 expressions
anyOf both (=='x') ('x','y')Trueimport Data.Data.LensanyOf biplate (== "world") (((),2::Int),"hello",("world",11::Int))True
any ≡ anyOf folded
ianyOf l ≡ anyOf l . Indexed
anyOf :: Getter s a     -> (a -> Bool) -> s -> Bool
anyOf :: Fold s a       -> (a -> Bool) -> s -> Bool
anyOf :: Lens' s a      -> (a -> Bool) -> s -> Bool
anyOf :: Iso' s a       -> (a -> Bool) -> s -> Bool
anyOf :: Traversal' s a -> (a -> Bool) -> s -> Bool
anyOf :: Prism' s a     -> (a -> Bool) -> s -> Bool
valueallOf :: Getting All s a -> (a -> Bool) -> s -> Bool
#

Returns True if every target of a Fold satisfies a predicate.

Example2 expressions
allOf both (>=3) (4,5)TrueallOf folded (>=2) [1..10]False
all ≡ allOf folded
iallOf l = allOf l . Indexed
allOf :: Getter s a     -> (a -> Bool) -> s -> Bool
allOf :: Fold s a       -> (a -> Bool) -> s -> Bool
allOf :: Lens' s a      -> (a -> Bool) -> s -> Bool
allOf :: Iso' s a       -> (a -> Bool) -> s -> Bool
allOf :: Traversal' s a -> (a -> Bool) -> s -> Bool
allOf :: Prism' s a     -> (a -> Bool) -> s -> Bool
valuenoneOf :: Getting Any s a -> (a -> Bool) -> s -> Bool
#

Returns True only if no targets of a Fold satisfy a predicate.

Example2 expressions
noneOf each (is _Nothing) (Just 3, Just 4, Just 5)TruenoneOf (folded.folded) (<10) [[13,99,20],[3,71,42]]False
inoneOf l = noneOf l . Indexed
noneOf :: Getter s a     -> (a -> Bool) -> s -> Bool
noneOf :: Fold s a       -> (a -> Bool) -> s -> Bool
noneOf :: Lens' s a      -> (a -> Bool) -> s -> Bool
noneOf :: Iso' s a       -> (a -> Bool) -> s -> Bool
noneOf :: Traversal' s a -> (a -> Bool) -> s -> Bool
noneOf :: Prism' s a     -> (a -> Bool) -> s -> Bool
valueproductOf :: Num a => Getting (Endo (Endo a)) s a -> s -> a
#

Calculate the Product of every number targeted by a Fold.

Example2 expressions
productOf both (4,5)20productOf folded [1,2,3,4,5]120
product ≡ productOf folded

This operation may be more strict than you would expect. If you want a lazier version use ala Product . foldMapOf

productOf :: Num a => Getter s a     -> s -> a
productOf :: Num a => Fold s a       -> s -> a
productOf :: Num a => Lens' s a      -> s -> a
productOf :: Num a => Iso' s a       -> s -> a
productOf :: Num a => Traversal' s a -> s -> a
productOf :: Num a => Prism' s a     -> s -> a
valuesumOf :: Num a => Getting (Endo (Endo a)) s a -> s -> a
#

Calculate the Sum of every number targeted by a Fold.

Example5 expressions
sumOf both (5,6)11sumOf folded [1,2,3,4]10sumOf (folded.both) [(1,2),(3,4)]10import Data.Data.LenssumOf biplate [(1::Int,[]),(2,[(3::Int,4::Int)])] :: Int10
sum ≡ sumOf folded

This operation may be more strict than you would expect. If you want a lazier version use ala Sum . foldMapOf

sumOf _1 :: Num a => (a, b) -> a
sumOf (folded . _1) :: (Foldable f, Num a) => f (a, b) -> a
sumOf :: Num a => Getter s a     -> s -> a
sumOf :: Num a => Fold s a       -> s -> a
sumOf :: Num a => Lens' s a      -> s -> a
sumOf :: Num a => Iso' s a       -> s -> a
sumOf :: Num a => Traversal' s a -> s -> a
sumOf :: Num a => Prism' s a     -> s -> a
valuetraverseOf_
  1. :: Functor f
  2. => Getting (Traversed r f) s a
  3. -> a -> f r
  4. -> s
  5. -> f ()
#

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.

Example1 expression
traverseOf_ both putStrLn ("hello","world")helloworld
traverse_ ≡ traverseOf_ folded
traverseOf_ _2 :: Functor f => (c -> f r) -> (d, c) -> f ()
traverseOf_ _Left :: Applicative f => (a -> f b) -> Either a 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_ :: Functor f     => Getter s a     -> (a -> f r) -> s -> f ()
traverseOf_ :: Applicative f => Fold s a       -> (a -> f r) -> s -> f ()
traverseOf_ :: Functor f     => Lens' s a      -> (a -> f r) -> s -> f ()
traverseOf_ :: Functor f     => Iso' s a       -> (a -> f r) -> s -> f ()
traverseOf_ :: Applicative f => Traversal' s a -> (a -> f r) -> s -> f ()
traverseOf_ :: Applicative f => Prism' s a     -> (a -> f r) -> s -> f ()
valueforOf_
  1. :: Functor f
  2. => Getting (Traversed r f) s a
  3. -> s
  4. -> a -> f r
  5. -> f ()
#

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
Example1 expression
forOf_ both ("hello","world") putStrLnhelloworld

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_ :: Functor f     => Getter s a     -> s -> (a -> f r) -> f ()
forOf_ :: Applicative f => Fold s a       -> s -> (a -> f r) -> f ()
forOf_ :: Functor f     => Lens' s a      -> s -> (a -> f r) -> f ()
forOf_ :: Functor f     => Iso' s a       -> s -> (a -> f r) -> f ()
forOf_ :: Applicative f => Traversal' s a -> s -> (a -> f r) -> f ()
forOf_ :: Applicative f => Prism' s a     -> s -> (a -> f r) -> f ()
valuesequenceAOf_ :: Functor f => Getting (Traversed a f) s (f a) -> s -> f ()
#

Evaluate each action in observed by a Fold on a structure from left to right, ignoring the results.

sequenceA_ ≡ sequenceAOf_ folded
Example1 expression
sequenceAOf_ both (putStrLn "hello",putStrLn "world")helloworld
sequenceAOf_ :: Functor f     => Getter s (f a)     -> s -> f ()
sequenceAOf_ :: Applicative f => Fold s (f a)       -> s -> f ()
sequenceAOf_ :: Functor f     => Lens' s (f a)      -> s -> f ()
sequenceAOf_ :: Functor f     => Iso' s (f a)       -> s -> f ()
sequenceAOf_ :: Applicative f => Traversal' s (f a) -> s -> f ()
sequenceAOf_ :: Applicative f => Prism' s (f a)     -> s -> f ()
valuetraverse1Of_
  1. :: Functor f
  2. => Getting (TraversedF r f) s a
  3. -> a -> f r
  4. -> s
  5. -> f ()
#

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.

Example1 expression
traverse1Of_ both1 (\ks -> Map.fromList [ (k, ()) | k <- ks ]) ("abc", "bcd")fromList [('b',()),('c',())]
traverse1Of_ :: Apply f => Fold1 s a -> (a -> f r) -> s -> f ()
valuemapMOf_ :: Monad m => Getting (Sequenced r m) s a -> (a -> m r) -> s -> m ()
#

Map each target of a Fold on a structure to a monadic action, evaluate these actions from left to right, and ignore the results.

Example1 expression
mapMOf_ both putStrLn ("hello","world")helloworld
Data.Foldable.mapM_ ≡ mapMOf_ folded
mapMOf_ :: Monad m => Getter s a     -> (a -> m r) -> s -> m ()
mapMOf_ :: Monad m => Fold s a       -> (a -> m r) -> s -> m ()
mapMOf_ :: Monad m => Lens' s a      -> (a -> m r) -> s -> m ()
mapMOf_ :: Monad m => Iso' s a       -> (a -> m r) -> s -> m ()
mapMOf_ :: Monad m => Traversal' s a -> (a -> m r) -> s -> m ()
mapMOf_ :: Monad m => Prism' s a     -> (a -> m r) -> s -> m ()
valueforMOf_ :: Monad m => Getting (Sequenced r m) s a -> s -> (a -> m r) -> m ()
#

forMOf_ is mapMOf_ with two of its arguments flipped.

Example1 expression
forMOf_ both ("hello","world") putStrLnhelloworld
Data.Foldable.forM_ ≡ forMOf_ folded
forMOf_ :: Monad m => Getter s a     -> s -> (a -> m r) -> m ()
forMOf_ :: Monad m => Fold s a       -> s -> (a -> m r) -> m ()
forMOf_ :: Monad m => Lens' s a      -> s -> (a -> m r) -> m ()
forMOf_ :: Monad m => Iso' s a       -> s -> (a -> m r) -> m ()
forMOf_ :: Monad m => Traversal' s a -> s -> (a -> m r) -> m ()
forMOf_ :: Monad m => Prism' s a     -> s -> (a -> m r) -> m ()
valuesequenceOf_ :: Monad m => Getting (Sequenced a m) s (m a) -> s -> m ()
#

Evaluate each monadic action referenced by a Fold on the structure from left to right, and ignore the results.

Example1 expression
sequenceOf_ both (putStrLn "hello",putStrLn "world")helloworld
Data.Foldable.sequence_ ≡ sequenceOf_ folded
sequenceOf_ :: Monad m => Getter s (m a)     -> s -> m ()
sequenceOf_ :: Monad m => Fold s (m a)       -> s -> m ()
sequenceOf_ :: Monad m => Lens' s (m a)      -> s -> m ()
sequenceOf_ :: Monad m => Iso' s (m a)       -> s -> m ()
sequenceOf_ :: Monad m => Traversal' s (m a) -> s -> m ()
sequenceOf_ :: Monad m => Prism' s (m a)     -> s -> m ()
valueasumOf :: Alternative f => Getting (Endo (f a)) s (f a) -> s -> f a
#

The sum of a collection of actions, generalizing concatOf.

Example1 expression
asumOf both ("hello","world")"helloworld"
Example1 expression
asumOf each (Nothing, Just "hello", Nothing)Just "hello"
asum ≡ asumOf folded
asumOf :: Alternative f => Getter s (f a)     -> s -> f a
asumOf :: Alternative f => Fold s (f a)       -> s -> f a
asumOf :: Alternative f => Lens' s (f a)      -> s -> f a
asumOf :: Alternative f => Iso' s (f a)       -> s -> f a
asumOf :: Alternative f => Traversal' s (f a) -> s -> f a
asumOf :: Alternative f => Prism' s (f a)     -> s -> f a
valuemsumOf :: MonadPlus m => Getting (Endo (m a)) s (m a) -> s -> m a
#

The sum of a collection of actions, generalizing concatOf.

Example1 expression
msumOf both ("hello","world")"helloworld"
Example1 expression
msumOf each (Nothing, Just "hello", Nothing)Just "hello"
msum ≡ msumOf folded
msumOf :: MonadPlus m => Getter s (m a)     -> s -> m a
msumOf :: MonadPlus m => Fold s (m a)       -> s -> m a
msumOf :: MonadPlus m => Lens' s (m a)      -> s -> m a
msumOf :: MonadPlus m => Iso' s (m a)       -> s -> m a
msumOf :: MonadPlus m => Traversal' s (m a) -> s -> m a
msumOf :: MonadPlus m => Prism' s (m a)     -> s -> m a
valueconcatMapOf :: Getting [r] s a -> (a -> [r]) -> s -> [r]
#

Map a function over all the targets of a Fold of a container and concatenate the resulting lists.

Example1 expression
concatMapOf both (\x -> [x, x + 1]) (1,3)[1,2,3,4]
concatMap ≡ concatMapOf folded
concatMapOf :: Getter s a     -> (a -> [r]) -> s -> [r]
concatMapOf :: Fold s 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]
valuenotElemOf :: Eq a => Getting All s a -> a -> s -> Bool
#

Does the element not occur anywhere within a given Fold of the structure?

Example1 expression
notElemOf each 'd' ('a','b','c')True
Example1 expression
notElemOf each 'a' ('a','b','c')False
notElem ≡ notElemOf folded
notElemOf :: Eq a => Getter s a     -> a -> s -> Bool
notElemOf :: Eq a => Fold s a       -> a -> s -> Bool
notElemOf :: Eq a => Iso' s a       -> a -> s -> Bool
notElemOf :: Eq a => Lens' s a      -> a -> s -> Bool
notElemOf :: Eq a => Traversal' s a -> a -> s -> Bool
notElemOf :: Eq a => Prism' s a     -> a -> s -> Bool
valuelengthOf :: Getting (Endo (Endo Int)) s a -> s -> Int
#

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 ≡ lengthOf folded
Example1 expression
lengthOf _1 ("hello",())1
Example1 expression
lengthOf traverse [1..10]10
Example1 expression
lengthOf (traverse.traverse) [[1,2],[3,4],[5,6]]6
lengthOf (folded . folded) :: (Foldable f, Foldable g) => f (g a) -> Int
lengthOf :: Getter s a     -> s -> Int
lengthOf :: Fold s a       -> s -> Int
lengthOf :: Lens' s a      -> s -> Int
lengthOf :: Iso' s a       -> s -> Int
lengthOf :: Traversal' s a -> s -> Int
valuenullOf :: Getting All s a -> s -> Bool
#

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 ≡ nullOf folded

This may be rather inefficient compared to the null check of many containers.

Example1 expression
nullOf _1 (1,2)False
Example1 expression
nullOf ignored ()True
Example1 expression
nullOf traverse []True
Example1 expression
nullOf (element 20) [1..10]True
nullOf (folded . _1 . folded) :: (Foldable f, Foldable g) => f (g a, b) -> Bool
nullOf :: Getter s a     -> s -> Bool
nullOf :: Fold s a       -> s -> Bool
nullOf :: Iso' s a       -> s -> Bool
nullOf :: Lens' s a      -> s -> Bool
nullOf :: Traversal' s a -> s -> Bool
valuenotNullOf :: Getting Any s a -> s -> Bool
#

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 ≡ notNullOf folded

This may be rather inefficient compared to the not . null check of many containers.

Example1 expression
notNullOf _1 (1,2)True
Example1 expression
notNullOf traverse [1..10]True
Example1 expression
notNullOf folded []False
Example1 expression
notNullOf (element 20) [1..10]False
notNullOf (folded . _1 . folded) :: (Foldable f, Foldable g) => f (g a, b) -> Bool
notNullOf :: Getter s a     -> s -> Bool
notNullOf :: Fold s a       -> s -> Bool
notNullOf :: Iso' s a       -> s -> Bool
notNullOf :: Lens' s a      -> s -> Bool
notNullOf :: Traversal' s a -> s -> Bool
valuefirstOf :: Getting (Leftmost a) s a -> s -> Maybe a
#

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 preview or ^?' 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.

Example1 expression
firstOf traverse [1..10]Just 1
Example1 expression
firstOf both (1,2)Just 1
Example1 expression
firstOf ignored ()Nothing
firstOf :: Getter s a     -> s -> Maybe a
firstOf :: Fold s a       -> s -> Maybe a
firstOf :: Lens' s a      -> s -> Maybe a
firstOf :: Iso' s a       -> s -> Maybe a
firstOf :: Traversal' s a -> s -> Maybe a
valuefirst1Of :: Getting (First a) s a -> s -> a
#

Retrieve the First entry of a Fold1 or Traversal1 or the result from a Getter or Lens.

Example1 expression
first1Of traverse1 (1 :| [2..10])1
Example1 expression
first1Of both1 (1,2)1

Note: this is different from ^..

Example1 expression
first1Of traverse1 ([1,2] :| [[3,4],[5,6]])[1,2]
Example1 expression
([1,2] :| [[3,4],[5,6]]) ^. traverse1[1,2,3,4,5,6]
first1Of :: Getter s a      -> s -> a
first1Of :: Fold1 s a       -> s -> a
first1Of :: Lens' s a       -> s -> a
first1Of :: Iso' s a        -> s -> a
first1Of :: Traversal1' s a -> s -> a
valuelastOf :: Getting (Rightmost a) s a -> s -> Maybe a
#

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 ala Last . foldMapOf and gives you back access to the outermost Just constructor more quickly, but may have worse constant factors.

Example1 expression
lastOf traverse [1..10]Just 10
Example1 expression
lastOf both (1,2)Just 2
Example1 expression
lastOf ignored ()Nothing
lastOf :: Getter s a     -> s -> Maybe a
lastOf :: Fold s a       -> s -> Maybe a
lastOf :: Lens' s a      -> s -> Maybe a
lastOf :: Iso' s a       -> s -> Maybe a
lastOf :: Traversal' s a -> s -> Maybe a
valuemaximumOf :: Ord a => Getting (Endo (Endo (Maybe a))) s a -> s -> Maybe a
#

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.

Example1 expression
maximumOf traverse [1..10]Just 10
Example1 expression
maximumOf traverse []Nothing
Example1 expression
maximumOf (folded.filtered even) [1,4,3,6,7,9,2]Just 6
maximum ≡ fromMaybe (error "empty") . maximumOf folded

In the interest of efficiency, This operation has semantics more strict than strictly necessary. rmap getMax (foldMapOf l Max) has lazier semantics but could leak memory.

maximumOf :: Ord a => Getter s a     -> s -> Maybe a
maximumOf :: Ord a => Fold s a       -> s -> Maybe a
maximumOf :: Ord a => Iso' s a       -> s -> Maybe a
maximumOf :: Ord a => Lens' s a      -> s -> Maybe a
maximumOf :: Ord a => Traversal' s a -> s -> Maybe a
valueminimumOf :: Ord a => Getting (Endo (Endo (Maybe a))) s a -> s -> Maybe a
#

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.

Example1 expression
minimumOf traverse [1..10]Just 1
Example1 expression
minimumOf traverse []Nothing
Example1 expression
minimumOf (folded.filtered even) [1,4,3,6,7,9,2]Just 2
minimum ≡ fromMaybe (error "empty") . minimumOf folded

In the interest of efficiency, This operation has semantics more strict than strictly necessary. rmap getMin (foldMapOf l Min) has lazier semantics but could leak memory.

minimumOf :: Ord a => Getter s a     -> s -> Maybe a
minimumOf :: Ord a => Fold s a       -> s -> Maybe a
minimumOf :: Ord a => Iso' s a       -> s -> Maybe a
minimumOf :: Ord a => Lens' s a      -> s -> Maybe a
minimumOf :: Ord a => Traversal' s a -> s -> Maybe a
valuemaximumByOf
  1. :: Getting (Endo (Endo (Maybe a))) s a
  2. -> a -> a -> Ordering
  3. -> s
  4. -> Maybe a
#

Obtain the maximum element (if any) targeted by a Fold, Traversal, Lens, Iso, or Getter according to a user supplied Ordering.

Example1 expression
maximumByOf traverse (compare `on` length) ["mustard","relish","ham"]Just "mustard"

In the interest of efficiency, This operation has semantics more strict than strictly necessary.

maximumBy cmp ≡ fromMaybe (error "empty") . maximumByOf folded cmp
maximumByOf :: Getter s a     -> (a -> a -> Ordering) -> s -> Maybe a
maximumByOf :: Fold s a       -> (a -> a -> Ordering) -> s -> Maybe a
maximumByOf :: Iso' s a       -> (a -> a -> Ordering) -> s -> Maybe a
maximumByOf :: Lens' s a      -> (a -> a -> Ordering) -> s -> Maybe a
maximumByOf :: Traversal' s a -> (a -> a -> Ordering) -> s -> Maybe a
valueminimumByOf
  1. :: Getting (Endo (Endo (Maybe a))) s a
  2. -> a -> a -> Ordering
  3. -> s
  4. -> Maybe a
#

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.

Example1 expression
minimumByOf traverse (compare `on` length) ["mustard","relish","ham"]Just "ham"
minimumBy cmp ≡ fromMaybe (error "empty") . minimumByOf folded cmp
minimumByOf :: Getter s a     -> (a -> a -> Ordering) -> s -> Maybe a
minimumByOf :: Fold s a       -> (a -> a -> Ordering) -> s -> Maybe a
minimumByOf :: Iso' s a       -> (a -> a -> Ordering) -> s -> Maybe a
minimumByOf :: Lens' s a      -> (a -> a -> Ordering) -> s -> Maybe a
minimumByOf :: Traversal' s a -> (a -> a -> Ordering) -> s -> Maybe a
valuefindOf :: Getting (Endo (Maybe a)) s a -> (a -> Bool) -> s -> Maybe a
#

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.

Example1 expression
findOf each even (1,3,4,6)Just 4
Example1 expression
findOf folded even [1,3,5,7]Nothing
findOf :: Getter s a     -> (a -> Bool) -> s -> Maybe a
findOf :: Fold s a       -> (a -> Bool) -> s -> Maybe a
findOf :: Iso' s a       -> (a -> Bool) -> s -> Maybe a
findOf :: Lens' s a      -> (a -> Bool) -> s -> Maybe a
findOf :: Traversal' s a -> (a -> Bool) -> s -> Maybe a
find ≡ findOf folded
ifindOf l ≡ findOf l . Indexed

A simpler version that didn't permit indexing, would be:

findOf :: Getting (Endo (Maybe a)) s a -> (a -> Bool) -> s -> Maybe a
findOf l p = foldrOf l (a y -> if p a then Just a else y) Nothing
valuefindMOf
  1. :: Monad m
  2. => Getting (Endo (m (Maybe a))) s a
  3. -> a -> m Bool
  4. -> s
  5. -> m (Maybe a)
#

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.

Example1 expression
findMOf each ( \x -> print ("Checking " ++ show x) >> return (even x)) (1,3,4,6)"Checking 1""Checking 3""Checking 4"Just 4
Example1 expression
findMOf each ( \x -> print ("Checking " ++ show x) >> return (even x)) (1,3,5,7)"Checking 1""Checking 3""Checking 5""Checking 7"Nothing
findMOf :: (Monad m, Getter s a)     -> (a -> m Bool) -> s -> m (Maybe a)
findMOf :: (Monad m, Fold s a)       -> (a -> m Bool) -> s -> m (Maybe a)
findMOf :: (Monad m, Iso' s a)       -> (a -> m Bool) -> s -> m (Maybe a)
findMOf :: (Monad m, Lens' s a)      -> (a -> m Bool) -> s -> m (Maybe a)
findMOf :: (Monad m, Traversal' s a) -> (a -> m Bool) -> s -> m (Maybe a)
findMOf folded :: (Monad m, Foldable f) => (a -> m Bool) -> f a -> m (Maybe a)
ifindMOf l ≡ findMOf l . Indexed

A simpler version that didn't permit indexing, would be:

findMOf :: Monad m => Getting (Endo (m (Maybe a))) s a -> (a -> m Bool) -> s -> m (Maybe a)
findMOf l p = foldrOf l (a y -> p a >>= x -> if x then return (Just a) else y) $ return Nothing
valuefoldr1Of
  1. :: HasCallStack
  2. => Getting (Endo (Maybe a)) s a
  3. -> a -> a -> a
  4. -> s
  5. -> a
#

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.

Example1 expression
foldr1Of each (+) (1,2,3,4)10
foldr1Of l f ≡ Prelude.foldr1 f . toListOf l
foldr1 ≡ foldr1Of folded
foldr1Of :: Getter s a     -> (a -> a -> a) -> s -> a
foldr1Of :: Fold s a       -> (a -> a -> a) -> s -> a
foldr1Of :: Iso' s a       -> (a -> a -> a) -> s -> a
foldr1Of :: Lens' s a      -> (a -> a -> a) -> s -> a
foldr1Of :: Traversal' s a -> (a -> a -> a) -> s -> a
valuefoldl1Of
  1. :: HasCallStack
  2. => Getting (Dual (Endo (Maybe a))) s a
  3. -> a -> a -> a
  4. -> s
  5. -> a
#

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.

Example1 expression
foldl1Of each (+) (1,2,3,4)10
foldl1Of l f ≡ Prelude.foldl1 f . toListOf l
foldl1 ≡ foldl1Of folded
foldl1Of :: Getter s a     -> (a -> a -> a) -> s -> a
foldl1Of :: Fold s a       -> (a -> a -> a) -> s -> a
foldl1Of :: Iso' s a       -> (a -> a -> a) -> s -> a
foldl1Of :: Lens' s a      -> (a -> a -> a) -> s -> a
foldl1Of :: Traversal' s a -> (a -> a -> a) -> s -> a
valuefoldr1Of'
  1. :: HasCallStack
  2. => Getting (Dual (Endo (Endo (Maybe a)))) s a
  3. -> a -> a -> a
  4. -> s
  5. -> a
#

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.

foldr1Of l f ≡ Prelude.foldr1 f . toListOf l
foldr1Of' :: Getter s a     -> (a -> a -> a) -> s -> a
foldr1Of' :: Fold s a       -> (a -> a -> a) -> s -> a
foldr1Of' :: Iso' s a       -> (a -> a -> a) -> s -> a
foldr1Of' :: Lens' s a      -> (a -> a -> a) -> s -> a
foldr1Of' :: Traversal' s a -> (a -> a -> a) -> s -> a
valuefoldl1Of'
  1. :: HasCallStack
  2. => Getting (Endo (Endo (Maybe a))) s a
  3. -> a -> a -> a
  4. -> s
  5. -> a
#

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 . toListOf l
foldl1Of' :: Getter s a     -> (a -> a -> a) -> s -> a
foldl1Of' :: Fold s a       -> (a -> a -> a) -> s -> a
foldl1Of' :: Iso' s a       -> (a -> a -> a) -> s -> a
foldl1Of' :: Lens' s a      -> (a -> a -> a) -> s -> a
foldl1Of' :: Traversal' s a -> (a -> a -> a) -> s -> a
valuefoldrMOf
  1. :: Monad m
  2. => Getting (Dual (Endo (r -> m r))) s a
  3. -> a -> r -> m r
  4. -> r
  5. -> s
  6. -> m r
#

Monadic fold over the elements of a structure, associating to the right, i.e. from right to left.

foldrM ≡ foldrMOf folded
foldrMOf :: Monad m => Getter s a     -> (a -> r -> m r) -> r -> s -> m r
foldrMOf :: Monad m => Fold s a       -> (a -> r -> m r) -> r -> s -> m r
foldrMOf :: Monad m => Iso' s a       -> (a -> r -> m r) -> r -> s -> m r
foldrMOf :: Monad m => Lens' s a      -> (a -> r -> m r) -> r -> s -> m r
foldrMOf :: Monad m => Traversal' s a -> (a -> r -> m r) -> r -> s -> m r
valuefoldlMOf
  1. :: Monad m
  2. => Getting (Endo (r -> m r)) s a
  3. -> r -> a -> m r
  4. -> r
  5. -> s
  6. -> m r
#

Monadic fold over the elements of a structure, associating to the left, i.e. from left to right.

foldlM ≡ foldlMOf folded
foldlMOf :: Monad m => Getter s a     -> (r -> a -> m r) -> r -> s -> m r
foldlMOf :: Monad m => Fold s a       -> (r -> a -> m r) -> r -> s -> m r
foldlMOf :: Monad m => Iso' s a       -> (r -> a -> m r) -> r -> s -> m r
foldlMOf :: Monad m => Lens' s a      -> (r -> a -> m r) -> r -> s -> m r
foldlMOf :: Monad m => Traversal' s a -> (r -> a -> m r) -> r -> s -> m r
valuelookupOf :: Eq k => Getting (Endo (Maybe v)) s (k, v) -> k -> s -> Maybe v
#

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.

Example1 expression
lookupOf folded 4 [(2, 'a'), (4, 'b'), (4, 'c')]Just 'b'
Example1 expression
lookupOf each 2 [(2, 'a'), (4, 'b'), (4, 'c')]Just 'a'
lookupOf :: Eq k => Fold s (k,v) -> k -> s -> Maybe v
valueifoldMapOf :: IndexedGetting i m s a -> (i -> a -> m) -> s -> m
#

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.

foldMapOf l ≡ ifoldMapOf l . const
ifoldMapOf ::             IndexedGetter i s a     -> (i -> a -> m) -> s -> m
ifoldMapOf :: Monoid m => IndexedFold i s a       -> (i -> a -> m) -> s -> m
ifoldMapOf ::             IndexedLens' i s a      -> (i -> a -> m) -> s -> m
ifoldMapOf :: Monoid m => IndexedTraversal' i s a -> (i -> a -> m) -> s -> m
valueifoldrOf
  1. :: IndexedGetting i (Endo r) s a
  2. -> i -> a -> r -> r
  3. -> r
  4. -> s
  5. -> r
#

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.

foldrOf l ≡ ifoldrOf l . const
ifoldrOf :: IndexedGetter i s a     -> (i -> a -> r -> r) -> r -> s -> r
ifoldrOf :: IndexedFold i s a       -> (i -> a -> r -> r) -> r -> s -> r
ifoldrOf :: IndexedLens' i s a      -> (i -> a -> r -> r) -> r -> s -> r
ifoldrOf :: IndexedTraversal' i s a -> (i -> a -> r -> r) -> r -> s -> r
valueifoldlOf
  1. :: IndexedGetting i (Dual (Endo r)) s a
  2. -> i -> r -> a -> r
  3. -> r
  4. -> s
  5. -> r
#

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.

foldlOf l ≡ ifoldlOf l . const
ifoldlOf :: IndexedGetter i s a     -> (i -> r -> a -> r) -> r -> s -> r
ifoldlOf :: IndexedFold i s a       -> (i -> r -> a -> r) -> r -> s -> r
ifoldlOf :: IndexedLens' i s a      -> (i -> r -> a -> r) -> r -> s -> r
ifoldlOf :: IndexedTraversal' i s a -> (i -> r -> a -> r) -> r -> s -> r
valueianyOf :: IndexedGetting i Any s a -> (i -> a -> Bool) -> s -> Bool
#

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.

anyOf l ≡ ianyOf l . const
ianyOf :: IndexedGetter i s a     -> (i -> a -> Bool) -> s -> Bool
ianyOf :: IndexedFold i s a       -> (i -> a -> Bool) -> s -> Bool
ianyOf :: IndexedLens' i s a      -> (i -> a -> Bool) -> s -> Bool
ianyOf :: IndexedTraversal' i s a -> (i -> a -> Bool) -> s -> Bool
valueiallOf :: IndexedGetting i All s a -> (i -> a -> Bool) -> s -> Bool
#

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.

allOf l ≡ iallOf l . const
iallOf :: IndexedGetter i s a     -> (i -> a -> Bool) -> s -> Bool
iallOf :: IndexedFold i s a       -> (i -> a -> Bool) -> s -> Bool
iallOf :: IndexedLens' i s a      -> (i -> a -> Bool) -> s -> Bool
iallOf :: IndexedTraversal' i s a -> (i -> a -> Bool) -> s -> Bool
valueinoneOf :: IndexedGetting i Any s a -> (i -> a -> Bool) -> s -> Bool
#

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.

noneOf l ≡ inoneOf l . const
inoneOf :: IndexedGetter i s a     -> (i -> a -> Bool) -> s -> Bool
inoneOf :: IndexedFold i s a       -> (i -> a -> Bool) -> s -> Bool
inoneOf :: IndexedLens' i s a      -> (i -> a -> Bool) -> s -> Bool
inoneOf :: IndexedTraversal' i s a -> (i -> a -> Bool) -> s -> Bool
valueitraverseOf_
  1. :: Functor f
  2. => IndexedGetting i (Traversed r f) s a
  3. -> i -> a -> f r
  4. -> s
  5. -> f ()
#

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 ≡ itraverseOf l . const
itraverseOf_ :: Functor f     => IndexedGetter i s a     -> (i -> a -> f r) -> s -> f ()
itraverseOf_ :: Applicative f => IndexedFold i s a       -> (i -> a -> f r) -> s -> f ()
itraverseOf_ :: Functor f     => IndexedLens' i s a      -> (i -> a -> f r) -> s -> f ()
itraverseOf_ :: Applicative f => IndexedTraversal' i s a -> (i -> a -> f r) -> s -> f ()
valueiforOf_
  1. :: Functor f
  2. => IndexedGetting i (Traversed r f) s a
  3. -> s
  4. -> i -> a -> f r
  5. -> f ()
#

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_ :: Functor f     => IndexedGetter i s a     -> s -> (i -> a -> f r) -> f ()
iforOf_ :: Applicative f => IndexedFold i s a       -> s -> (i -> a -> f r) -> f ()
iforOf_ :: Functor f     => IndexedLens' i s a      -> s -> (i -> a -> f r) -> f ()
iforOf_ :: Applicative f => IndexedTraversal' i s a -> s -> (i -> a -> f r) -> f ()
valueimapMOf_
  1. :: Monad m
  2. => IndexedGetting i (Sequenced r m) s a
  3. -> i -> a -> m r
  4. -> s
  5. -> m ()
#

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 ≡ Control.Lens.Setter.imapMOf l . const
imapMOf_ :: Monad m => IndexedGetter i s a     -> (i -> a -> m r) -> s -> m ()
imapMOf_ :: Monad m => IndexedFold i s a       -> (i -> a -> m r) -> s -> m ()
imapMOf_ :: Monad m => IndexedLens' i s a      -> (i -> a -> m r) -> s -> m ()
imapMOf_ :: Monad m => IndexedTraversal' i s a -> (i -> a -> m r) -> s -> m ()
valueiforMOf_
  1. :: Monad m
  2. => IndexedGetting i (Sequenced r m) s a
  3. -> s
  4. -> i -> a -> m r
  5. -> m ()
#

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 ≡ iforMOf l a . const
iforMOf_ :: Monad m => IndexedGetter i s a     -> s -> (i -> a -> m r) -> m ()
iforMOf_ :: Monad m => IndexedFold i s a       -> s -> (i -> a -> m r) -> m ()
iforMOf_ :: Monad m => IndexedLens' i s a      -> s -> (i -> a -> m r) -> m ()
iforMOf_ :: Monad m => IndexedTraversal' i s a -> s -> (i -> a -> m r) -> m ()
valueiconcatMapOf :: IndexedGetting i [r] s a -> (i -> a -> [r]) -> s -> [r]
#

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.

concatMapOf l ≡ iconcatMapOf l . const
iconcatMapOf ≡ ifoldMapOf
iconcatMapOf :: IndexedGetter i s a     -> (i -> a -> [r]) -> s -> [r]
iconcatMapOf :: IndexedFold i 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]
valueifindOf
  1. :: IndexedGetting i (Endo (Maybe a)) s a
  2. -> i -> a -> Bool
  3. -> s
  4. -> Maybe a
#

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.

findOf l ≡ ifindOf l . const
ifindOf :: IndexedGetter i s a     -> (i -> a -> Bool) -> s -> Maybe a
ifindOf :: IndexedFold i s a       -> (i -> a -> Bool) -> s -> Maybe a
ifindOf :: IndexedLens' i s a      -> (i -> a -> Bool) -> s -> Maybe a
ifindOf :: IndexedTraversal' i s a -> (i -> a -> Bool) -> s -> Maybe a
valueifindMOf
  1. :: Monad m
  2. => IndexedGetting i (Endo (m (Maybe a))) s a
  3. -> i -> a -> m Bool
  4. -> s
  5. -> m (Maybe a)
#

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.

findMOf l ≡ ifindMOf l . const
ifindMOf :: Monad m => IndexedGetter i s a     -> (i -> a -> m Bool) -> s -> m (Maybe a)
ifindMOf :: Monad m => IndexedFold i s a       -> (i -> a -> m Bool) -> s -> m (Maybe a)
ifindMOf :: Monad m => IndexedLens' i s a      -> (i -> a -> m Bool) -> s -> m (Maybe a)
ifindMOf :: Monad m => IndexedTraversal' i s a -> (i -> a -> m Bool) -> s -> m (Maybe a)
valueifoldrOf'
  1. :: IndexedGetting i (Dual (Endo (r -> r))) s a
  2. -> i -> a -> r -> r
  3. -> r
  4. -> s
  5. -> r
#

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' :: IndexedGetter i s a     -> (i -> a -> r -> r) -> r -> s -> r
ifoldrOf' :: IndexedFold i s a       -> (i -> a -> r -> r) -> r -> s -> r
ifoldrOf' :: IndexedLens' i s a      -> (i -> a -> r -> r) -> r -> s -> r
ifoldrOf' :: IndexedTraversal' i s a -> (i -> a -> r -> r) -> r -> s -> r
valueifoldlOf'
  1. :: IndexedGetting i (Endo (r -> r)) s a
  2. -> i -> r -> a -> r
  3. -> r
  4. -> s
  5. -> r
#

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' :: IndexedGetter i s a       -> (i -> r -> a -> r) -> r -> s -> r
ifoldlOf' :: IndexedFold i s a         -> (i -> r -> a -> r) -> r -> s -> r
ifoldlOf' :: IndexedLens' i s a        -> (i -> r -> a -> r) -> r -> s -> r
ifoldlOf' :: IndexedTraversal' i s a   -> (i -> r -> a -> r) -> r -> s -> r
valueifoldrMOf
  1. :: Monad m
  2. => IndexedGetting i (Dual (Endo (r -> m r))) s a
  3. -> i -> a -> r -> m r
  4. -> r
  5. -> s
  6. -> m r
#

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.

foldrMOf l ≡ ifoldrMOf l . const
ifoldrMOf :: Monad m => IndexedGetter i s a     -> (i -> a -> r -> m r) -> r -> s -> m r
ifoldrMOf :: Monad m => IndexedFold i s a       -> (i -> a -> r -> m r) -> r -> s -> m r
ifoldrMOf :: Monad m => IndexedLens' i s a      -> (i -> a -> r -> m r) -> r -> s -> m r
ifoldrMOf :: Monad m => IndexedTraversal' i s a -> (i -> a -> r -> m r) -> r -> s -> m r
valueifoldlMOf
  1. :: Monad m
  2. => IndexedGetting i (Endo (r -> m r)) s a
  3. -> i -> r -> a -> m r
  4. -> r
  5. -> s
  6. -> m r
#

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.

foldlMOf l ≡ ifoldlMOf l . const
ifoldlMOf :: Monad m => IndexedGetter i s a     -> (i -> r -> a -> m r) -> r -> s -> m r
ifoldlMOf :: Monad m => IndexedFold i s a       -> (i -> r -> a -> m r) -> r -> s -> m r
ifoldlMOf :: Monad m => IndexedLens' i s a      -> (i -> r -> a -> m r) -> r -> s -> m r
ifoldlMOf :: Monad m => IndexedTraversal' i s a -> (i -> r -> a -> m r) -> r -> s -> m r
valueifiltered
  1. :: (Indexable i p, Applicative f)
  2. => i -> a -> Bool
  3. -> Optical' p (Indexed i) f a a
#

Filter an IndexedFold or IndexedGetter, obtaining an IndexedFold.

Example1 expression
[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!

valueitakingWhile
  1. :: (Indexable i p, Profunctor q, Contravariant f, Applicative f)
  2. => i -> a -> Bool
  3. -> Optical' (Indexed i) q (Const (Endo (f s))) s a
  4. -> Optical' p q f s a
#

Obtain an IndexedFold by taking elements from another IndexedFold, IndexedLens, IndexedGetter or IndexedTraversal while a predicate holds.

itakingWhile :: (i -> a -> Bool) -> IndexedFold i s a          -> IndexedFold i s a
itakingWhile :: (i -> a -> Bool) -> IndexedTraversal' i s a    -> IndexedFold i s a
itakingWhile :: (i -> a -> Bool) -> IndexedLens' i s a         -> IndexedFold i s a
itakingWhile :: (i -> a -> Bool) -> IndexedGetter i s a        -> IndexedFold i 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.

valueidroppingWhile
  1. :: (Indexable i p, Profunctor q, Applicative f)
  2. => i -> a -> Bool
  3. -> Optical (Indexed i) q (Compose (State Bool) f) s t a a
  4. -> Optical p q f s t a a
#

Obtain an IndexedFold by dropping elements from another IndexedFold, IndexedLens, IndexedGetter or IndexedTraversal while a predicate holds.

idroppingWhile :: (i -> a -> Bool) -> IndexedFold i s a          -> IndexedFold i s a
idroppingWhile :: (i -> a -> Bool) -> IndexedTraversal' i s a    -> IndexedFold i s a -- see notes
idroppingWhile :: (i -> a -> Bool) -> IndexedLens' i s a         -> IndexedFold i s a -- see notes
idroppingWhile :: (i -> a -> Bool) -> IndexedGetter i s a        -> IndexedFold i 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.

newtypenewtype Traversed a (f :: Type -> Type)
#

Used internally by Control.Lens.Traversal.traverseOf_ and the like.

The argument a of the result should not be used!

Instances2Semigroup, Monoid
newtypenewtype Sequenced a (m :: Type -> Type)
#

Used internally by Control.Lens.Traversal.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 =>"?

Instances2Semigroup, Monoid
valuefoldBy :: Foldable t => (a -> a -> a) -> a -> t a -> a
#

Fold a value using its Foldable instance using explicitly provided Monoid operations. This is like fold where the Monoid instance can be manually specified.

foldBy mappend mempty ≡ fold
Example1 expression
foldBy (++) [] ["hello","world"]"helloworld"
valuefoldByOf :: Fold s a -> (a -> a -> a) -> a -> s -> a
#

Fold a value using a specified Fold and Monoid operations. This is like foldBy where the Foldable instance can be manually specified.

foldByOf folded ≡ foldBy
foldByOf :: Getter s a     -> (a -> a -> a) -> a -> s -> a
foldByOf :: Fold s a       -> (a -> a -> a) -> a -> s -> a
foldByOf :: Lens' s a      -> (a -> a -> a) -> a -> s -> a
foldByOf :: Traversal' s a -> (a -> a -> a) -> a -> s -> a
foldByOf :: Iso' s a       -> (a -> a -> a) -> a -> s -> a
Example1 expression
foldByOf both (++) [] ("hello","world")"helloworld"
valuefoldMapByOf :: Fold s a -> (r -> r -> r) -> r -> (a -> r) -> s -> r
#

Fold a value using a specified Fold and Monoid operations. This is like foldMapBy where the Foldable instance can be manually specified.

foldMapByOf folded ≡ foldMapBy
foldMapByOf :: Getter s a     -> (r -> r -> r) -> r -> (a -> r) -> s -> r
foldMapByOf :: Fold s a       -> (r -> r -> r) -> r -> (a -> r) -> s -> r
foldMapByOf :: Traversal' s a -> (r -> r -> r) -> r -> (a -> r) -> s -> r
foldMapByOf :: Lens' s a      -> (r -> r -> r) -> r -> (a -> r) -> s -> r
foldMapByOf :: Iso' s a       -> (r -> r -> r) -> r -> (a -> r) -> s -> r
Example1 expression
foldMapByOf both (+) 0 length ("hello","world")10
datadata Magma i t b a where
#

This provides a way to peek at the internal structure of a Control.Lens.Traversal.Traversal or Control.Lens.Traversal.IndexedTraversal

Instances7FoldableWithIndex, FunctorWithIndex, TraversableWithIndex, Functor, Foldable, Traversable, …
newtypenewtype ReifiedIndexedGetter i s a
#

Reify an IndexedGetter so it can be stored safely in a container.

Instances7Strong, Profunctor, Representable, Sieve, Functor, Apply, …
typetype Getting r s a = (a -> Const r a) -> s -> Const r s
#

When you see this in a type signature it indicates that you can pass the function a Lens, Getter, Control.Lens.Traversal.Traversal, Control.Lens.Fold.Fold, Control.Lens.Prism.Prism, Control.Lens.Iso.Iso, or one of the indexed variants, and it will just "do the right thing".

Most Getter combinators are able to be used with both a Getter or a Control.Lens.Fold.Fold in limited situations, to do so, they need to be monomorphic in what we are going to extract with Const. To be compatible with Lens, Control.Lens.Traversal.Traversal and Control.Lens.Iso.Iso we also restricted choices of the irrelevant t and b parameters.

If a function accepts a Getting r s a, then when r is a Monoid, then you can pass a Control.Lens.Fold.Fold (or Control.Lens.Traversal.Traversal), otherwise you can only pass this a Getter or Lens.

typetype Accessing (p :: Type -> Type -> Type) m s a = p a (Const m a) -> s -> Const m s
#

This is a convenient alias used when consuming (indexed) getters and (indexed) folds in a highly general fashion.

valueto :: (Profunctor p, Contravariant f) => (s -> a) -> Optic' p f s a
#

Build an (index-preserving) Getter from an arbitrary Haskell function.

to f . to g ≡ to (g . f)
a ^. to f ≡ f a
Example1 expression
a ^.to ff a
Example1 expression
("hello","world")^.to snd"world"
Example1 expression
5^.to succ6
Example1 expression
(0, -5)^._2.to abs5
to :: (s -> a) -> IndexPreservingGetter s a
valueview :: MonadReader s m => Getting a s a -> m a
#

View the value pointed to by a Getter, Control.Lens.Iso.Iso or Lens or the result of folding over all the results of a Fold or Control.Lens.Traversal.Traversal that points at a monoidal value.

view . to ≡ id
Example1 expression
view (to f) af a
Example1 expression
view _2 (1,"hello")"hello"
Example1 expression
view (to succ) 56
Example1 expression
view (_2._1) ("hello",("world","!!!"))"world"

As view is commonly used to access the target of a Getter or obtain a monoidal summary of the targets of a Fold, It may be useful to think of it as having one of these more restricted signatures:

view ::             Getter s a     -> s -> a
view :: Monoid m => Fold s m       -> s -> m
view ::             Control.Lens.Iso.Iso' s a       -> s -> a
view ::             Lens' s a      -> s -> a
view :: Monoid m => Control.Lens.Traversal.Traversal' s m -> s -> m

In a more general setting, such as when working with a Monad transformer stack you can use:

view :: MonadReader s m             => Getter s a     -> m a
view :: (MonadReader s m, Monoid a) => Fold s a       -> m a
view :: MonadReader s m             => Control.Lens.Iso.Iso' s a       -> m a
view :: MonadReader s m             => Lens' s a      -> m a
view :: (MonadReader s m, Monoid a) => Control.Lens.Traversal.Traversal' s a -> m a
valueviews :: MonadReader s m => LensLike' (Const r) s a -> (a -> r) -> m r
#

View a function of the value pointed to by a Getter or Lens or the result of folding over the result of mapping the targets of a Fold or Control.Lens.Traversal.Traversal.

views l f ≡ view (l . to f)
Example1 expression
views (to f) g ag (f a)
Example1 expression
views _2 length (1,"hello")5

As views is commonly used to access the target of a Getter or obtain a monoidal summary of the targets of a Fold, It may be useful to think of it as having one of these more restricted signatures:

views ::             Getter s a     -> (a -> r) -> s -> r
views :: Monoid m => Fold s a       -> (a -> m) -> s -> m
views ::             Control.Lens.Iso.Iso' s a       -> (a -> r) -> s -> r
views ::             Lens' s a      -> (a -> r) -> s -> r
views :: Monoid m => Control.Lens.Traversal.Traversal' s a -> (a -> m) -> s -> m

In a more general setting, such as when working with a Monad transformer stack you can use:

views :: MonadReader s m             => Getter s a     -> (a -> r) -> m r
views :: (MonadReader s m, Monoid r) => Fold s a       -> (a -> r) -> m r
views :: MonadReader s m             => Control.Lens.Iso.Iso' s a       -> (a -> r) -> m r
views :: MonadReader s m             => Lens' s a      -> (a -> r) -> m r
views :: (MonadReader s m, Monoid r) => Control.Lens.Traversal.Traversal' s a -> (a -> r) -> m r
views :: MonadReader s m => Getting r s a -> (a -> r) -> m r
valueuse :: MonadState s m => Getting a s a -> m a
#

Use the target of a Lens, Control.Lens.Iso.Iso, or Getter in the current state, or use a summary of a Control.Lens.Fold.Fold or Control.Lens.Traversal.Traversal that points to a monoidal value.

Example1 expression
evalState (use _1) (a,b)a
Example1 expression
evalState (use _1) ("hello","world")"hello"
use :: MonadState s m             => Getter s a     -> m a
use :: (MonadState s m, Monoid r) => Control.Lens.Fold.Fold s r       -> m r
use :: MonadState s m             => Control.Lens.Iso.Iso' s a       -> m a
use :: MonadState s m             => Lens' s a      -> m a
use :: (MonadState s m, Monoid r) => Control.Lens.Traversal.Traversal' s r -> m r
valueuses :: MonadState s m => LensLike' (Const r) s a -> (a -> r) -> m r
#

Use the target of a Lens, Control.Lens.Iso.Iso or Getter in the current state, or use a summary of a Control.Lens.Fold.Fold or Control.Lens.Traversal.Traversal that points to a monoidal value.

Example1 expression
evalState (uses _1 length) ("hello","world")5
uses :: MonadState s m             => Getter s a     -> (a -> r) -> m r
uses :: (MonadState s m, Monoid r) => Control.Lens.Fold.Fold s a       -> (a -> r) -> m r
uses :: MonadState s m             => Lens' s a      -> (a -> r) -> m r
uses :: MonadState s m             => Control.Lens.Iso.Iso' s a       -> (a -> r) -> m r
uses :: (MonadState s m, Monoid r) => Control.Lens.Traversal.Traversal' s a -> (a -> r) -> m r
uses :: MonadState s m => Getting r s t a b -> (a -> r) -> m r
valuelistening :: MonadWriter w m => Getting u w u -> m a -> m (a, u)
#

This is a generalized form of listen that only extracts the portion of the log that is focused on by a Getter. If given a Fold or a Traversal then a monoidal summary of the parts of the log that are visited will be returned.

listening :: MonadWriter w m             => Getter w u     -> m a -> m (a, u)
listening :: MonadWriter w m             => Lens' w u      -> m a -> m (a, u)
listening :: MonadWriter w m             => Iso' w u       -> m a -> m (a, u)
listening :: (MonadWriter w m, Monoid u) => Fold w u       -> m a -> m (a, u)
listening :: (MonadWriter w m, Monoid u) => Traversal' w u -> m a -> m (a, u)
listening :: (MonadWriter w m, Monoid u) => Prism' w u     -> m a -> m (a, u)
valuelistenings
  1. :: MonadWriter w m
  2. => Getting v w u
  3. -> u -> v
  4. -> m a
  5. -> m (a, v)
#

This is a generalized form of listen that only extracts the portion of the log that is focused on by a Getter. If given a Fold or a Traversal then a monoidal summary of the parts of the log that are visited will be returned.

listenings :: MonadWriter w m             => Getter w u     -> (u -> v) -> m a -> m (a, v)
listenings :: MonadWriter w m             => Lens' w u      -> (u -> v) -> m a -> m (a, v)
listenings :: MonadWriter w m             => Iso' w u       -> (u -> v) -> m a -> m (a, v)
listenings :: (MonadWriter w m, Monoid v) => Fold w u       -> (u -> v) -> m a -> m (a, v)
listenings :: (MonadWriter w m, Monoid v) => Traversal' w u -> (u -> v) -> m a -> m (a, v)
listenings :: (MonadWriter w m, Monoid v) => Prism' w u     -> (u -> v) -> m a -> m (a, v)
valueiview :: MonadReader s m => IndexedGetting i (i, a) s a -> m (i, a)
#

View the index and value of an IndexedGetter into the current environment as a pair.

When applied to an IndexedFold the result will most likely be a nonsensical monoidal summary of the indices tupled with a monoidal summary of the values and probably not whatever it is you wanted.

valueiuse :: MonadState s m => IndexedGetting i (i, a) s a -> m (i, a)
#

Use the index and value of an IndexedGetter into the current state as a pair.

When applied to an IndexedFold the result will most likely be a nonsensical monoidal summary of the indices tupled with a monoidal summary of the values and probably not whatever it is you wanted.

valueilistening
  1. :: MonadWriter w m
  2. => IndexedGetting i (i, u) w u
  3. -> m a
  4. -> m (a, (i, u))
#

This is a generalized form of listen that only extracts the portion of the log that is focused on by a Getter. If given a Fold or a Traversal then a monoidal summary of the parts of the log that are visited will be returned.

ilistening :: MonadWriter w m             => IndexedGetter i w u     -> m a -> m (a, (i, u))
ilistening :: MonadWriter w m             => IndexedLens' i w u      -> m a -> m (a, (i, u))
ilistening :: (MonadWriter w m, Monoid u) => IndexedFold i w u       -> m a -> m (a, (i, u))
ilistening :: (MonadWriter w m, Monoid u) => IndexedTraversal' i w u -> m a -> m (a, (i, u))
valueilistenings
  1. :: MonadWriter w m
  2. => IndexedGetting i v w u
  3. -> i -> u -> v
  4. -> m a
  5. -> m (a, v)
#

This is a generalized form of listen that only extracts the portion of the log that is focused on by a Getter. If given a Fold or a Traversal then a monoidal summary of the parts of the log that are visited will be returned.

ilistenings :: MonadWriter w m             => IndexedGetter w u     -> (i -> u -> v) -> m a -> m (a, v)
ilistenings :: MonadWriter w m             => IndexedLens' w u      -> (i -> u -> v) -> m a -> m (a, v)
ilistenings :: (MonadWriter w m, Monoid v) => IndexedFold w u       -> (i -> u -> v) -> m a -> m (a, v)
ilistenings :: (MonadWriter w m, Monoid v) => IndexedTraversal' w u -> (i -> u -> v) -> m a -> m (a, v)
classclass Contravariant (f :: Type -> Type) where
#

The class of contravariant functors.

Whereas in Haskell, one can think of a Functor as containing or producing values, a contravariant functor is a functor that can be thought of as consuming values.

As an example, consider the type of predicate functions a -> Bool. One such predicate might be negative x = x < 0, which classifies integers as to whether they are negative. However, given this predicate, we can re-use it in other situations, providing we have a way to map values to integers. For instance, we can use the negative predicate on a person's bank balance to work out if they are currently overdrawn:

newtype Predicate a = Predicate { getPredicate :: a -> Bool }

instance Contravariant Predicate where
  contramap :: (a' -> a) -> (Predicate a -> Predicate a')
  contramap f (Predicate p) = Predicate (p . f)
                                         |   `- First, map the input...
                                         `----- then apply the predicate.

overdrawn :: Predicate Person
overdrawn = contramap personBankBalance negative

Any instance should be subject to the following laws:

Identity

contramap id = id

Composition

contramap (g . f) = contramap f . contramap g

Note, that the second law follows from the free theorem of the type of contramap and the first law, so you need only check that the former condition holds.

Methods

  • contramap :: (a' -> a) -> f a -> f a'
  • (>$) :: b -> f b -> f ainfixl 4

    Replace all locations in the output with the same value. The default definition is contramap . const, but this may be overridden with a more efficient version.

Instances51Contravariant, …
newtypenewtype Const a (b :: k)
#

The Const functor.

Examples
Example1 expression
fmap (++ "World") (Const "Hello")Const "Hello"

Because we ignore the second type parameter to Const, the Applicative instance, which has (<*>) :: Monoid m => Const m (a -> b) -> Const m a -> Const m b essentially turns into Monoid m => m -> m -> m, which is (<>)

Example1 expression
Const [1, 2, 3] <*> Const [4, 5, 6]Const [1,2,3,4,5,6]

Constructors

Instances72Semigroupoid, Generic1, FoldableWithIndex, FunctorWithIndex, TraversableWithIndex, Bifoldable, …
classclass Conjoined p => Indexable i (p :: Type -> Type -> Type) where
#

This class permits overloading of function application for things that also admit a notion of a key or index.

Methods

  • indexed :: p a b -> i -> a -> b

    Build a function from an indexed function.

Instances2Indexable
  • i ~ j => Indexable i (Indexed j)Defined in lens-5.3.5 · Control.Lens.Internal.Indexed
  • Indexable i (->)Defined in lens-5.3.5 · Control.Lens.Internal.Indexed
classclass (Choice p, Corepresentable p, Comonad (Corep p), Traversable (Corep p), Strong p, Representable p, Monad (Rep p), MonadFix (Rep p), Distributive (Rep p), Costrong p, ArrowLoop p, ArrowApply p, ArrowChoice p, Closed p) => Conjoined (p :: Type -> Type -> Type) where
#

This is a Profunctor that is both Corepresentable by f and Representable by g such that f is left adjoint to g. From this you can derive a lot of structure due to the preservation of limits and colimits.

Methods

  • distrib :: Functor f => p a b -> p (f a) (f b)

    Conjoined is strong enough to let us distribute every Conjoined Profunctor over every Haskell Functor. This is effectively a generalization of fmap.

  • conjoined :: (p ~ (->) => q (a -> b) r) -> q (p a b) r -> q (p a b) r

    This permits us to make a decision at an outermost point about whether or not we use an index.

    Ideally any use of this function should be done in such a way so that you compute the same answer, but this cannot be enforced at the type level.

Instances3Conjoined
valueselfIndex :: Indexable a p => p a fb -> a -> fb
#

Use a value itself as its own index. This is essentially an indexed version of id.

Note: When used to modify the value, this can break the index requirements assumed by indices and similar, so this is only properly an IndexedGetter, but it can be used as more.

selfIndex :: IndexedGetter a a b
valueindexing
  1. :: Indexable Int p
  2. => (a -> Indexing f b) -> s -> Indexing f t
  3. -> p a (f b)
  4. -> s
  5. -> f t
#

Transform a Control.Lens.Traversal.Traversal into an Control.Lens.Traversal.IndexedTraversal or a Control.Lens.Fold.Fold into an Control.Lens.Fold.IndexedFold, etc.

indexing :: Traversal s t a b -> IndexedTraversal Int s t a b
indexing :: Prism s t a b     -> IndexedTraversal Int s t a b
indexing :: Lens s t a b      -> IndexedLens Int  s t a b
indexing :: Iso s t a b       -> IndexedLens Int s t a b
indexing :: Fold s a          -> IndexedFold Int s a
indexing :: Getter s a        -> IndexedGetter Int s a
indexing :: Indexable Int p => LensLike (Indexing f) s t a b -> Over p f s t a b
valueindexing64
  1. :: Indexable Int64 p
  2. => (a -> Indexing64 f b) -> s -> Indexing64 f t
  3. -> p a (f b)
  4. -> s
  5. -> f t
#

Transform a Control.Lens.Traversal.Traversal into an Control.Lens.Traversal.IndexedTraversal or a Control.Lens.Fold.Fold into an Control.Lens.Fold.IndexedFold, etc.

This combinator is like indexing except that it handles large traversals and folds gracefully.

indexing64 :: Traversal s t a b -> IndexedTraversal Int64 s t a b
indexing64 :: Prism s t a b     -> IndexedTraversal Int64 s t a b
indexing64 :: Lens s t a b      -> IndexedLens Int64 s t a b
indexing64 :: Iso s t a b       -> IndexedLens Int64 s t a b
indexing64 :: Fold s a          -> IndexedFold Int64 s a
indexing64 :: Getter s a        -> IndexedGetter Int64 s a
indexing64 :: Indexable Int64 p => LensLike (Indexing64 f) s t a b -> Over p f s t a b
classclass Functor f => FunctorWithIndex i (f :: Type -> Type) | f -> i where
#

A Functor with an additional index.

Instances must satisfy a modified form of the Functor laws:

imap f . imap g ≡ imap (\i -> f i . g i)
imap (\_ a -> a) ≡ id

Methods

  • imap :: (i -> a -> b) -> f a -> f b

    Map with access to the index.

Instances40FunctorWithIndex, …
classclass Foldable f => FoldableWithIndex i (f :: Type -> Type) | f -> i where
#

A container that supports folding with an additional index.

Methods

  • ifoldMap :: Monoid m => (i -> a -> m) -> f a -> m

    Fold a container by mapping value to an arbitrary Monoid with access to the index i.

    When you don't need access to the index then foldMap is more flexible in what it accepts.

    foldMap ≡ ifoldMap . const
    
  • ifoldMap' :: Monoid m => (i -> a -> m) -> f a -> m

    A variant of ifoldMap that is strict in the accumulator.

    When you don't need access to the index then foldMap' is more flexible in what it accepts.

    foldMap' ≡ ifoldMap' . const
    
  • ifoldr :: (i -> a -> b -> b) -> b -> f a -> b

    Right-associative fold of an indexed container with access to the index i.

    When you don't need access to the index then foldr is more flexible in what it accepts.

    foldr ≡ ifoldr . const
    
  • ifoldl :: (i -> b -> a -> b) -> b -> f a -> b

    Left-associative fold of an indexed container with access to the index i.

    When you don't need access to the index then foldl is more flexible in what it accepts.

    foldl ≡ ifoldl . const
    
  • ifoldr' :: (i -> a -> b -> b) -> b -> f a -> b

    Strictly fold right over the elements of a structure with access to the index i.

    When you don't need access to the index then foldr' is more flexible in what it accepts.

    foldr' ≡ ifoldr' . const
    
  • ifoldl' :: (i -> b -> a -> b) -> b -> f a -> b

    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.

    foldl' l ≡ ifoldl' l . const
    
Instances37FoldableWithIndex, …
valueiany :: FoldableWithIndex i f => (i -> a -> Bool) -> f a -> Bool
#

Return whether or not any element in a container satisfies a predicate, with access to the index i.

When you don't need access to the index then any is more flexible in what it accepts.

any ≡ iany . const
valueiall :: FoldableWithIndex i f => (i -> a -> Bool) -> f a -> Bool
#

Return whether or not all elements in a container satisfy a predicate, with access to the index i.

When you don't need access to the index then all is more flexible in what it accepts.

all ≡ iall . const
valueinone :: FoldableWithIndex i f => (i -> a -> Bool) -> f a -> Bool
#

Return whether or not none of the elements in a container satisfy a predicate, with access to the index i.

When you don't need access to the index then none is more flexible in what it accepts.

none ≡ inone . const
inone f ≡ not . iany f
valueitraverse_
  1. :: (FoldableWithIndex i t, Applicative f)
  2. => i -> a -> f b
  3. -> t a
  4. -> f ()
#

Traverse elements with access to the index i, discarding the results.

When you don't need access to the index then traverse_ is more flexible in what it accepts.

traverse_ l = itraverse . const
valueifor_
  1. :: (FoldableWithIndex i t, Applicative f)
  2. => t a
  3. -> i -> a -> f b
  4. -> f ()
#

Traverse elements with access to the index i, discarding the results (with the arguments flipped).

ifor_ ≡ flip itraverse_

When you don't need access to the index then for_ is more flexible in what it accepts.

for_ a ≡ ifor_ a . const
valueimapM_ :: (FoldableWithIndex i t, Monad m) => (i -> a -> m b) -> t a -> m ()
#

Run monadic actions for each target of an IndexedFold or Control.Lens.IndexedTraversal.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.

mapM_ ≡ imapM . const
valueiforM_ :: (FoldableWithIndex i t, Monad m) => t a -> (i -> a -> m b) -> m ()
#

Run monadic actions for each target of an IndexedFold or Control.Lens.IndexedTraversal.IndexedTraversal with access to the index, discarding the results (with the arguments flipped).

iforM_ ≡ flip imapM_

When you don't need access to the index then forM_ is more flexible in what it accepts.

forM_ a ≡ iforM a . const
valueiconcatMap :: FoldableWithIndex i f => (i -> a -> [b]) -> f a -> [b]
#

Concatenate the results of a function of the elements of an indexed container with access to the index.

When you don't need access to the index then concatMap is more flexible in what it accepts.

concatMap ≡ iconcatMap . const
iconcatMap ≡ ifoldMap
valueifind :: FoldableWithIndex i f => (i -> a -> Bool) -> f a -> Maybe (i, a)
#

Searches a container with a predicate that is also supplied the index, returning 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 find is more flexible in what it accepts.

find ≡ ifind . const
valueifoldrM
  1. :: (FoldableWithIndex i f, Monad m)
  2. => i -> a -> b -> m b
  3. -> b
  4. -> f a
  5. -> m b
#

Monadic fold right over the elements of a structure with an index.

When you don't need access to the index then foldrM is more flexible in what it accepts.

foldrM ≡ ifoldrM . const
valueifoldlM
  1. :: (FoldableWithIndex i f, Monad m)
  2. => i -> b -> a -> m b
  3. -> b
  4. -> f a
  5. -> m b
#

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 foldlM is more flexible in what it accepts.

foldlM ≡ ifoldlM . const
valueitoList :: FoldableWithIndex i f => f a -> [(i, a)]
#

Extract the key-value pairs from a structure.

When you don't need access to the indices in the result, then toList is more flexible in what it accepts.

toList ≡ map snd . itoList
valuewithIndex
  1. :: (Indexable i p, Functor f)
  2. => p (i, s) (f (j, t))
  3. -> Indexed i s (f t)
#

Fold a container with indices returning both the indices and the values.

The result is only valid to compose in a Traversal, if you don't edit the index as edits to the index have no effect.

Example1 expression
[10, 20, 30] ^.. ifolded . withIndex[(0,10),(1,20),(2,30)]
Example1 expression
[10, 20, 30] ^.. ifolded . withIndex . alongside negated (re _Show)[(0,"10"),(-1,"20"),(-2,"30")]
classclass (FunctorWithIndex i t, FoldableWithIndex i t, Traversable t) => TraversableWithIndex i (t :: Type -> Type) | t -> i where
#

A Traversable with an additional index.

An instance must satisfy a (modified) form of the Traversable laws:

itraverse (const Identity) ≡ Identity
fmap (itraverse f) . itraverse g ≡ getCompose . itraverse (\i -> Compose . fmap (f i) . g i)

Methods

Instances37TraversableWithIndex, …
valueimapM
  1. :: (TraversableWithIndex i t, Monad m)
  2. => i -> a -> m b
  3. -> t a
  4. -> m (t b)
#

Map each element of a structure to a monadic action, evaluate these actions from left to right, and collect the results, with access the index.

When you don't need access to the index mapM is more liberal in what it can accept.

mapM ≡ imapM . const
valueiforM
  1. :: (TraversableWithIndex i t, Monad m)
  2. => t a
  3. -> i -> a -> m b
  4. -> m (t b)
#

Map each element of a structure to a monadic action, evaluate these actions from left to right, and collect the results, with access its position (and the arguments flipped).

forM a ≡ iforM a . const
iforM ≡ flip imapM
valueimapAccumR
  1. :: TraversableWithIndex i t
  2. => i -> s -> a -> (s, b)
  3. -> s
  4. -> t a
  5. -> (s, t b)
#

Generalizes Data.Traversable.mapAccumR to add access to the index.

imapAccumR accumulates state from right to left.

Data.Traversable.mapAccumR ≡ imapAccumR . const
valueimapAccumL
  1. :: TraversableWithIndex i t
  2. => i -> s -> a -> (s, b)
  3. -> s
  4. -> t a
  5. -> (s, t b)
#

Generalizes Data.Traversable.mapAccumL to add access to the index.

imapAccumL accumulates state from left to right.

Data.Traversable.mapAccumL ≡ imapAccumL . const
newtypenewtype Bazaar (p :: Type -> Type -> Type) a b t
#

This is used to characterize a Control.Lens.Traversal.Traversal.

a.k.a. indexed Cartesian store comonad, indexed Kleene store comonad, or an indexed FunList.

http://twanvl.nl/blog/haskell/non-regular1

A Bazaar is like a Control.Lens.Traversal.Traversal that has already been applied to some structure.

Where a Context a b t holds an a and a function from b to t, a Bazaar a b t holds N as and a function from N bs to t, (where N might be infinite).

Mnemonically, a Bazaar holds many stores and you can easily add more.

This is a final encoding of Bazaar.

Constructors

Instances9Bizarre, Sellable, IndexedComonad, IndexedFunctor, Functor, Applicative, …
datadata Context a b t
#

The indexed store can be used to characterize a Control.Lens.Lens.Lens and is used by cloneLens.

Context a b t is isomorphic to newtype Context a b t = Context { runContext :: forall f. Functor f => (a -> f b) -> f t }, and to exists s. (s, Control.Lens.Lens.Lens s t a b).

A Context is like a Control.Lens.Lens.Lens that has already been applied to a some structure.

Constructors

Instances7IndexedComonad, IndexedComonadStore, IndexedFunctor, ComonadStore, Functor, Comonad, …
newtypenewtype Bazaar1 (p :: Type -> Type -> Type) a b t
#

This is used to characterize a Control.Lens.Traversal.Traversal.

a.k.a. indexed Cartesian store comonad, indexed Kleene store comonad, or an indexed FunList.

http://twanvl.nl/blog/haskell/non-regular1

A Bazaar1 is like a Control.Lens.Traversal.Traversal that has already been applied to some structure.

Where a Context a b t holds an a and a function from b to t, a Bazaar1 a b t holds N as and a function from N bs to t, (where N might be infinite).

Mnemonically, a Bazaar1 holds many stores and you can easily add more.

This is a final encoding of Bazaar1.

Constructors

Instances8Bizarre1, Sellable, IndexedComonad, IndexedFunctor, Functor, Comonad, …
datadata LensRules
#

Rules to construct lenses for data fields.

typetype FieldNamer = Name -> [Name] -> Name -> [DefName]
#

The rule to create function names of lenses for data fields.

Although it's sometimes useful, you won't need the first two arguments most of the time.

datadata DefName
#

Name to give to generated field optics.

Constructors

Instances3Eq, Ord, Show
  • Eq DefNameDefined in lens-5.3.5 · Control.Lens.Internal.FieldTH
  • Ord DefNameDefined in lens-5.3.5 · Control.Lens.Internal.FieldTH
  • Show DefNameDefined in lens-5.3.5 · Control.Lens.Internal.FieldTH
typetype ClassyNamer = Name -> Maybe (Name, Name)
#

The optional rule to create a class and method around a monomorphic data type. If this naming convention is provided, it generates a "classy" lens.

classclass (Functor t, Foldable t) => Traversable (t :: Type -> Type) where
#

Functors representing data structures that can be transformed to structures of the same shape by performing an Applicative (or, therefore, Monad) action on each element from left to right.

A more detailed description of what same shape means, the various methods, how traversals are constructed, and example advanced use-cases can be found in the Overview section of Data.Traversable#overview.

For the class laws see the Laws section of Data.Traversable#laws.

Methods

  • traverse :: Applicative f => (a -> f b) -> t a -> f (t b)

    Map each element of a structure to an action, evaluate these actions from left to right, and collect the results. For a version that ignores the results see traverse_.

    Examples

    Basic usage:

    In the first two examples we show each evaluated action mapping to the output structure.

    Example1 expression
    traverse Just [1,2,3,4]Just [1,2,3,4]
    Example1 expression
    traverse id [Right 1, Right 2, Right 3, Right 4]Right [1,2,3,4]

    In the next examples, we show that Nothing and Left values short circuit the created structure.

    Example1 expression
    traverse (const Nothing) [1,2,3,4]Nothing
    Example1 expression
    traverse (\x -> if odd x then Just x else Nothing)  [1,2,3,4]Nothing
    Example1 expression
    traverse id [Right 1, Right 2, Right 3, Right 4, Left 0]Left 0
Instances113Traversable, …
valuemakePrisms
  1. :: Name

    Type constructor name

  2. -> DecsQ
#

Generate a Prism for each constructor of a data type. Isos generated when possible. Reviews are created for constructors with existentially quantified constructors and GADTs.

e.g.

data FooBarBaz a
  = Foo Int
  | Bar a
  | Baz Int Char
makePrisms ''FooBarBaz

will create

_Foo :: Prism' (FooBarBaz a) Int
_Bar :: Prism (FooBarBaz a) (FooBarBaz b) a b
_Baz :: Prism' (FooBarBaz a) (Int, Char)
valuemakeClassyPrisms
  1. :: Name

    Type constructor name

  2. -> DecsQ
#

Generate a Prism for each constructor of a data type and combine them into a single class. No Isos are created. Reviews are created for constructors with existentially quantified constructors and GADTs.

e.g.

data FooBarBaz a
  = Foo Int
  | Bar a
  | Baz Int Char
makeClassyPrisms ''FooBarBaz

will create

class AsFooBarBaz s a | s -> a where
  _FooBarBaz :: Prism' s (FooBarBaz a)
  _Foo :: Prism' s Int
  _Bar :: Prism' s a
  _Baz :: Prism' s (Int,Char)

  _Foo = _FooBarBaz . _Foo
  _Bar = _FooBarBaz . _Bar
  _Baz = _FooBarBaz . _Baz

instance AsFooBarBaz (FooBarBaz a) a

Generate an As class of prisms. Names are selected by prefixing the constructor name with an underscore. Constructors with multiple fields will construct Prisms to tuples of those fields.

In the event that the name of a data type is also the name of one of its constructors, the name of the Prism generated for the data type will be prefixed with an extra _ (if the data type name is prefix) or . (if the name is infix) to disambiguate it from the Prism for the corresponding constructor. For example, this code:

data Quux = Quux Int | Fred Bool
makeClassyPrisms ''Quux

will create:

class AsQuux s where
  __Quux :: Prism' s Quux -- Data type prism
  _Quux :: Prism' s Int   -- Constructor prism
  _Fred :: Prism' s Bool

  _Quux = __Quux . _Quux
  _Fred = __Quux . _Fred

instance AsQuux Quux
classclass (forall a. Functor (p a)) => Bifunctor (p :: Type -> Type -> Type) where
#

A bifunctor is a type constructor that takes two type arguments and is a functor in both arguments. That is, unlike with Functor, a type constructor such as Either does not need to be partially applied for a Bifunctor instance, and the methods in this class permit mapping functions over the Left value or the Right value, or both at the same time.

Formally, the class Bifunctor represents a bifunctor from Hask -> Hask.

Intuitively it is a bifunctor where both the first and second arguments are covariant.

The class definition of a Bifunctor p uses the QuantifiedConstraints language extension to quantify over the first type argument a in its context. The context requires that p a must be a Functor for all a. In other words a partially applied Bifunctor must be a Functor. This makes Functor a superclass of Bifunctor such that a function with a Bifunctor constraint may use fmap in its implementation. Functor has been a quantified superclass of Bifunctor since base-4.18.0.0.

You can define a Bifunctor by either defining bimap or by defining both first and second. The second method must agree with fmap:

second ≡ fmap

From this it follows that:

second id ≡ id

If you supply bimap, you should ensure that:

bimap id id ≡ id

If you supply first and second, ensure:

first id ≡ id
second id ≡ id

If you supply both, you should also ensure:

bimap f g ≡ first f . second g

These ensure by parametricity:

bimap  (f . g) (h . i) ≡ bimap f h . bimap g i
first  (f . g) ≡ first  f . first  g
second (f . g) ≡ second f . second g

Methods

  • bimap :: (a -> b) -> (c -> d) -> p a c -> p b d

    Map over both arguments at the same time.

    bimap f g ≡ first f . second g
    Examples
    Example1 expression
    bimap toUpper (+1) ('j', 3)('J',4)
    Example1 expression
    bimap toUpper (+1) (Left 'j')Left 'J'
    Example1 expression
    bimap toUpper (+1) (Right 3)Right 4
Instances30Bifunctor, …
classclass Reversing t where
#

This class provides a generalized notion of list reversal extended to other containers.

Methods

Instances13Reversing, …
classclass Profunctor (p :: Type -> Type -> Type) where
#

Formally, the class Profunctor represents a profunctor from Hask -> Hask.

Intuitively it is a bifunctor where the first argument is contravariant and the second argument is covariant.

You can define a Profunctor by either defining dimap or by defining both lmap and rmap.

If you supply dimap, you should ensure that:

dimap id id ≡ id

If you supply lmap and rmap, ensure:

lmap id ≡ id
rmap id ≡ id

If you supply both, you should also ensure:

dimap f g ≡ lmap f . rmap g

These ensure by parametricity:

dimap (f . g) (h . i) ≡ dimap g h . dimap f i
lmap (f . g) ≡ lmap g . lmap f
rmap (f . g) ≡ rmap f . rmap g

Methods

  • dimap :: (a -> b) -> (c -> d) -> p b c -> p a d

    Map over both arguments at the same time.

    dimap f g ≡ lmap f . rmap g
  • lmap :: (a -> b) -> p b c -> p a c

    Map the first argument contravariantly.

    lmap f ≡ dimap f id
  • rmap :: (b -> c) -> p a b -> p a c

    Map the second argument covariantly.

    rmap ≡ dimap id
Instances46Profunctor, …
classclass (Applicative f, Distributive f, Traversable f) => Settable (f :: Type -> Type) where
#

Anything Settable must be isomorphic to the Identity Functor.

Instances3Settable
valuecloneIso :: AnIso s t a b -> Iso s t a b
#

Convert from AnIso back to any Iso.

This is useful when you need to store an isomorphism as a data type inside a container and later reconstitute it as an overloaded function.

See cloneLens or cloneTraversal for more information on why you might want to do this.

valuewithIso :: AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
#

Extract the two functions, one from s -> a and one from b -> t that characterize an Iso.

valueau :: Functor f => AnIso s t a b -> ((b -> t) -> f s) -> f a
#

Based on ala from Conor McBride's work on Epigram.

This version is generalized to accept any Iso, not just a newtype.

Example1 expression
au (_Wrapping Sum) foldMap [1,2,3,4]10

You may want to think of this combinator as having the following, simpler type:

au :: AnIso s t a b -> ((b -> t) -> e -> s) -> e -> a
au = xplat . from
valueauf :: (Functor f, Functor g) => AnIso s t a b -> (f t -> g s) -> f b -> g a
#

Based on ala' from Conor McBride's work on Epigram.

This version is generalized to accept any Iso, not just a newtype.

For a version you pass the name of the newtype constructor to, see alaf.

Example1 expression
auf (_Wrapping Sum) (foldMapOf both) Prelude.length ("hello","world")10

Mnemonically, the German auf plays a similar role to à la, and the combinator is au with an extra function argument:

auf :: Iso s t a b -> ((r -> t) -> e -> s) -> (r -> b) -> e -> a

but the signature is general.

Note: The direction of the Iso required for this function changed in lens 4.18 to match up with the behavior of au. For the old behavior use xplatf or for a version that is compatible across both old and new versions of lens you can just use coerce!

valuexplatf :: Optic (Costar f) g s t a b -> (f a -> g b) -> f s -> g t
#

xplatf = auf . from but with a nicer signature.

Example1 expression
xplatf (_Unwrapping Sum) (foldMapOf both) Prelude.length ("hello","world")10
xplatf :: Iso s t a b -> ((r -> a) -> e -> b) -> (r -> s) -> e -> t
valuenon :: Eq a => a -> Iso' (Maybe a) a
#

If v is an element of a type a, and a' is a sans the element v, then non v is an isomorphism from Maybe a' to a.

non ≡ non' . only

Keep in mind this is only a real isomorphism if you treat the domain as being Maybe (a sans v).

This is practically quite useful when you want to have a Data.Map.Map where all the entries should have non-zero values.

Example1 expression
Map.fromList [("hello",1)] & at "hello" . non 0 +~ 2fromList [("hello",3)]
Example1 expression
Map.fromList [("hello",1)] & at "hello" . non 0 -~ 1fromList []
Example1 expression
Map.fromList [("hello",1)] ^. at "hello" . non 01
Example1 expression
Map.fromList [] ^. at "hello" . non 00

This combinator is also particularly useful when working with nested maps.

e.g. When you want to create the nested Data.Map.Map when it is missing:

Example1 expression
Map.empty & at "hello" . non Map.empty . at "world" ?~ "!!!"fromList [("hello",fromList [("world","!!!")])]

and when have deleting the last entry from the nested Data.Map.Map mean that we should delete its entry from the surrounding one:

Example1 expression
Map.fromList [("hello",Map.fromList [("world","!!!")])] & at "hello" . non Map.empty . at "world" .~ NothingfromList []

It can also be used in reverse to exclude a given value:

Example1 expression
non 0 # rem 10 4Just 2
Example1 expression
non 0 # rem 10 5Nothing
valuenon' :: APrism' a () -> Iso' (Maybe a) a
#

non' p generalizes non (p # ()) to take any unit Prism

This function generates an isomorphism between Maybe (a | isn't p a) and a.

Example1 expression
Map.singleton "hello" Map.empty & at "hello" . non' _Empty . at "world" ?~ "!!!"fromList [("hello",fromList [("world","!!!")])]
Example1 expression
Map.fromList [("hello",Map.fromList [("world","!!!")])] & at "hello" . non' _Empty . at "world" .~ NothingfromList []
valueanon :: a -> (a -> Bool) -> Iso' (Maybe a) a
#

anon a p generalizes non a to take any value and a predicate.

This function assumes that p a holds True and generates an isomorphism between Maybe (a | not (p a)) and a.

Example1 expression
Map.empty & at "hello" . anon Map.empty Map.null . at "world" ?~ "!!!"fromList [("hello",fromList [("world","!!!")])]
Example1 expression
Map.fromList [("hello",Map.fromList [("world","!!!")])] & at "hello" . anon Map.empty Map.null . at "world" .~ NothingfromList []
valueenum :: Enum a => Iso' Int a
#

This isomorphism can be used to convert to or from an instance of Enum.

Example1 expression
LT^.from enum0
Example1 expression
97^.enum :: Char'a'

Note: this is only an isomorphism from the numeric range actually used and it is a bit of a pleasant fiction, since there are questionable Enum instances for Double, and Float that exist solely for [1.0 .. 4.0] sugar and the instances for those and Integer don't cover all values in their range.

valuecurried
  1. :: (Profunctor p, Functor f2)
  2. => p (a -> b -> c) (f2 (d -> e -> f1))
  3. -> p ((a, b) -> c) (f2 ((d, e) -> f1))
#

The canonical isomorphism for currying and uncurrying a function.

curried = iso curry uncurry
Example1 expression
(fst^.curried) 3 43
Example1 expression
view curried fst 3 43
valueflipped
  1. :: (Profunctor p, Functor f)
  2. => p (b -> a -> c) (f (b' -> a' -> c'))
  3. -> p (a -> b -> c) (f (a' -> b' -> c'))
#

The isomorphism for flipping a function.

Example1 expression
((,)^.flipped) 1 2(2,1)
valuereversed :: Reversing a => Iso' a a
#

An Iso between a list, ByteString, Text fragment, etc. and its reversal.

Example1 expression
"live" ^. reversed"evil"
Example1 expression
"live" & reversed %~ ('d':)"lived"
valueinvoluted :: (a -> a) -> Iso' a a
#

Given a function that is its own inverse, this gives you an Iso using it in both directions.

involuted ≡ join iso
Example1 expression
"live" ^. involuted reverse"evil"
Example1 expression
"live" & involuted reverse %~ ('d':)"lived"
valuemagma
  1. :: LensLike (Mafic a b) s t a b
  2. -> Iso s u (Magma Int t b a) (Magma j u c c)
#

This isomorphism can be used to inspect a Traversal to see how it associates the structure and it can also be used to bake the Traversal into a Magma so that you can traverse over it multiple times.

valuecoerced :: (Coercible s a, Coercible t b) => Iso s t a b
#

Data types that are representationally equal are isomorphic.

This is only available on GHC 7.8+

newtypenewtype Identity a
#

Identity functor and monad. (a non-strict monad)

Examples
Example1 expression
fmap (+1) (Identity 0)Identity 1
Example1 expression
Identity [1, 2, 3] <> Identity [4, 5, 6]Identity [1,2,3,4,5,6]
>>> do
      x <- Identity 10
      y <- Identity (x + 5)
      pure (x + y)
Identity 25

Constructors

Instances81Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
typetype ALens s t a b = LensLike (Pretext (->) a b) s t a b
#

When you see this as an argument to a function, it expects a Lens.

This type can also be used when you need to store a Lens in a container, since it is rank-1. You can turn them back into a Lens with cloneLens, or use it directly with combinators like storing and (^#).

valuelens :: (s -> a) -> (s -> b -> t) -> Lens s t a b
#

Build a Lens from a getter and a setter.

lens :: Functor f => (s -> a) -> (s -> b -> t) -> (a -> f b) -> s -> f t
Example1 expression
s ^. lens getter settergetter s
Example1 expression
s & lens getter setter .~ bsetter s b
Example1 expression
s & lens getter setter %~ fsetter s (f (getter s))
lens :: (s -> a) -> (s -> a -> s) -> Lens' s a
valueilens :: (s -> (i, a)) -> (s -> b -> t) -> IndexedLens i s t a b
#

Build an IndexedLens from a Control.Lens.Getter.Getter and a Control.Lens.Setter.Setter.

valueiplens :: (s -> a) -> (s -> b -> t) -> IndexPreservingLens s t a b
#

Build an index-preserving Lens from a Control.Lens.Getter.Getter and a Control.Lens.Setter.Setter.

valuewithLens :: ALens s t a b -> ((s -> a) -> (s -> b -> t) -> r) -> r
#

Obtain a getter and a setter from a lens, reversing lens.

valuechoosing
  1. :: Functor f
  2. => LensLike f s t a b
  3. -> LensLike f s' t' a b
  4. -> LensLike f (Either s s') (Either t t') a b
#

Merge two lenses, getters, setters, folds or traversals.

chosen ≡ choosing id id
choosing :: Control.Lens.Getter.Getter s a     -> Control.Lens.Getter.Getter s' a     -> Control.Lens.Getter.Getter (Either s s') a
choosing :: Control.Lens.Fold.Fold s a       -> Control.Lens.Fold.Fold s' a       -> Control.Lens.Fold.Fold (Either s s') a
choosing :: Lens' s a      -> Lens' s' a      -> Lens' (Either s s') a
choosing :: Control.Lens.Traversal.Traversal' s a -> Control.Lens.Traversal.Traversal' s' a -> Control.Lens.Traversal.Traversal' (Either s s') a
choosing :: Control.Lens.Setter.Setter' s a    -> Control.Lens.Setter.Setter' s' a    -> Control.Lens.Setter.Setter' (Either s s') a
valuechosen
  1. :: (Conjoined p, Functor f)
  2. => p a (f b)
  3. -> p (Either a a) (f (Either b b))
#

This is a Lens that updates either side of an Either, where both sides have the same type.

chosen ≡ choosing id id
Example1 expression
Left a^.chosena
Example1 expression
Right a^.chosena
Example1 expression
Right "hello"^.chosen"hello"
Example1 expression
Right a & chosen *~ bRight (a * b)
chosen :: Lens (Either a a) (Either b b) a b
chosen f (Left a)  = Left <$> f a
chosen f (Right a) = Right <$> f a
valuealongside
  1. :: LensLike (AlongsideLeft f b') s t a b
  2. -> LensLike (AlongsideRight f t) s' t' a' b'
  3. -> LensLike f (s, s') (t, t') (a, a') (b, b')
#

alongside makes a Lens from two other lenses or a Getter from two other getters by executing them on their respective halves of a product.

Example1 expression
(Left a, Right b)^.alongside chosen chosen(a,b)
Example1 expression
(Left a, Right b) & alongside chosen chosen .~ (c,d)(Left c,Right d)
alongside :: Lens   s t a b -> Lens   s' t' a' b' -> Lens   (s,s') (t,t') (a,a') (b,b')
alongside :: Getter s   a   -> Getter s'    a'    -> Getter (s,s')        (a,a')
valueinside
  1. :: Corepresentable p
  2. => ALens s t a b
  3. -> Lens (p e s) (p e t) (p e a) (p e b)
#

Lift a Lens so it can run under a function (or other corepresentable profunctor).

inside :: Lens s t a b -> Lens (e -> s) (e -> t) (e -> a) (e -> b)
Example1 expression
(\x -> (x-1,x+1)) ^. inside _1 $ 54
Example1 expression
runState (modify (1:) >> modify (2:)) ^. (inside _2) $ [][2,1]
value(<<<>:~) :: Semigroup m => LensLike' (Tuple2 m) s m -> m -> s -> (m, s)
#

(<>) a Semigroup value onto the front of the target of a Lens and return the old result. However, unlike (<<>~), it is prepended to the head side.

When you do not need the result of the operation, (<>:~) is more flexible.

value(<<<>:=)
  1. :: (MonadState s m, Semigroup r)
  2. => LensLike' (Tuple2 r) s r
  3. -> r
  4. -> m r
#

(<>) a Semigroup value onto the front of the target of a Lens into your Monad's state and return the old result. However, unlike (<<<>=), it is prepended to the head side.

When you do not need the result of the operation, (<>:=) is more flexible.

valuecloneLens :: ALens s t a b -> Lens s t a b
#

Cloning a Lens is one way to make sure you aren't given something weaker, such as a Control.Lens.Traversal.Traversal and can be used as a way to pass around lenses that have to be monomorphic in f.

Note: This only accepts a proper Lens.

Example1 expression
let example l x = set (cloneLens l) (x^.cloneLens l + 1) x in example _2 ("hello",1,"you")("hello",2,"you")
valueoverA :: Arrow ar => LensLike (Context a b) s t a b -> ar a b -> ar s t
#

over for Arrows.

Unlike over, overA can't accept a simple Control.Lens.Setter.Setter, but requires a full lens, or close enough.

Example1 expression
overA _1 ((+1) *** (+2)) ((1,2),6)((2,4),6)
overA :: Arrow ar => Lens s t a b -> ar a b -> ar s t
valuestoring :: ALens s t a b -> b -> s -> t
#

A version of set that works on ALens.

Example1 expression
storing _2 "world" ("hello","there")("hello","world")
valuedevoid :: Over p f Void Void a b
#

There is a field for every type in the Void. Very zen.

Example1 expression
[] & mapped.devoid +~ 1[]
Example1 expression
Nothing & mapped.devoid %~ absNothing
devoid :: Lens' Void a
valueunited :: Functor f => (() -> f ()) -> a -> f a
#

We can always retrieve a () from any type.

Example1 expression
"hello"^.united()
Example1 expression
"hello" & united .~ ()"hello"
valuehead1 :: Traversable1 t => Lens' (t a) a
#

A Lens focusing on the first element of a Traversable1 container.

Example1 expression
2 :| [3, 4] & head1 +~ 1012 :| [3,4]
Example1 expression
Identity True ^. head1True
valuelast1 :: Traversable1 t => Lens' (t a) a
#

A Lens focusing on the last element of a Traversable1 container.

Example1 expression
2 :| [3, 4] & last1 +~ 102 :| [3,14]
Example1 expression
Node 'a' [Node 'b' [], Node 'c' []] ^. last1'c'
valuefusing :: Functor f => LensLike (Yoneda f) s t a b -> LensLike f s t a b
#

Fuse a composition of lenses using Yoneda to provide fmap fusion.

In general, given a pair of lenses foo and bar

fusing (foo.bar) = foo.bar

however, foo and bar are either going to fmap internally or they are trivial.

fusing exploits the Yoneda lemma to merge these separate uses into a single fmap.

This is particularly effective when the choice of functor f is unknown at compile time or when the Lens foo.bar in the above description is recursive or complex enough to prevent inlining.

fusing :: Lens s t a b -> Lens s t a b
valuelevels
  1. :: Applicative f
  2. => Traversing (->) f s t a b
  3. -> IndexedLensLike Int f s t (Level () a) (Level () b)
#

This provides a breadth-first Traversal or Fold of the individual levels of any other Traversal or Fold via iterative deepening depth-first search. The levels are returned to you in a compressed format.

This can permit us to extract the levels directly:

Example1 expression
["hello","world"]^..levels (traverse.traverse)[Zero,Zero,One () 'h',Two 0 (One () 'e') (One () 'w'),Two 0 (One () 'l') (One () 'o'),Two 0 (One () 'l') (One () 'r'),Two 0 (One () 'o') (One () 'l'),One () 'd']

But we can also traverse them in turn:

Example1 expression
["hello","world"]^..levels (traverse.traverse).traverse"hewlolrold"

We can use this to traverse to a fixed depth in the tree of (<*>) used in the Traversal:

Example1 expression
["hello","world"] & taking 4 (levels (traverse.traverse)).traverse %~ toUpper["HEllo","World"]

Or we can use it to traverse the first n elements in found in that Traversal regardless of the depth at which they were found.

Example1 expression
["hello","world"] & taking 4 (levels (traverse.traverse).traverse) %~ toUpper["HELlo","World"]

The resulting Traversal of the levels which is indexed by the depth of each Level.

Example1 expression
["dog","cat"]^@..levels (traverse.traverse) <. traverse[(2,'d'),(3,'o'),(3,'c'),(4,'g'),(4,'a'),(5,'t')]
levels :: Traversal s t a b      -> IndexedTraversal Int s t (Level () a) (Level () b)
levels :: Fold s a               -> IndexedFold Int s (Level () a)

Note: Internally this is implemented by using an illegal Applicative, as it extracts information in an order that violates the Applicative laws.

valueilevels
  1. :: Applicative f
  2. => Traversing (Indexed i) f s t a b
  3. -> IndexedLensLike Int f s t (Level i a) (Level j b)
#

This provides a breadth-first Traversal or Fold of the individual levels of any other Traversal or Fold via iterative deepening depth-first search. The levels are returned to you in a compressed format.

This is similar to levels, but retains the index of the original IndexedTraversal, so you can access it when traversing the levels later on.

Example1 expression
["dog","cat"]^@..ilevels (traversed<.>traversed).itraversed[((0,0),'d'),((0,1),'o'),((1,0),'c'),((0,2),'g'),((1,1),'a'),((1,2),'t')]

The resulting Traversal of the levels which is indexed by the depth of each Level.

Example1 expression
["dog","cat"]^@..ilevels (traversed<.>traversed)<.>itraversed[((2,(0,0)),'d'),((3,(0,1)),'o'),((3,(1,0)),'c'),((4,(0,2)),'g'),((4,(1,1)),'a'),((5,(1,2)),'t')]
ilevels :: IndexedTraversal i s t a b      -> IndexedTraversal Int s t (Level i a) (Level i b)
ilevels :: IndexedFold i s a               -> IndexedFold Int s (Level i a)

Note: Internally this is implemented by using an illegal Applicative, as it extracts information in an order that violates the Applicative laws.

valuerewrite :: Plated a => (a -> Maybe a) -> a -> a
#

Rewrite by applying a rule everywhere you can. Ensures that the rule cannot be applied anywhere in the result:

propRewrite r x = all (Data.Just.isNothing . r) (universe (rewrite r x))

Usually transform is more appropriate, but rewrite can give better compositionality. Given two single transformations f and g, you can construct \a -> f a <|> g a which performs both rewrites until a fixed point.

valuerewriteOf :: ASetter a b a b -> (b -> Maybe a) -> a -> b
#

Rewrite by applying a rule everywhere you can. Ensures that the rule cannot be applied anywhere in the result:

propRewriteOf l r x = all (Data.Just.isNothing . r) (universeOf l (rewriteOf l r x))

Usually transformOf is more appropriate, but rewriteOf can give better compositionality. Given two single transformations f and g, you can construct \a -> f a <|> g a which performs both rewrites until a fixed point.

rewriteOf :: Control.Lens.Iso.Iso' a a       -> (a -> Maybe a) -> a -> a
rewriteOf :: Lens' a a      -> (a -> Maybe a) -> a -> a
rewriteOf :: Traversal' a a -> (a -> Maybe a) -> a -> a
rewriteOf :: Setter' a a    -> (a -> Maybe a) -> a -> a
valuerewriteM :: (Monad m, Plated a) => (a -> m (Maybe a)) -> a -> m a
#

Rewrite by applying a monadic rule everywhere you can. Ensures that the rule cannot be applied anywhere in the result.

valuerewriteMOf
  1. :: Monad m
  2. => LensLike (WrappedMonad m) a b a b
  3. -> b -> m (Maybe a)
  4. -> a
  5. -> m b
#

Rewrite by applying a monadic rule everywhere you recursing with a user-specified Traversal. Ensures that the rule cannot be applied anywhere in the result.

valueuniverse :: Plated a => a -> [a]
#

Retrieve all of the transitive descendants of a Plated container, including itself.

valueuniverseOf :: Getting (Endo [a]) a a -> a -> [a]
#

Given a Fold that knows how to locate immediate children, retrieve all of the transitive descendants of a node, including itself.

universeOf :: Fold a a -> a -> [a]
valueuniverseOn :: Plated a => Getting (Endo [a]) s a -> s -> [a]
#

Given a Fold that knows how to find Plated parts of a container retrieve them and all of their descendants, recursively.

valuecosmos :: Plated a => Fold a a
#

Fold over all transitive descendants of a Plated container, including itself.

valuetransform :: Plated a => (a -> a) -> a -> a
#

Transform every element in the tree, in a bottom-up manner.

For example, replacing negative literals with literals:

negLits = transform $ \x -> case x of
  Neg (Lit i) -> Lit (negate i)
  _           -> x
valuetransformM :: (Monad m, Plated a) => (a -> m a) -> a -> m a
#

Transform every element in the tree, in a bottom-up manner, monadically.

valueholes :: Plated a => a -> [Pretext (->) a a a]
#

The one-level version of context. This extracts a list of the immediate children as editable contexts.

Given a context you can use pos to see the values, peek at what the structure would be like with an edited result, or simply Control.Lens.Internal.Context.extract the original structure.

propChildren x = children l x == map pos (holes l x)
propId x = all (== x) [Control.Lens.Internal.Context.extract w | w <- holes l x]
holes = holesOf plate
valueholesOnOf
  1. :: Conjoined p
  2. => LensLike (Bazaar p r r) s t a b
  3. -> Over p (Bazaar p r r) a b r r
  4. -> s
  5. -> [Pretext p r r t]
#

Extract one level of holes from a container in a region specified by one Traversal, using another.

holesOnOf b l ≡ holesOf (b . l)
holesOnOf :: Iso' s a       -> Iso' a a                -> s -> [Pretext (->) a a s]
holesOnOf :: Lens' s a      -> Lens' a a               -> s -> [Pretext (->) a a s]
holesOnOf :: Traversal' s a -> Traversal' a a          -> s -> [Pretext (->) a a s]
holesOnOf :: Lens' s a      -> IndexedLens' i a a      -> s -> [Pretext (Indexed i) a a s]
holesOnOf :: Traversal' s a -> IndexedTraversal' i a a -> s -> [Pretext (Indexed i) a a s]
valuepara :: Plated a => (a -> [r] -> r) -> a -> r
#

Perform a fold-like computation on each value, technically a paramorphism.

para ≡ paraOf plate
valueparaOf :: Getting (Endo [a]) a a -> (a -> [r] -> r) -> a -> r
#

Perform a fold-like computation on each value, technically a paramorphism.

paraOf :: Fold a a -> (a -> [r] -> r) -> a -> r
valueparts :: Plated a => Lens' a [a]
#

The original uniplate combinator, implemented in terms of Plated as a Lens.

parts ≡ partsOf plate

The resulting Lens is safer to use as it ignores 'over-application' and deals gracefully with under-application, but it is only a proper Lens if you don't change the list length!

valuegplate :: (Generic a, GPlated a (Rep a)) => Traversal' a a
#

Implement plate operation for a type using its Generic instance.

Note: the behavior may be different than with uniplate in some special cases. gplate doesn't look through other types in a group of mutually recursive types.

For example consider mutually recursive even and odd natural numbers:

Example1 expression
data Even = Z | E Odd deriving (Show, Generic, Data); data Odd = O Even deriving (Show, Generic, Data)

Then uniplate, which is based on Data, finds all even numbers less or equal than four:

Example2 expressions
import Data.Data.Lens (uniplate)universeOf uniplate (E (O (E (O Z))))[E (O (E (O Z))),E (O Z),Z]

but gplate doesn't see through Odd.

Example1 expression
universeOf gplate (E (O (E (O Z))))[E (O (E (O Z)))]

If using Data is not an option, you can still write the traversal manually. It is sometimes useful to use helper traversals

Example1 expression
:{let oddeven :: Traversal' Odd Even    oddeven f (O n) = O <$> f n    evenplate :: Traversal' Even Even    evenplate f Z     = pure Z    evenplate f (E n) = E <$> oddeven f n:}
Example1 expression
universeOf evenplate (E (O (E (O Z))))[E (O (E (O Z))),E (O Z),Z]
classclass GPlated a (g :: k -> Type) where
#
Instances8GPlated, …
  • GPlated a U1Defined in lens-5.3.5 · Control.Lens.Plated
  • GPlated a V1Defined in lens-5.3.5 · Control.Lens.Plated
  • GPlated a (URec b)Defined in lens-5.3.5 · Control.Lens.Plated
  • GPlated a (K1 i a)Defined in lens-5.3.5 · Control.Lens.Plated
  • GPlated a (K1 i b)Defined in lens-5.3.5 · Control.Lens.Plated
  • (GPlated a f, GPlated a g) => GPlated a (f :*: g)Defined in lens-5.3.5 · Control.Lens.Plated
  • (GPlated a f, GPlated a g) => GPlated a (f :+: g)Defined in lens-5.3.5 · Control.Lens.Plated
  • GPlated a f => GPlated a (M1 i c f)Defined in lens-5.3.5 · Control.Lens.Plated
classclass GPlated1 (f :: k -> Type) (g :: k -> Type) where
#
Instances11GPlated1, …
valueprism :: (b -> t) -> (s -> Either t a) -> Prism s t a b
#

Build a Control.Lens.Prism.Prism.

Either t a is used instead of Maybe a to permit the types of s and t to differ.

valueprism' :: (b -> s) -> (s -> Maybe a) -> Prism s s a b
#

This is usually used to build a Prism', when you have to use an operation like cast which already returns a Maybe.

valuewithPrism :: APrism s t a b -> ((b -> t) -> (s -> Either t a) -> r) -> r
#

Convert APrism to the pair of functions that characterize it.

valueaside :: APrism s t a b -> Prism (e, s) (e, t) (e, a) (e, b)
#

Use a Prism to work over part of a structure.

valuebelow :: Traversable f => APrism' s a -> Prism' (f s) (f a)
#

lift a Prism through a Traversable functor, giving a Prism that matches only if all the elements of the container match the Prism.

Example1 expression
[Left 1, Right "foo", Left 4, Right "woot"]^..below _Right[]
Example1 expression
[Right "hail hydra!", Right "foo", Right "blah", Right "woot"]^..below _Right[["hail hydra!","foo","blah","woot"]]
valueisn't :: APrism s t a b -> s -> Bool
#

Check to see if this Prism doesn't match.

Example1 expression
isn't _Left (Right 12)True
Example1 expression
isn't _Left (Left 12)False
Example1 expression
isn't _Empty []False
isn't = not . Control.Lens.Extra.is
isn't = hasn't
valuematching :: APrism s t a b -> s -> Either t a
#

Retrieve the value targeted by a Prism or return the original value while allowing the type to change if it does not match.

Example1 expression
matching _Just (Just 12)Right 12
Example1 expression
matching _Just (Nothing :: Maybe Int) :: Either (Maybe Bool) IntLeft Nothing
valuematching' :: LensLike (Either a) s t a b -> s -> Either t a
#

Like matching, but also works for combinations of Lens and Prisms, and also Traversals.

Example1 expression
matching' (_2 . _Just) ('x', Just True)Right True
Example1 expression
matching' (_2 . _Just) ('x', Nothing :: Maybe Int) :: Either (Char, Maybe Bool) IntLeft ('x',Nothing)
Example1 expression
matching' traverse "" :: Either [Int] CharLeft []
Example1 expression
matching' traverse "xyz" :: Either [Int] CharRight 'x'
value_Left
  1. :: (Choice p, Applicative f)
  2. => p a (f b)
  3. -> p (Either a c) (f (Either b c))
#

This Prism provides a Traversal for tweaking the Left half of an Either:

Example1 expression
over _Left (+1) (Left 2)Left 3
Example1 expression
over _Left (+1) (Right 2)Right 2
Example1 expression
Right 42 ^._Left :: String""
Example1 expression
Left "hello" ^._Left"hello"

It also can be turned around to obtain the embedding into the Left half of an Either:

Example1 expression
_Left # 5Left 5
Example1 expression
5^.re _LeftLeft 5
value_Right
  1. :: (Choice p, Applicative f)
  2. => p a (f b)
  3. -> p (Either c a) (f (Either c b))
#

This Prism provides a Traversal for tweaking the Right half of an Either:

Example1 expression
over _Right (+1) (Left 2)Left 2
Example1 expression
over _Right (+1) (Right 2)Right 3
Example1 expression
Right "hello" ^._Right"hello"
Example1 expression
Left "hello" ^._Right :: [Double][]

It also can be turned around to obtain the embedding into the Right half of an Either:

Example1 expression
_Right # 5Right 5
Example1 expression
5^.re _RightRight 5
value_Just :: (Choice p, Applicative f) => p a (f b) -> p (Maybe a) (f (Maybe b))
#

This Prism provides a Traversal for tweaking the target of the value of Just in a Maybe.

Example1 expression
over _Just (+1) (Just 2)Just 3

Unlike traverse this is a Prism, and so you can use it to inject as well:

Example1 expression
_Just # 5Just 5
Example1 expression
5^.re _JustJust 5

Interestingly,

m ^? _Just ≡ m
Example1 expression
Just x ^? _JustJust x
Example1 expression
Nothing ^? _JustNothing
value_Nothing
  1. :: (Choice p, Applicative f)
  2. => p () (f ())
  3. -> p (Maybe a) (f (Maybe a))
#

This Prism provides the Traversal of a Nothing in a Maybe.

Example1 expression
Nothing ^? _NothingJust ()
Example1 expression
Just () ^? _NothingNothing

But you can turn it around and use it to construct Nothing as well:

Example1 expression
_Nothing # ()Nothing
value_Show :: (Read a, Show a) => Prism' String a
#

This is an improper prism for text formatting based on Read and Show.

This Prism is "improper" in the sense that it normalizes the text formatting, but round tripping is idempotent given sane Read/Show instances.

Example1 expression
_Show # 2"2"
Example1 expression
"EQ" ^? _Show :: Maybe OrderingJust EQ
_Show ≡ prism' show readMaybe
valueonly :: Eq a => a -> Prism' a ()
#

This Prism compares for exact equality with a given value.

Example1 expression
only 4 # ()4
Example1 expression
5 ^? only 4Nothing
valuenearly :: a -> (a -> Bool) -> Prism' a ()
#

This Prism compares for approximate equality with a given value and a predicate for testing, an example where the value is the empty list and the predicate checks that a list is empty (same as _Empty with the AsEmpty list instance):

Example2 expressions
nearly [] null # ()[][1,2,3,4] ^? nearly [] nullNothing
nearly [] Prelude.null :: Prism' [a] ()

To comply with the Prism laws the arguments you supply to nearly a p are somewhat constrained.

We assume p x holds iff x ≡ a. Under that assumption then this is a valid Prism.

This is useful when working with a type where you can test equality for only a subset of its values, and the prism selects such a value.

classclass Prefixed t where
#

Methods

  • prefixed :: t -> Prism' t t

    A Prism stripping a prefix from a sequence when used as a Traversal, or prepending that prefix when run backwards:

    Example1 expression
    "preview" ^? prefixed "pre"Just "view"
    Example1 expression
    "review" ^? prefixed "pre"Nothing
    Example1 expression
    prefixed "pre" # "amble""preamble"
Instances5Prefixed
classclass Suffixed t where
#

Methods

  • suffixed :: t -> Prism' t t

    A Prism stripping a suffix from a sequence when used as a Traversal, or appending that suffix when run backwards:

    Example1 expression
    "review" ^? suffixed "view"Just "re"
    Example1 expression
    "review" ^? suffixed "tire"Nothing
    Example1 expression
    suffixed ".o" # "hello""hello.o"
Instances5Suffixed
classclass Profunctor p => Choice (p :: Type -> Type -> Type) where
#

The generalization of Costar of Functor that is strong with respect to Either.

Note: This is also a notion of strength, except with regards to another monoidal structure that we can choose to equip Hask with: the cocartesian coproduct.

Methods

Instances28Choice, …
typetype LensLike (f :: k -> Type) s (t :: k) a (b :: k) = (a -> f b) -> s -> f t
#

Many combinators that accept a Lens can also accept a Traversal in limited situations.

They do so by specializing the type of Functor that they require of the caller.

If a function accepts a LensLike f s t a b for some Functor f, then they may be passed a Lens.

Further, if f is an Applicative, they may also be passed a Traversal.

typetype ASetter s t a b = (a -> Identity b) -> s -> Identity t
#

Running a Setter instantiates it to a concrete type.

When consuming a setter directly to perform a mapping, you can use this type, but most user code will not need to use this type.

valuecloneTraversal :: ATraversal s t a b -> Traversal s t a b
#

A Traversal is completely characterized by its behavior on a Bazaar.

Cloning a Traversal is one way to make sure you aren't given something weaker, such as a Fold and can be used as a way to pass around traversals that have to be monomorphic in f.

Note: This only accepts a proper Traversal (or Lens). To clone a Lens as such, use cloneLens.

Note: It is usually better to use ReifiedTraversal and runTraversal than to cloneTraversal. The former can execute at full speed, while the latter needs to round trip through the Bazaar.

Example2 expressions
let foo l a = (view (getting (cloneTraversal l)) a, set (cloneTraversal l) 10 a)foo both ("hello","world")("helloworld",(10,10))
cloneTraversal :: LensLike (Bazaar (->) a b) s t a b -> Traversal s t a b
valuereview :: MonadReader b m => AReview t b -> m t
#

This can be used to turn an Control.Lens.Iso.Iso or Prism around and view a value (or the current environment) through it the other way.

review ≡ view . re
review . unto ≡ id
Example1 expression
review _Left "mustard"Left "mustard"
Example1 expression
review (unto succ) 56

Usually review is used in the (->) Monad with a Prism or Control.Lens.Iso.Iso, in which case it may be useful to think of it as having one of these more restricted type signatures:

review :: Iso' s a   -> a -> s
review :: Prism' s a -> a -> s

However, when working with a Monad transformer stack, it is sometimes useful to be able to review the current environment, in which case it may be beneficial to think of it as having one of these slightly more liberal type signatures:

review :: MonadReader a m => Iso' s a   -> m s
review :: MonadReader a m => Prism' s a -> m s
valuereviews :: MonadReader b m => AReview t b -> (t -> r) -> m r
#

This can be used to turn an Control.Lens.Iso.Iso or Prism around and view a value (or the current environment) through it the other way, applying a function.

reviews ≡ views . re
reviews (unto f) g ≡ g . f
Example1 expression
reviews _Left isRight "mustard"False
Example1 expression
reviews (unto succ) (*2) 38

Usually this function is used in the (->) Monad with a Prism or Control.Lens.Iso.Iso, in which case it may be useful to think of it as having one of these more restricted type signatures:

reviews :: Iso' s a   -> (s -> r) -> a -> r
reviews :: Prism' s a -> (s -> r) -> a -> r

However, when working with a Monad transformer stack, it is sometimes useful to be able to review the current environment, in which case it may be beneficial to think of it as having one of these slightly more liberal type signatures:

reviews :: MonadReader a m => Iso' s a   -> (s -> r) -> m r
reviews :: MonadReader a m => Prism' s a -> (s -> r) -> m r
typetype AnIndexedSetter i s t a b = Indexed i a (Identity b) -> s -> Identity t
#

Running an IndexedSetter instantiates it to a concrete type.

When consuming a setter directly to perform a mapping, you can use this type, but most user code will not need to use this type.

typetype Setting (p :: Type -> Type -> Type) s t a b = p a (Identity b) -> s -> Identity t
#

This is a convenient alias when defining highly polymorphic code that takes both ASetter and AnIndexedSetter as appropriate. If a function takes this it is expecting one of those two things based on context.

typetype Setting' (p :: Type -> Type -> Type) s a = Setting p s s a a
#

This is a convenient alias when defining highly polymorphic code that takes both ASetter' and AnIndexedSetter' as appropriate. If a function takes this it is expecting one of those two things based on context.

valuemapped :: Functor f => Setter (f a) (f b) a b
#

This Setter can be used to map over all of the values in a Functor.

fmap ≡ over mapped
fmapDefault ≡ over traverse
(<$) ≡ set mapped
Example1 expression
over mapped f [a,b,c][f a,f b,f c]
Example1 expression
over mapped (+1) [1,2,3][2,3,4]
Example1 expression
set mapped x [a,b,c][x,x,x]
Example1 expression
[[a,b],[c]] & mapped.mapped +~ x[[a + x,b + x],[c + x]]
Example1 expression
over (mapped._2) length [("hello","world"),("leaders","!!!")][("hello",5),("leaders",3)]
mapped :: Functor f => Setter (f a) (f b) a b

If you want an IndexPreservingSetter use setting fmap.

valuecontramapped :: Contravariant f => Setter (f b) (f a) a b
#

This Setter can be used to map over all of the inputs to a Contravariant.

contramap ≡ over contramapped
Example1 expression
getPredicate (over contramapped (*2) (Predicate even)) 5True
Example1 expression
getOp (over contramapped (*5) (Op show)) 100"500"
Example1 expression
Prelude.map ($ 1) $ over (mapped . _Unwrapping' Op . contramapped) (*12) [(*2),(+1),(^3)][24,13,1728]
valueargument :: Profunctor p => Setter (p b r) (p a r) a b
#

This Setter can be used to map over the input of a Profunctor.

The most common Profunctor to use this with is (->).

Example1 expression
(argument %~ f) g xg (f x)
Example1 expression
(argument %~ show) length [1,2,3]7
Example1 expression
(argument %~ f) h x yh (f x) y

Map over the argument of the result of a function -- i.e., its second argument:

Example1 expression
(mapped.argument %~ f) h x yh x (f y)
argument :: Setter (b -> r) (a -> r) a b
valueover :: ASetter s t a b -> (a -> b) -> s -> t
#

Modify the target of a Lens or all the targets of a Setter or Traversal with a function.

fmap ≡ over mapped
fmapDefault ≡ over traverse
sets . over ≡ id
over . sets ≡ id

Given any valid Setter l, you can also rely on the law:

over l f . over l g = over l (f . g)

e.g.

Example1 expression
over mapped f (over mapped g [a,b,c]) == over mapped (f . g) [a,b,c]True

Another way to view over is to say that it transforms a Setter into a "semantic editor combinator".

Example1 expression
over mapped f (Just a)Just (f a)
Example1 expression
over mapped (*10) [1,2,3][10,20,30]
Example1 expression
over _1 f (a,b)(f a,b)
Example1 expression
over _1 show (10,20)("10",20)
over :: Setter s t a b -> (a -> b) -> s -> t
over :: ASetter s t a b -> (a -> b) -> s -> t
valueset :: ASetter s t a b -> b -> s -> t
#

Replace the target of a Lens or all of the targets of a Setter or Traversal with a constant value.

(<$) ≡ set mapped
Example1 expression
set _2 "hello" (1,())(1,"hello")
Example1 expression
set mapped () [1,2,3,4][(),(),(),()]

Note: Attempting to set a Fold or Getter will fail at compile time with an relatively nice error message.

set :: Setter s t a b    -> b -> s -> t
set :: Iso s t a b       -> b -> s -> t
set :: Lens s t a b      -> b -> s -> t
set :: Traversal s t a b -> b -> s -> t
valueassign :: MonadState s m => ASetter s s a b -> b -> m ()
#

Replace the target of a Lens or all of the targets of a Setter or Traversal in our monadic state with a new value, irrespective of the old.

This is an alias for (.=).

Example1 expression
execState (do assign _1 c; assign _2 d) (a,b)(c,d)
Example1 expression
execState (both .= c) (a,b)(c,c)
assign :: MonadState s m => Iso' s a       -> a -> m ()
assign :: MonadState s m => Lens' s a      -> a -> m ()
assign :: MonadState s m => Traversal' s a -> a -> m ()
assign :: MonadState s m => Setter' s a    -> a -> m ()
valuelocally :: MonadReader s m => ASetter s s a b -> (a -> b) -> m r -> m r
#

Modify the value of the Reader environment associated with the target of a Setter, Lens, or Traversal.

locally l id a ≡ a
locally l f . locally l g ≡ locally l (f . g)
Example1 expression
(1,1) & locally _1 (+1) (uncurry (+))3
Example1 expression
"," & locally ($) ("Hello" <>) (<> " world!")"Hello, world!"
locally :: MonadReader s m => Iso s s a b       -> (a -> b) -> m r -> m r
locally :: MonadReader s m => Lens s s a b      -> (a -> b) -> m r -> m r
locally :: MonadReader s m => Traversal s s a b -> (a -> b) -> m r -> m r
locally :: MonadReader s m => Setter s s a b    -> (a -> b) -> m r -> m r
valueilocally
  1. :: MonadReader s m
  2. => AnIndexedSetter i s s a b
  3. -> i -> a -> b
  4. -> m r
  5. -> m r
#

This is a generalization of locally that allows one to make indexed local changes to a Reader environment associated with the target of a Setter, Lens, or Traversal.

locally l f ≡ ilocally l f . const
ilocally l f ≡ locally l f . Indexed
ilocally :: MonadReader s m => IndexedLens s s a b      -> (i -> a -> b) -> m r -> m r
ilocally :: MonadReader s m => IndexedTraversal s s a b -> (i -> a -> b) -> m r -> m r
ilocally :: MonadReader s m => IndexedSetter s s a b    -> (i -> a -> b) -> m r -> m r
valueset' :: ASetter' s a -> a -> s -> s
#

Replace the target of a Lens or all of the targets of a Setter' or Traversal with a constant value, without changing its type.

This is a type restricted version of set, which retains the type of the original.

Example1 expression
set' mapped x [a,b,c,d][x,x,x,x]
Example1 expression
set' _2 "hello" (1,"world")(1,"hello")
Example1 expression
set' mapped 0 [1,2,3,4][0,0,0,0]

Note: Attempting to adjust set' a Fold or Getter will fail at compile time with an relatively nice error message.

set' :: Setter' s a    -> a -> s -> s
set' :: Iso' s a       -> a -> s -> s
set' :: Lens' s a      -> a -> s -> s
set' :: Traversal' s a -> a -> s -> s
valueassignA :: Arrow p => ASetter s t a b -> p s b -> p s t
#

Run an arrow command and use the output to set all the targets of a Lens, Setter or Traversal to the result.

assignA can be used very similarly to (<~), except that the type of the object being modified can change; for example:

runKleisli action ((), (), ()) where
  action =      assignA _1 (Kleisli (const getVal1))
           >>> assignA _2 (Kleisli (const getVal2))
           >>> assignA _3 (Kleisli (const getVal3))
  getVal1 :: Either String Int
  getVal1 = ...
  getVal2 :: Either String Bool
  getVal2 = ...
  getVal3 :: Either String Char
  getVal3 = ...

has the type Either String (Int, Bool, Char)

assignA :: Arrow p => Iso s t a b       -> p s b -> p s t
assignA :: Arrow p => Lens s t a b      -> p s b -> p s t
assignA :: Arrow p => Traversal s t a b -> p s b -> p s t
assignA :: Arrow p => Setter s t a b    -> p s b -> p s t
valuemakeLensesFor :: [(String, String)] -> Name -> DecsQ
#

Derive lenses and traversals, specifying explicit pairings of (fieldName, lensName).

If you map multiple names to the same label, and it is present in the same constructor then this will generate a Traversal.

e.g.

makeLensesFor [("_foo", "fooLens"), ("baz", "lbaz")] ''Foo
makeLensesFor [("_barX", "bar"), ("_barY", "bar")] ''Bar
valuemakeClassy :: Name -> DecsQ
#

Make lenses and traversals for a type, and create a class when the type has no arguments.

e.g.

data Foo = Foo { _fooX, _fooY :: Int }
makeClassy ''Foo

will create

class HasFoo t where
  foo :: Lens' t Foo
  fooX :: Lens' t Int
  fooX = foo . go where go f (Foo x y) = (\x' -> Foo x' y) <$> f x
  fooY :: Lens' t Int
  fooY = foo . go where go f (Foo x y) = (\y' -> Foo x y') <$> f y
instance HasFoo Foo where
  foo = id
makeClassy = makeLensesWith classyRules
valuemakeClassy_ :: Name -> DecsQ
#

Make lenses and traversals for a type, and create a class when the type has no arguments. Works the same as makeClassy except that (a) it expects that record field names do not begin with an underscore, (b) all record fields are made into lenses, and (c) the resulting lens is prefixed with an underscore.

valuemakeFields :: Name -> DecsQ
#

Generate overloaded field accessors.

e.g

data Foo a = Foo { _fooX :: Int, _fooY :: a }
newtype Bar = Bar { _barX :: Char }
makeFields ''Foo
makeFields ''Bar

will create

_fooXLens :: Lens' (Foo a) Int
_fooYLens :: Lens (Foo a) (Foo b) a b
class HasX s a | s -> a where
  x :: Lens' s a
instance HasX (Foo a) Int where
  x = _fooXLens
class HasY s a | s -> a where
  y :: Lens' s a
instance HasY (Foo a) a where
  y = _fooYLens
_barXLens :: Iso' Bar Char
instance HasX Bar Char where
  x = _barXLens

For details, see camelCaseFields.

makeFields = makeLensesWith defaultFieldRules

Generate overloaded field accessors based on field names which are only prefixed with an underscore (e.g. _name), not additionally with the type name (e.g. _fooName).

This might be the desired behaviour in case the DuplicateRecordFields language extension is used in order to get rid of the necessity to prefix each field name with the type name.

As an example:

data Foo a  = Foo { _x :: Int, _y :: a }
newtype Bar = Bar { _x :: Char }
makeFieldsNoPrefix ''Foo
makeFieldsNoPrefix ''Bar

will create classes

class HasX s a | s -> a where
  x :: Lens' s a
class HasY s a | s -> a where
  y :: Lens' s a

together with instances

instance HasX (Foo a) Int
instance HasY (Foo a) a where
instance HasX Bar Char where

For details, see classUnderscoreNoPrefixFields.

makeFieldsNoPrefix = makeLensesWith classUnderscoreNoPrefixFields
valuemakeFieldsId :: Name -> DecsQ
#

Generate overloaded field accessors, using exactly the same names as the underlying fields. Intended for use with the NoFieldSelectors and DuplicateRecordFields language extensions.

As an example:

data Foo a  = Foo { x :: Int, y :: a }
newtype Bar = Bar { x :: Char }
makeFieldsId ''Foo
makeFieldsId ''Bar

will create classes

class HasX s a | s -> a where
  x :: Lens' s a
class HasY s a | s -> a where
  y :: Lens' s a

together with instances

instance HasX (Foo a) Int
instance HasY (Foo a) a where
instance HasX Bar Char where
makeFieldsId = makeLensesWith classIdFields
valuedeclareLenses :: DecsQ -> DecsQ
#

Make lenses for all records in the given declaration quote. All record syntax in the input will be stripped off.

e.g.

declareLenses [d|
  data Foo = Foo { fooX, fooY :: Int }
    deriving Show
  |]

will create

data Foo = Foo Int Int deriving Show
fooX, fooY :: Lens' Foo Int
valuedeclareClassy :: DecsQ -> DecsQ
#

For each record in the declaration quote, make lenses and traversals for it, and create a class when the type has no arguments. All record syntax in the input will be stripped off.

e.g.

declareClassy [d|
  data Foo = Foo { fooX, fooY :: Int }
    deriving Show
  |]

will create

data Foo = Foo Int Int deriving Show
class HasFoo t where
  foo :: Lens' t Foo
instance HasFoo Foo where foo = id
fooX, fooY :: HasFoo t => Lens' t Int
valuedeclarePrisms :: DecsQ -> DecsQ
#

Generate a Prism for each constructor of each data type.

e.g.

declarePrisms [d|
  data Exp = Lit Int | Var String | Lambda{ bound::String, body::Exp }
  |]

will create

data Exp = Lit Int | Var String | Lambda { bound::String, body::Exp }
_Lit :: Prism' Exp Int
_Var :: Prism' Exp String
_Lambda :: Prism' Exp (String, Exp)

Field rules for fields in the form prefixFieldname or _prefixFieldname If you want all fields to be lensed, then there is no reason to use an _ before the prefix. If any of the record fields leads with an _ then it is assume a field without an _ should not have a lens created.

Note: The prefix must be the same as the typename (with the first letter lowercased). This is a change from lens versions before lens 4.5. If you want the old behaviour, use makeLensesWith abbreviatedFields

Field rules for fields in the form _fieldname (the leading underscore is mandatory).

Note: The primary difference to camelCaseFields is that for classUnderscoreNoPrefixFields the field names are not expected to be prefixed with the type name. This might be the desired behaviour when the DuplicateRecordFields extension is enabled.

Field rules fields in the form prefixFieldname or _prefixFieldname If you want all fields to be lensed, then there is no reason to use an _ before the prefix. If any of the record fields leads with an _ then it is assume a field without an _ should not have a lens created.

Note that prefix may be any string of characters that are not uppercase letters. (In particular, it may be arbitrary string of lowercase letters and numbers) This is the behavior that defaultFieldRules had in lens 4.4 and earlier.

Field rules for fields whose names are to be used verbatim, with no prefixes, no underscores, no transformations of any kind.

Indicate whether or not to supply the signatures for the generated lenses.

Disabling this can be useful if you want to provide a more restricted type signature or if you want to supply hand-written haddocks.

Generate optics using lazy pattern matches. This can allow fields of an undefined value to be initialized with lenses:

data Foo = Foo {_x :: Int, _y :: Bool}
  deriving Show

makeLensesWith (lensRules & generateLazyPatterns .~ True) ''Foo
> undefined & x .~ 8 & y .~ True
Foo {_x = 8, _y = True}

The downside of this flag is that it can lead to space-leaks and code-size/compile-time increases when generated for large records. By default this flag is turned off, and strict optics are generated.

When using lazy optics the strict optic can be recovered by composing with $!:

strictOptic = ($!) . lazyOptic
typetype IndexedTraversal i s t a b = forall (p :: Type -> Type -> Type) (f :: Type -> Type). (Indexable i p, Applicative f) => p a (f b) -> s -> f t
#

Every IndexedTraversal is a valid Traversal or Control.Lens.Fold.IndexedFold.

The Indexed constraint is used to allow an IndexedTraversal to be used directly as a Traversal.

The Traversal laws are still required to hold.

In addition, the index i should satisfy the requirement that it stays unchanged even when modifying the value a, otherwise traversals like indices break the Traversal laws.

valuetraversal :: ((a -> f b) -> s -> f t) -> LensLike f s t a b
#

Build a Traversal by providing a function which specifies the elements you wish to focus.

The caller provides a function of type:

Applicative f => (a -> f b) -> s -> f t

Which is a higher order function which accepts a "focusing function" and applies it to all desired focuses within s, then constructs a t using the Applicative instance of f.

Only elements which are "focused" using the focusing function will be targeted by the resulting traversal.

For example, we can explicitly write a traversal which targets the first and third elements of a tuple like this:

firstAndThird :: Traversal (a, x, a) (b, x, b) a b
firstAndThird = traversal go
  where
    go :: Applicative f => (a -> f b) -> (a, x, a) -> f (b, x, b)
    go focus (a, x, a') = liftA3 (,,) (focus a) (pure x) (focus a')
Example1 expression
(1,"two",3) & firstAndThird *~ 10(10,"two",30)
Example1 expression
over firstAndThird length ("one",2,"three")(3,2,5)

We can re-use existing Traversals when writing new ones by passing our focusing function along to them. This example re-uses traverse to focus all elements in a list which is embedded in a tuple. This traversal could also be written simply as _2 . traverse.

selectNested :: Traversal (x, [a]) (x, [b]) a b
selectNested = traversal go
  where
    go :: Applicative f => (a -> f b) -> (x, [a]) -> f (x, [b])
    go focus (x, as) = liftA2 (,) (pure x) (traverse focus as)
Example1 expression
selectNested .~ "hello" $ (1,[2,3,4,5])(1,["hello","hello","hello","hello"])
Example1 expression
(1,[2,3,4,5]) & selectNested *~ 3(1,[6,9,12,15])

Note that the traversal function actually just returns the same function you pass to it. The function it accepts is in fact a valid traversal all on its own! The use of traversal does nothing except verify that the function it is passed matches the signature of a valid traversal. One could remove the traversal combinator from either of the last two examples and use the definition of go directly with no change in behaviour.

This function exists for consistency with the lens, prism and iso constructors as well as to serve as a touchpoint for beginners who wish to construct their own traversals but are uncertain how to do so.

valuetraverseOf :: LensLike f s t a b -> (a -> f b) -> s -> f t
#

Map each element of a structure targeted by a Lens or Traversal, evaluate these actions from left to right, and collect the results.

This function is only provided for consistency, id is strictly more general.

Example1 expression
traverseOf each print (1,2,3)123((),(),())
traverseOf ≡ id
itraverseOf l ≡ traverseOf l . Indexed
itraverseOf itraversed ≡ itraverse

This yields the obvious law:

traverse ≡ traverseOf traverse
traverseOf :: Functor f     => Iso s t a b        -> (a -> f b) -> s -> f t
traverseOf :: Functor f     => Lens s t a b       -> (a -> f b) -> s -> f t
traverseOf :: Apply f       => Traversal1 s t a b -> (a -> f b) -> s -> f t
traverseOf :: Applicative f => Traversal s t a b  -> (a -> f b) -> s -> f t
valueforOf :: LensLike f s t a b -> s -> (a -> f b) -> f t
#

A version of traverseOf with the arguments flipped, such that:

Example1 expression
forOf each (1,2,3) print123((),(),())

This function is only provided for consistency, flip is strictly more general.

forOf ≡ flip
forOf ≡ flip . traverseOf
for ≡ forOf traverse
ifor l s ≡ for l s . Indexed
forOf :: Functor f => Iso s t a b -> s -> (a -> f b) -> f t
forOf :: Functor f => Lens s t a b -> s -> (a -> f b) -> f t
forOf :: Applicative f => Traversal s t a b -> s -> (a -> f b) -> f t
valuemapMOf :: LensLike (WrappedMonad m) s t a b -> (a -> m b) -> s -> m t
#

Map each element of a structure targeted by a Lens to a monadic action, evaluate these actions from left to right, and collect the results.

Example1 expression
mapMOf both (\x -> [x, x + 1]) (1,3)[(1,3),(1,4),(2,3),(2,4)]
mapM ≡ mapMOf traverse
imapMOf l ≡ forM l . Indexed
mapMOf :: Monad m => Iso s t a b       -> (a -> m b) -> s -> m t
mapMOf :: Monad m => Lens s t a b      -> (a -> m b) -> s -> m t
mapMOf :: Monad m => Traversal s t a b -> (a -> m b) -> s -> m t
valueforMOf :: LensLike (WrappedMonad m) s t a b -> s -> (a -> m b) -> m t
#

forMOf is a flipped version of mapMOf, consistent with the definition of forM.

Example1 expression
forMOf both (1,3) $ \x -> [x, x + 1][(1,3),(1,4),(2,3),(2,4)]
forM ≡ forMOf traverse
forMOf l ≡ flip (mapMOf l)
iforMOf l s ≡ forM l s . Indexed
forMOf :: Monad m => Iso s t a b       -> s -> (a -> m b) -> m t
forMOf :: Monad m => Lens s t a b      -> s -> (a -> m b) -> m t
forMOf :: Monad m => Traversal s t a b -> s -> (a -> m b) -> m t
valuesequenceOf :: LensLike (WrappedMonad m) s t (m b) b -> s -> m t
#

Sequence the (monadic) effects targeted by a Lens in a container from left to right.

Example1 expression
sequenceOf each ([1,2],[3,4],[5,6])[(1,3,5),(1,3,6),(1,4,5),(1,4,6),(2,3,5),(2,3,6),(2,4,5),(2,4,6)]
sequence ≡ sequenceOf traverse
sequenceOf l ≡ mapMOf l id
sequenceOf l ≡ unwrapMonad . l WrapMonad
sequenceOf :: Monad m => Iso s t (m b) b       -> s -> m t
sequenceOf :: Monad m => Lens s t (m b) b      -> s -> m t
sequenceOf :: Monad m => Traversal s t (m b) b -> s -> m t
valuemapAccumLOf
  1. :: LensLike (State acc) s t a b
  2. -> acc -> a -> (acc, b)
  3. -> acc
  4. -> s
  5. -> (acc, t)
#

This generalizes Data.Traversable.mapAccumL to an arbitrary Traversal.

mapAccumL ≡ mapAccumLOf traverse

mapAccumLOf accumulates State from left to right.

mapAccumLOf :: Iso s t a b       -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
mapAccumLOf :: Lens s t a b      -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
mapAccumLOf :: Traversal s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
mapAccumLOf :: LensLike (State acc) s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
mapAccumLOf l f acc0 s = swap (runState (l (a -> state (acc -> swap (f acc a))) s) acc0)
valuemapAccumROf
  1. :: LensLike (Backwards (State acc)) s t a b
  2. -> acc -> a -> (acc, b)
  3. -> acc
  4. -> s
  5. -> (acc, t)
#

This generalizes Data.Traversable.mapAccumR to an arbitrary Traversal.

mapAccumR ≡ mapAccumROf traverse

mapAccumROf accumulates State from right to left.

mapAccumROf :: Iso s t a b       -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
mapAccumROf :: Lens s t a b      -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
mapAccumROf :: Traversal s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
mapAccumROf :: LensLike (Backwards (State acc)) s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
valuefailover
  1. :: Alternative m
  2. => LensLike (Tuple2 Any) s t a b
  3. -> a -> b
  4. -> s
  5. -> m t
#

Try to map a function over this Traversal, failing if the Traversal has no targets.

Example1 expression
failover (element 3) (*2) [1,2] :: Maybe [Int]Nothing
Example1 expression
failover _Left (*2) (Right 4) :: Maybe (Either Int Int)Nothing
Example1 expression
failover _Right (*2) (Right 4) :: Maybe (Either Int Int)Just (Right 8)
failover :: Alternative m => Traversal s t a b -> (a -> b) -> s -> m t
valuepartsOf :: Functor f => Traversing (->) f s t a a -> LensLike f s t [a] [a]
#

partsOf turns a Traversal into a Lens that resembles an early version of the uniplate (or biplate) type.

Note: You should really try to maintain the invariant of the number of children in the list.

Example1 expression
(a,b,c) & partsOf each .~ [x,y,z](x,y,z)

Any extras will be lost. If you do not supply enough, then the remainder will come from the original structure.

Example1 expression
(a,b,c) & partsOf each .~ [w,x,y,z](w,x,y)
Example1 expression
(a,b,c) & partsOf each .~ [x,y](x,y,c)
Example1 expression
('b', 'a', 'd', 'c') & partsOf each %~ sort('a','b','c','d')

So technically, this is only a Lens if you do not change the number of results it returns.

When applied to a Fold the result is merely a Getter.

partsOf :: Iso' s a       -> Lens' s [a]
partsOf :: Lens' s a      -> Lens' s [a]
partsOf :: Traversal' s a -> Lens' s [a]
partsOf :: Fold s a       -> Getter s [a]
partsOf :: Getter s a     -> Getter s [a]
valueunsafePartsOf
  1. :: Functor f
  2. => Traversing (->) f s t a b
  3. -> LensLike f s t [a] [b]
#

unsafePartsOf turns a Traversal into a uniplate (or biplate) family.

If you do not need the types of s and t to be different, it is recommended that you use partsOf.

It is generally safer to traverse with the Bazaar rather than use this combinator. However, it is sometimes convenient.

This is unsafe because if you don't supply at least as many b's as you were given a's, then the reconstruction of t will result in an error!

When applied to a Fold the result is merely a Getter (and becomes safe).

unsafePartsOf :: Iso s t a b       -> Lens s t [a] [b]
unsafePartsOf :: Lens s t a b      -> Lens s t [a] [b]
unsafePartsOf :: Traversal s t a b -> Lens s t [a] [b]
unsafePartsOf :: Fold s a          -> Getter s [a]
unsafePartsOf :: Getter s a        -> Getter s [a]
valueholesOf
  1. :: Conjoined p
  2. => Over p (Bazaar p a a) s t a a
  3. -> s
  4. -> [Pretext p a a t]
#

The one-level version of contextsOf. This extracts a list of the immediate children according to a given Traversal as editable contexts.

Given a context you can use pos to see the values, peek at what the structure would be like with an edited result, or simply extract the original structure.

propChildren l x = toListOf l x == map pos (holesOf l x)
propId l x = all (== x) [extract w | w <- holesOf l x]
holesOf :: Iso' s a                -> s -> [Pretext' (->) a s]
holesOf :: Lens' s a               -> s -> [Pretext' (->) a s]
holesOf :: Traversal' s a          -> s -> [Pretext' (->) a s]
holesOf :: IndexedLens' i s a      -> s -> [Pretext' (Indexed i) a s]
holesOf :: IndexedTraversal' i s a -> s -> [Pretext' (Indexed i) a s]
valueholes1Of
  1. :: Conjoined p
  2. => Over p (Bazaar1 p a a) s t a a
  3. -> s
  4. -> NonEmpty (Pretext p a a t)
#

The non-empty version of holesOf. This extract a non-empty list of immediate children according to a given Traversal1 as editable contexts.

Example2 expressions
let head1 f s = runPretext (NonEmpty.head $ holes1Of traversed1 s) f('a' :| "bc") ^. head1'a'
Example1 expression
('a' :| "bc") & head1 %~ toUpper'A' :| "bc"
holes1Of :: Iso' s a                 -> s -> NonEmpty (Pretext' (->) a s)
holes1Of :: Lens' s a                -> s -> NonEmpty (Pretext' (->) a s)
holes1Of :: Traversal1' s a          -> s -> NonEmpty (Pretext' (->) a s)
holes1Of :: IndexedLens' i s a       -> s -> NonEmpty (Pretext' (Indexed i) a s)
holes1Of :: IndexedTraversal1' i s a -> s -> NonEmpty (Pretext' (Indexed i) a s)
valuesingular
  1. :: (HasCallStack, Conjoined p, Functor f)
  2. => Traversing p f s t a a
  3. -> Over p f s t a a
#

This converts a Traversal that you "know" will target one or more elements to a Lens. It can also be used to transform a non-empty Fold into a Getter.

The resulting Lens or Getter will be partial if the supplied Traversal returns no results.

Example1 expression
[1,2,3] ^. singular _head1
Example1 expression
Left (ErrorCall "singular: empty traversal") <- try (evaluate ([] ^. singular _head)) :: IO (Either ErrorCall ())
Example1 expression
Left 4 ^. singular _Left4
Example1 expression
[1..10] ^. singular (ix 7)8
Example1 expression
[] & singular traverse .~ 0[]
singular :: Traversal s t a a          -> Lens s t a a
singular :: Fold s a                   -> Getter s a
singular :: IndexedTraversal i s t a a -> IndexedLens i s t a a
singular :: IndexedFold i s a          -> IndexedGetter i s a
valueunsafeSingular
  1. :: (HasCallStack, Conjoined p, Functor f)
  2. => Traversing p f s t a b
  3. -> Over p f s t a b
#

This converts a Traversal that you "know" will target only one element to a Lens. It can also be used to transform a Fold into a Getter.

The resulting Lens or Getter will be partial if the Traversal targets nothing or more than one element.

Example1 expression
Left (ErrorCall "unsafeSingular: empty traversal") <- try (evaluate ([] & unsafeSingular traverse .~ 0)) :: IO (Either ErrorCall [Integer])
unsafeSingular :: Traversal s t a b          -> Lens s t a b
unsafeSingular :: Fold s a                   -> Getter s a
unsafeSingular :: IndexedTraversal i s t a b -> IndexedLens i s t a b
unsafeSingular :: IndexedFold i s a          -> IndexedGetter i s a
classclass (Foldable1 t, Traversable t) => Traversable1 (t :: Type -> Type) where
#

Methods

Instances40Traversable1, …
valueboth :: Bitraversable r => Traversal (r a a) (r b b) a b
#

Traverse both parts of a Bitraversable container with matching types.

Usually that type will be a pair. Use each to traverse the elements of arbitrary homogeneous tuples.

Example1 expression
(1,2) & both *~ 10(10,20)
Example1 expression
over both length ("hello","world")(5,5)
Example1 expression
("hello","world")^.both"helloworld"
both :: Traversal (a, a)       (b, b)       a b
both :: Traversal (Either a a) (Either b b) a b
valuebeside
  1. :: (Representable q, Applicative (Rep q), Applicative f, Bitraversable r)
  2. => Optical p q f s t a b
  3. -> Optical p q f s' t' a b
  4. -> Optical p q f (r s s') (r t t') a b
#

Apply a different Traversal or Fold to each side of a Bitraversable container.

beside :: Traversal s t a b                -> Traversal s' t' a b                -> Traversal (r s s') (r t t') a b
beside :: IndexedTraversal i s t a b       -> IndexedTraversal i s' t' a b       -> IndexedTraversal i (r s s') (r t t') a b
beside :: IndexPreservingTraversal s t a b -> IndexPreservingTraversal s' t' a b -> IndexPreservingTraversal (r s s') (r t t') a b
beside :: Traversal s t a b                -> Traversal s' t' a b                -> Traversal (s,s') (t,t') a b
beside :: Lens s t a b                     -> Lens s' t' a b                     -> Traversal (s,s') (t,t') a b
beside :: Fold s a                         -> Fold s' a                          -> Fold (s,s') a
beside :: Getter s a                       -> Getter s' a                        -> Fold (s,s') a
beside :: IndexedTraversal i s t a b       -> IndexedTraversal i s' t' a b       -> IndexedTraversal i (s,s') (t,t') a b
beside :: IndexedLens i s t a b            -> IndexedLens i s' t' a b            -> IndexedTraversal i (s,s') (t,t') a b
beside :: IndexedFold i s a                -> IndexedFold i s' a                 -> IndexedFold i (s,s') a
beside :: IndexedGetter i s a              -> IndexedGetter i s' a               -> IndexedFold i (s,s') a
beside :: IndexPreservingTraversal s t a b -> IndexPreservingTraversal s' t' a b -> IndexPreservingTraversal (s,s') (t,t') a b
beside :: IndexPreservingLens s t a b      -> IndexPreservingLens s' t' a b      -> IndexPreservingTraversal (s,s') (t,t') a b
beside :: IndexPreservingFold s a          -> IndexPreservingFold s' a           -> IndexPreservingFold (s,s') a
beside :: IndexPreservingGetter s a        -> IndexPreservingGetter s' a         -> IndexPreservingFold (s,s') a
Example1 expression
("hello",["world","!!!"])^..beside id traverse["hello","world","!!!"]
valuetaking
  1. :: (Conjoined p, Applicative f)
  2. => Int
  3. -> Traversing p f s t a a
  4. -> Over p f s t a a
#

Visit the first n targets of a Traversal, Fold, Getter or Lens.

Example1 expression
[("hello","world"),("!!!","!!!")]^.. taking 2 (traverse.both)["hello","world"]
Example1 expression
timingOut $ [1..] ^.. taking 3 traverse[1,2,3]
Example1 expression
over (taking 5 traverse) succ "hello world""ifmmp world"
taking :: Int -> Traversal' s a                   -> Traversal' s a
taking :: Int -> Lens' s a                        -> Traversal' s a
taking :: Int -> Iso' s a                         -> Traversal' s a
taking :: Int -> Prism' s a                       -> Traversal' s a
taking :: Int -> Getter s a                       -> Fold s a
taking :: Int -> Fold s a                         -> Fold s a
taking :: Int -> IndexedTraversal' i s a          -> IndexedTraversal' i s a
taking :: Int -> IndexedLens' i s a               -> IndexedTraversal' i s a
taking :: Int -> IndexedGetter i s a              -> IndexedFold i s a
taking :: Int -> IndexedFold i s a                -> IndexedFold i s a
valuedropping
  1. :: (Conjoined p, Applicative f)
  2. => Int
  3. -> Over p (Indexing f) s t a a
  4. -> Over p f s t a a
#

Visit all but the first n targets of a Traversal, Fold, Getter or Lens.

Example1 expression
("hello","world") ^? dropping 1 bothJust "world"

Dropping works on infinite traversals as well:

Example1 expression
[1..] ^? dropping 1 foldedJust 2
dropping :: Int -> Traversal' s a                   -> Traversal' s a
dropping :: Int -> Lens' s a                        -> Traversal' s a
dropping :: Int -> Iso' s a                         -> Traversal' s a
dropping :: Int -> Prism' s a                       -> Traversal' s a
dropping :: Int -> Getter s a                       -> Fold s a
dropping :: Int -> Fold s a                         -> Fold s a
dropping :: Int -> IndexedTraversal' i s a          -> IndexedTraversal' i s a
dropping :: Int -> IndexedLens' i s a               -> IndexedTraversal' i s a
dropping :: Int -> IndexedGetter i s a              -> IndexedFold i s a
dropping :: Int -> IndexedFold i s a                -> IndexedFold i s a
valuefailing
  1. :: (Conjoined p, Applicative f)
  2. => Traversing p f s t a b
  3. -> Over p f s t a b
  4. -> Over p f s t a b
#

Try the first Traversal (or Fold), falling back on the second Traversal (or Fold) if it returns no entries.

This is only a valid Traversal if the second Traversal is disjoint from the result of the first or returns exactly the same results. These conditions are trivially met when given a Lens, Iso, Getter, Prism or "affine" Traversal -- one that has 0 or 1 target.

Mutatis mutandis for Fold.

Example1 expression
[0,1,2,3] ^? failing (ix 1) (ix 2)Just 1
Example1 expression
[0,1,2,3] ^? failing (ix 42) (ix 2)Just 2
failing :: Traversal s t a b -> Traversal s t a b -> Traversal s t a b
failing :: Prism s t a b     -> Prism s t a b     -> Traversal s t a b
failing :: Fold s a          -> Fold s a          -> Fold s a

These cases are also supported, trivially, but are boring, because the left hand side always succeeds.

failing :: Lens s t a b      -> Traversal s t a b -> Traversal s t a b
failing :: Iso s t a b       -> Traversal s t a b -> Traversal s t a b
failing :: Equality s t a b  -> Traversal s t a b -> Traversal s t a b
failing :: Getter s a        -> Fold s a          -> Fold s a

If both of the inputs are indexed, the result is also indexed, so you can apply this to a pair of indexed traversals or indexed folds, obtaining an indexed traversal or indexed fold.

failing :: IndexedTraversal i s t a b -> IndexedTraversal i s t a b -> IndexedTraversal i s t a b
failing :: IndexedFold i s a          -> IndexedFold i s a          -> IndexedFold i s a

These cases are also supported, trivially, but are boring, because the left hand side always succeeds.

failing :: IndexedLens i s t a b      -> IndexedTraversal i s t a b -> IndexedTraversal i s t a b
failing :: IndexedGetter i s a        -> IndexedGetter i s a        -> IndexedFold i s a
valuedeepOf
  1. :: (Conjoined p, Applicative f)
  2. => LensLike f s t s t
  3. -> Traversing p f s t a b
  4. -> Over p f s t a b
#

Try the second traversal. If it returns no entries, try again with all entries from the first traversal, recursively.

deepOf :: Fold s s          -> Fold s a                   -> Fold s a
deepOf :: Traversal' s s    -> Traversal' s a             -> Traversal' s a
deepOf :: Traversal s t s t -> Traversal s t a b          -> Traversal s t a b
deepOf :: Fold s s          -> IndexedFold i s a          -> IndexedFold i s a
deepOf :: Traversal s t s t -> IndexedTraversal i s t a b -> IndexedTraversal i s t a b
valueelementOf
  1. :: Applicative f
  2. => LensLike (Indexing f) s t a a
  3. -> Int
  4. -> IndexedLensLike Int f s t a a
#

Traverse the nth elementOf a Traversal, Lens or Iso if it exists.

Example1 expression
[[1],[3,4]] & elementOf (traverse.traverse) 1 .~ 5[[1],[5,4]]
Example1 expression
[[1],[3,4]] ^? elementOf (folded.folded) 1Just 3
Example1 expression
timingOut $ ['a'..] ^?! elementOf folded 5'f'
Example1 expression
timingOut $ take 10 $ elementOf traverse 3 .~ 16 $ [0..][0,1,2,16,4,5,6,7,8,9]
elementOf :: Traversal' s a -> Int -> IndexedTraversal' Int s a
elementOf :: Fold s a       -> Int -> IndexedFold Int s a
valueitraverseOf
  1. :: Indexed i a (f b) -> s -> f t
  2. -> i -> a -> f b
  3. -> s
  4. -> f t
#

Traversal with an index.

NB: When you don't need access to the index then you can just apply your IndexedTraversal directly as a function!

itraverseOf ≡ Control.Lens.Indexed.withIndex
traverseOf l = itraverseOf l . const = id
itraverseOf :: Functor f     => IndexedLens i s t a b       -> (i -> a -> f b) -> s -> f t
itraverseOf :: Applicative f => IndexedTraversal i s t a b  -> (i -> a -> f b) -> s -> f t
itraverseOf :: Apply f       => IndexedTraversal1 i s t a b -> (i -> a -> f b) -> s -> f t
valueimapMOf
  1. :: Over (Indexed i) (WrappedMonad m) s t a b
  2. -> i -> a -> m b
  3. -> s
  4. -> m t
#

Map each element of a structure targeted by a Lens to a monadic action, evaluate these actions from left to right, and collect the results, with access its position.

When you don't need access to the index mapMOf is more liberal in what it can accept.

mapMOf l ≡ imapMOf l . const
imapMOf :: Monad m => IndexedLens       i s t a b -> (i -> a -> m b) -> s -> m t
imapMOf :: Monad m => IndexedTraversal  i s t a b -> (i -> a -> m b) -> s -> m t
imapMOf :: Bind  m => IndexedTraversal1 i s t a b -> (i -> a -> m b) -> s -> m t
valueiforMOf
  1. :: Indexed i a (WrappedMonad m b) -> s -> WrappedMonad m t
  2. -> s
  3. -> i -> a -> m b
  4. -> m t
#

Map each element of a structure targeted by a Lens to a monadic action, evaluate these actions from left to right, and collect the results, with access its position (and the arguments flipped).

forMOf l a ≡ iforMOf l a . const
iforMOf ≡ flip . imapMOf
iforMOf :: Monad m => IndexedLens i s t a b      -> s -> (i -> a -> m b) -> m t
iforMOf :: Monad m => IndexedTraversal i s t a b -> s -> (i -> a -> m b) -> m t
valueimapAccumROf
  1. :: Over (Indexed i) (Backwards (State acc)) s t a b
  2. -> i -> acc -> a -> (acc, b)
  3. -> acc
  4. -> s
  5. -> (acc, t)
#

Generalizes Data.Traversable.mapAccumR to an arbitrary IndexedTraversal with access to the index.

imapAccumROf accumulates state from right to left.

mapAccumROf l ≡ imapAccumROf l . const
imapAccumROf :: IndexedLens i s t a b      -> (i -> acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
imapAccumROf :: IndexedTraversal i s t a b -> (i -> acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
valueconfusing
  1. :: Applicative f
  2. => LensLike (Curried (Yoneda f) (Yoneda f)) s t a b
  3. -> LensLike f s t a b
#

Fuse a Traversal by reassociating all of the (<*>) operations to the left and fusing all of the fmap calls into one. This is particularly useful when constructing a Traversal using operations from GHC.Generics.

Given a pair of Traversals foo and bar,

confusing (foo.bar) = foo.bar

However, foo and bar are each going to use the Applicative they are given.

confusing exploits the Yoneda lemma to merge their separate uses of fmap into a single fmap. and it further exploits an interesting property of the right Kan lift (or Curried) to left associate all of the uses of (<*>) to make it possible to fuse together more fmaps.

This is particularly effective when the choice of functor f is unknown at compile time or when the Traversal foo.bar in the above description is recursive or complex enough to prevent inlining.

fusing is a version of this combinator suitable for fusing lenses.

confusing :: Traversal s t a b -> Traversal s t a b
classclass Field1 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to 1st field of a tuple.

Methods

  • _1 :: Lens s t a b

    Access the 1st field of a tuple (and possibly change its type).

    Example1 expression
    (1,2)^._11
    Example1 expression
    _1 .~ "hello" $ (1,2)("hello",2)
    Example1 expression
    (1,2) & _1 .~ "hello"("hello",2)
    Example1 expression
    _1 putStrLn ("hello","world")hello((),"world")

    This can also be used on larger tuples as well:

    Example1 expression
    (1,2,3,4,5) & _1 +~ 41(42,2,3,4,5)
    _1 :: Lens (a,b) (a',b) a a'
    _1 :: Lens (a,b,c) (a',b,c) a a'
    _1 :: Lens (a,b,c,d) (a',b,c,d) a a'
    ...
    _1 :: Lens (a,b,c,d,e,f,g,h,i) (a',b,c,d,e,f,g,h,i) a a'
    
Instances22Field1, …
  • Field1 (Identity a) (Identity b) a bDefined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (Pair a b) (Pair a' b) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b) (a', b) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
    _1 k ~(a,b) = (\a' -> (a',b)) Data.Functor.<$> k a
    
  • Field1 (a, b, c) (a', b, c) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d) (a', b, c, d) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (Product f g a) (Product f' g a) (f a) (f' a)Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 ((:*:) f g p) ((:*:) f' g p) (f p) (f' p)Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d, e) (a', b, c, d, e) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d, e, f) (a', b, c, d, e, f) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d, e, f, g) (a', b, c, d, e, f, g) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d, e, f, g, h) (a', b, c, d, e, f, g, h) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d, e, f, g, h, i) (a', b, c, d, e, f, g, h, i) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d, e, f, g, h, i, j) (a', b, c, d, e, f, g, h, i, j) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d, e, f, g, h, i, j, kk) (a', b, c, d, e, f, g, h, i, j, kk) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d, e, f, g, h, i, j, kk, l) (a', b, c, d, e, f, g, h, i, j, kk, l) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d, e, f, g, h, i, j, kk, l, m) (a', b, c, d, e, f, g, h, i, j, kk, l, m) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n) (a', b, c, d, e, f, g, h, i, j, kk, l, m, n) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o) (a', b, c, d, e, f, g, h, i, j, kk, l, m, n, o) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a', b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a', b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a', b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field1 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a', b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) a a'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field2 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to the 2nd field of a tuple.

Methods

Instances21Field2, …
  • Field2 (Pair a b) (Pair a b') b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b) (a, b') b b'Defined in lens-5.3.5 · Control.Lens.Tuple
    _2 k ~(a,b) = (\b' -> (a,b')) Data.Functor.<$> k b
    
  • Field2 (a, b, c) (a, b', c) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d) (a, b', c, d) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (Product f g a) (Product f g' a) (g a) (g' a)Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 ((:*:) f g p) ((:*:) f g' p) (g p) (g' p)Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d, e) (a, b', c, d, e) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d, e, f) (a, b', c, d, e, f) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d, e, f, g) (a, b', c, d, e, f, g) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d, e, f, g, h) (a, b', c, d, e, f, g, h) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d, e, f, g, h, i) (a, b', c, d, e, f, g, h, i) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d, e, f, g, h, i, j) (a, b', c, d, e, f, g, h, i, j) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d, e, f, g, h, i, j, kk) (a, b', c, d, e, f, g, h, i, j, kk) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d, e, f, g, h, i, j, kk, l) (a, b', c, d, e, f, g, h, i, j, kk, l) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d, e, f, g, h, i, j, kk, l, m) (a, b', c, d, e, f, g, h, i, j, kk, l, m) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n) (a, b', c, d, e, f, g, h, i, j, kk, l, m, n) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o) (a, b', c, d, e, f, g, h, i, j, kk, l, m, n, o) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a, b', c, d, e, f, g, h, i, j, kk, l, m, n, o, p) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b', c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b', c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field2 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b', c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) b b'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field3 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to the 3rd field of a tuple.

Methods

  • _3 :: Lens s t a b

    Access the 3rd field of a tuple.

Instances17Field3, …
  • Field3 (a, b, c) (a, b, c') c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d) (a, b, c', d) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d, e) (a, b, c', d, e) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d, e, f) (a, b, c', d, e, f) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d, e, f, g) (a, b, c', d, e, f, g) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d, e, f, g, h) (a, b, c', d, e, f, g, h) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d, e, f, g, h, i) (a, b, c', d, e, f, g, h, i) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d, e, f, g, h, i, j) (a, b, c', d, e, f, g, h, i, j) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d, e, f, g, h, i, j, kk) (a, b, c', d, e, f, g, h, i, j, kk) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d, e, f, g, h, i, j, kk, l) (a, b, c', d, e, f, g, h, i, j, kk, l) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d, e, f, g, h, i, j, kk, l, m) (a, b, c', d, e, f, g, h, i, j, kk, l, m) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n) (a, b, c', d, e, f, g, h, i, j, kk, l, m, n) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o) (a, b, c', d, e, f, g, h, i, j, kk, l, m, n, o) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a, b, c', d, e, f, g, h, i, j, kk, l, m, n, o, p) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b, c', d, e, f, g, h, i, j, kk, l, m, n, o, p, q) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c', d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field3 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c', d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) c c'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field4 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provide access to the 4th field of a tuple.

Methods

  • _4 :: Lens s t a b

    Access the 4th field of a tuple.

Instances16Field4, …
  • Field4 (a, b, c, d) (a, b, c, d') d d'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field4 (a, b, c, d, e) (a, b, c, d', e) d d'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field4 (a, b, c, d, e, f) (a, b, c, d', e, f) d d'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field4 (a, b, c, d, e, f, g) (a, b, c, d', e, f, g) d d'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field4 (a, b, c, d, e, f, g, h) (a, b, c, d', e, f, g, h) d d'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field4 (a, b, c, d, e, f, g, h, i) (a, b, c, d', e, f, g, h, i) d d'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field4 (a, b, c, d, e, f, g, h, i, j) (a, b, c, d', e, f, g, h, i, j) d d'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field4 (a, b, c, d, e, f, g, h, i, j, kk) (a, b, c, d', e, f, g, h, i, j, kk) d d'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field4 (a, b, c, d, e, f, g, h, i, j, kk, l) (a, b, c, d', e, f, g, h, i, j, kk, l) d d'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field4 (a, b, c, d, e, f, g, h, i, j, kk, l, m) (a, b, c, d', e, f, g, h, i, j, kk, l, m) d d'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field4 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n) (a, b, c, d', e, f, g, h, i, j, kk, l, m, n) d d'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field4 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o) (a, b, c, d', e, f, g, h, i, j, kk, l, m, n, o) d d'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field4 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a, b, c, d', e, f, g, h, i, j, kk, l, m, n, o, p) d d'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field4 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b, c, d', e, f, g, h, i, j, kk, l, m, n, o, p, q) d d'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field4 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c, d', e, f, g, h, i, j, kk, l, m, n, o, p, q, r) d d'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field4 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d', e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) d d'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field5 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to the 5th field of a tuple.

Methods

  • _5 :: Lens s t a b

    Access the 5th field of a tuple.

Instances15Field5, …
  • Field5 (a, b, c, d, e) (a, b, c, d, e') e e'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field5 (a, b, c, d, e, f) (a, b, c, d, e', f) e e'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field5 (a, b, c, d, e, f, g) (a, b, c, d, e', f, g) e e'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field5 (a, b, c, d, e, f, g, h) (a, b, c, d, e', f, g, h) e e'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field5 (a, b, c, d, e, f, g, h, i) (a, b, c, d, e', f, g, h, i) e e'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field5 (a, b, c, d, e, f, g, h, i, j) (a, b, c, d, e', f, g, h, i, j) e e'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field5 (a, b, c, d, e, f, g, h, i, j, kk) (a, b, c, d, e', f, g, h, i, j, kk) e e'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field5 (a, b, c, d, e, f, g, h, i, j, kk, l) (a, b, c, d, e', f, g, h, i, j, kk, l) e e'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field5 (a, b, c, d, e, f, g, h, i, j, kk, l, m) (a, b, c, d, e', f, g, h, i, j, kk, l, m) e e'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field5 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n) (a, b, c, d, e', f, g, h, i, j, kk, l, m, n) e e'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field5 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o) (a, b, c, d, e', f, g, h, i, j, kk, l, m, n, o) e e'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field5 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a, b, c, d, e', f, g, h, i, j, kk, l, m, n, o, p) e e'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field5 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b, c, d, e', f, g, h, i, j, kk, l, m, n, o, p, q) e e'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field5 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c, d, e', f, g, h, i, j, kk, l, m, n, o, p, q, r) e e'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field5 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d, e', f, g, h, i, j, kk, l, m, n, o, p, q, r, s) e e'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field6 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to the 6th element of a tuple.

Methods

  • _6 :: Lens s t a b

    Access the 6th field of a tuple.

Instances14Field6, …
  • Field6 (a, b, c, d, e, f) (a, b, c, d, e, f') f f'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field6 (a, b, c, d, e, f, g) (a, b, c, d, e, f', g) f f'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field6 (a, b, c, d, e, f, g, h) (a, b, c, d, e, f', g, h) f f'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field6 (a, b, c, d, e, f, g, h, i) (a, b, c, d, e, f', g, h, i) f f'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field6 (a, b, c, d, e, f, g, h, i, j) (a, b, c, d, e, f', g, h, i, j) f f'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field6 (a, b, c, d, e, f, g, h, i, j, kk) (a, b, c, d, e, f', g, h, i, j, kk) f f'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field6 (a, b, c, d, e, f, g, h, i, j, kk, l) (a, b, c, d, e, f', g, h, i, j, kk, l) f f'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field6 (a, b, c, d, e, f, g, h, i, j, kk, l, m) (a, b, c, d, e, f', g, h, i, j, kk, l, m) f f'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field6 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n) (a, b, c, d, e, f', g, h, i, j, kk, l, m, n) f f'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field6 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o) (a, b, c, d, e, f', g, h, i, j, kk, l, m, n, o) f f'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field6 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a, b, c, d, e, f', g, h, i, j, kk, l, m, n, o, p) f f'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field6 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b, c, d, e, f', g, h, i, j, kk, l, m, n, o, p, q) f f'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field6 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c, d, e, f', g, h, i, j, kk, l, m, n, o, p, q, r) f f'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field6 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d, e, f', g, h, i, j, kk, l, m, n, o, p, q, r, s) f f'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field7 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provide access to the 7th field of a tuple.

Methods

  • _7 :: Lens s t a b

    Access the 7th field of a tuple.

Instances13Field7, …
  • Field7 (a, b, c, d, e, f, g) (a, b, c, d, e, f, g') g g'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field7 (a, b, c, d, e, f, g, h) (a, b, c, d, e, f, g', h) g g'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field7 (a, b, c, d, e, f, g, h, i) (a, b, c, d, e, f, g', h, i) g g'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field7 (a, b, c, d, e, f, g, h, i, j) (a, b, c, d, e, f, g', h, i, j) g g'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field7 (a, b, c, d, e, f, g, h, i, j, kk) (a, b, c, d, e, f, g', h, i, j, kk) g g'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field7 (a, b, c, d, e, f, g, h, i, j, kk, l) (a, b, c, d, e, f, g', h, i, j, kk, l) g g'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field7 (a, b, c, d, e, f, g, h, i, j, kk, l, m) (a, b, c, d, e, f, g', h, i, j, kk, l, m) g g'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field7 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n) (a, b, c, d, e, f, g', h, i, j, kk, l, m, n) g g'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field7 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o) (a, b, c, d, e, f, g', h, i, j, kk, l, m, n, o) g g'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field7 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a, b, c, d, e, f, g', h, i, j, kk, l, m, n, o, p) g g'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field7 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b, c, d, e, f, g', h, i, j, kk, l, m, n, o, p, q) g g'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field7 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c, d, e, f, g', h, i, j, kk, l, m, n, o, p, q, r) g g'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field7 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d, e, f, g', h, i, j, kk, l, m, n, o, p, q, r, s) g g'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field8 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provide access to the 8th field of a tuple.

Methods

  • _8 :: Lens s t a b

    Access the 8th field of a tuple.

Instances12Field8, …
  • Field8 (a, b, c, d, e, f, g, h) (a, b, c, d, e, f, g, h') h h'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field8 (a, b, c, d, e, f, g, h, i) (a, b, c, d, e, f, g, h', i) h h'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field8 (a, b, c, d, e, f, g, h, i, j) (a, b, c, d, e, f, g, h', i, j) h h'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field8 (a, b, c, d, e, f, g, h, i, j, kk) (a, b, c, d, e, f, g, h', i, j, kk) h h'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field8 (a, b, c, d, e, f, g, h, i, j, kk, l) (a, b, c, d, e, f, g, h', i, j, kk, l) h h'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field8 (a, b, c, d, e, f, g, h, i, j, kk, l, m) (a, b, c, d, e, f, g, h', i, j, kk, l, m) h h'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field8 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n) (a, b, c, d, e, f, g, h', i, j, kk, l, m, n) h h'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field8 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o) (a, b, c, d, e, f, g, h', i, j, kk, l, m, n, o) h h'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field8 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a, b, c, d, e, f, g, h', i, j, kk, l, m, n, o, p) h h'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field8 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b, c, d, e, f, g, h', i, j, kk, l, m, n, o, p, q) h h'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field8 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c, d, e, f, g, h', i, j, kk, l, m, n, o, p, q, r) h h'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field8 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d, e, f, g, h', i, j, kk, l, m, n, o, p, q, r, s) h h'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field9 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to the 9th field of a tuple.

Methods

  • _9 :: Lens s t a b

    Access the 9th field of a tuple.

Instances11Field9, …
  • Field9 (a, b, c, d, e, f, g, h, i) (a, b, c, d, e, f, g, h, i') i i'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field9 (a, b, c, d, e, f, g, h, i, j) (a, b, c, d, e, f, g, h, i', j) i i'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field9 (a, b, c, d, e, f, g, h, i, j, kk) (a, b, c, d, e, f, g, h, i', j, kk) i i'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field9 (a, b, c, d, e, f, g, h, i, j, kk, l) (a, b, c, d, e, f, g, h, i', j, kk, l) i i'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field9 (a, b, c, d, e, f, g, h, i, j, kk, l, m) (a, b, c, d, e, f, g, h, i', j, kk, l, m) i i'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field9 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n) (a, b, c, d, e, f, g, h, i', j, kk, l, m, n) i i'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field9 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o) (a, b, c, d, e, f, g, h, i', j, kk, l, m, n, o) i i'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field9 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a, b, c, d, e, f, g, h, i', j, kk, l, m, n, o, p) i i'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field9 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b, c, d, e, f, g, h, i', j, kk, l, m, n, o, p, q) i i'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field9 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c, d, e, f, g, h, i', j, kk, l, m, n, o, p, q, r) i i'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field9 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d, e, f, g, h, i', j, kk, l, m, n, o, p, q, r, s) i i'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field10 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to the 10th field of a tuple.

Methods

  • _10 :: Lens s t a b

    Access the 10th field of a tuple.

Instances10Field10, …
  • Field10 (a, b, c, d, e, f, g, h, i, j) (a, b, c, d, e, f, g, h, i, j') j j'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field10 (a, b, c, d, e, f, g, h, i, j, kk) (a, b, c, d, e, f, g, h, i, j', kk) j j'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field10 (a, b, c, d, e, f, g, h, i, j, kk, l) (a, b, c, d, e, f, g, h, i, j', kk, l) j j'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field10 (a, b, c, d, e, f, g, h, i, j, kk, l, m) (a, b, c, d, e, f, g, h, i, j', kk, l, m) j j'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field10 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n) (a, b, c, d, e, f, g, h, i, j', kk, l, m, n) j j'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field10 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o) (a, b, c, d, e, f, g, h, i, j', kk, l, m, n, o) j j'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field10 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a, b, c, d, e, f, g, h, i, j', kk, l, m, n, o, p) j j'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field10 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b, c, d, e, f, g, h, i, j', kk, l, m, n, o, p, q) j j'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field10 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c, d, e, f, g, h, i, j', kk, l, m, n, o, p, q, r) j j'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field10 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d, e, f, g, h, i, j', kk, l, m, n, o, p, q, r, s) j j'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field11 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to the 11th field of a tuple.

Methods

  • _11 :: Lens s t a b

    Access the 11th field of a tuple.

Instances9Field11, …
  • Field11 (a, b, c, d, e, f, g, h, i, j, kk) (a, b, c, d, e, f, g, h, i, j, kk') kk kk'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field11 (a, b, c, d, e, f, g, h, i, j, kk, l) (a, b, c, d, e, f, g, h, i, j, kk', l) kk kk'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field11 (a, b, c, d, e, f, g, h, i, j, kk, l, m) (a, b, c, d, e, f, g, h, i, j, kk', l, m) kk kk'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field11 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n) (a, b, c, d, e, f, g, h, i, j, kk', l, m, n) kk kk'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field11 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o) (a, b, c, d, e, f, g, h, i, j, kk', l, m, n, o) kk kk'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field11 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a, b, c, d, e, f, g, h, i, j, kk', l, m, n, o, p) kk kk'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field11 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b, c, d, e, f, g, h, i, j, kk', l, m, n, o, p, q) kk kk'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field11 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c, d, e, f, g, h, i, j, kk', l, m, n, o, p, q, r) kk kk'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field11 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d, e, f, g, h, i, j, kk', l, m, n, o, p, q, r, s) kk kk'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field12 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to the 12th field of a tuple.

Methods

  • _12 :: Lens s t a b

    Access the 12th field of a tuple.

Instances8Field12, …
  • Field12 (a, b, c, d, e, f, g, h, i, j, kk, l) (a, b, c, d, e, f, g, h, i, j, kk, l') l l'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field12 (a, b, c, d, e, f, g, h, i, j, kk, l, m) (a, b, c, d, e, f, g, h, i, j, kk, l', m) l l'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field12 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n) (a, b, c, d, e, f, g, h, i, j, kk, l', m, n) l l'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field12 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o) (a, b, c, d, e, f, g, h, i, j, kk, l', m, n, o) l l'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field12 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a, b, c, d, e, f, g, h, i, j, kk, l', m, n, o, p) l l'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field12 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b, c, d, e, f, g, h, i, j, kk, l', m, n, o, p, q) l l'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field12 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c, d, e, f, g, h, i, j, kk, l', m, n, o, p, q, r) l l'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field12 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d, e, f, g, h, i, j, kk, l', m, n, o, p, q, r, s) l l'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field13 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to the 13th field of a tuple.

Methods

  • _13 :: Lens s t a b

    Access the 13th field of a tuple.

Instances7Field13, …
  • Field13 (a, b, c, d, e, f, g, h, i, j, kk, l, m) (a, b, c, d, e, f, g, h, i, j, kk, l, m') m m'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field13 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n) (a, b, c, d, e, f, g, h, i, j, kk, l, m', n) m m'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field13 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o) (a, b, c, d, e, f, g, h, i, j, kk, l, m', n, o) m m'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field13 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a, b, c, d, e, f, g, h, i, j, kk, l, m', n, o, p) m m'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field13 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b, c, d, e, f, g, h, i, j, kk, l, m', n, o, p, q) m m'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field13 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c, d, e, f, g, h, i, j, kk, l, m', n, o, p, q, r) m m'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field13 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d, e, f, g, h, i, j, kk, l, m', n, o, p, q, r, s) m m'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field14 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to the 14th field of a tuple.

Methods

  • _14 :: Lens s t a b

    Access the 14th field of a tuple.

Instances6Field14
  • Field14 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n') n n'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field14 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n', o) n n'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field14 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n', o, p) n n'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field14 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n', o, p, q) n n'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field14 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n', o, p, q, r) n n'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field14 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n', o, p, q, r, s) n n'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field15 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to the 15th field of a tuple.

Methods

  • _15 :: Lens s t a b

    Access the 15th field of a tuple.

Instances5Field15
  • Field15 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o') o o'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field15 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o', p) o o'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field15 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o', p, q) o o'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field15 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o', p, q, r) o o'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field15 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o', p, q, r, s) o o'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field16 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to the 16th field of a tuple.

Methods

  • _16 :: Lens s t a b

    Access the 16th field of a tuple.

Instances4Field16
  • Field16 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p') p p'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field16 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p', q) p p'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field16 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p', q, r) p p'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field16 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p', q, r, s) p p'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field17 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to the 17th field of a tuple.

Methods

  • _17 :: Lens s t a b

    Access the 17th field of a tuple.

Instances3Field17
  • Field17 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q') q q'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field17 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q', r) q q'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field17 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q', r, s) q q'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field18 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to the 18th field of a tuple.

Methods

  • _18 :: Lens s t a b

    Access the 18th field of a tuple.

Instances2Field18
  • Field18 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r') r r'Defined in lens-5.3.5 · Control.Lens.Tuple
  • Field18 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r', s) r r'Defined in lens-5.3.5 · Control.Lens.Tuple
classclass Field19 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Provides access to the 19th field of a tuple.

Methods

  • _19 :: Lens s t a b

    Access the 19th field of a tuple.

Instances1Field19
  • Field19 (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s) (a, b, c, d, e, f, g, h, i, j, kk, l, m, n, o, p, q, r, s') s s'Defined in lens-5.3.5 · Control.Lens.Tuple
typetype As (a :: k2) = Equality' a a
#

Composable asTypeOf. Useful for constraining excess polymorphism, foo . (id :: As Int) . bar.

typetype Simple (f :: k1 -> k1 -> k2 -> k2 -> k) (s :: k1) (a :: k2) = f s s a a
#

A Simple Lens, Simple Traversal, ... can be used instead of a Lens,Traversal, ... whenever the type variables don't change upon setting a value.

_imagPart :: Simple Lens (Complex a) a
traversed :: Simple (IndexedTraversal Int) [a] a

Note: To use this alias in your own code with LensLike f or Setter, you may have to turn on LiberalTypeSynonyms.

This is commonly abbreviated as a "prime" marker, e.g. Lens' = Simple Lens.

typetype Over (p :: k -> Type -> Type) (f :: k1 -> Type) s (t :: k1) (a :: k) (b :: k1) = p a (f b) -> s -> f t
#

This is a convenient alias for use when you need to consume either indexed or non-indexed lens-likes based on context.

typetype Over' (p :: Type -> Type -> Type) (f :: Type -> Type) s a = Over p f s s a a
#

This is a convenient alias for use when you need to consume either indexed or non-indexed lens-likes based on context.

type Over' p f = Simple (Over p f)
typetype IndexedLensLike i (f :: k -> Type) s (t :: k) a (b :: k) = forall (p :: Type -> Type -> Type). Indexable i p => p a (f b) -> s -> f t
#

Convenient alias for constructing indexed lenses and their ilk.

typetype Optical (p :: k -> k1 -> Type) (q :: k2 -> k1 -> Type) (f :: k3 -> k1) (s :: k2) (t :: k3) (a :: k) (b :: k3) = p a (f b) -> q s (f t)
#
type LensLike f s t a b = Optical (->) (->) f s t a b
type Over p f s t a b = Optical p (->) f s t a b
type Optic p f s t a b = Optical p p f s t a b
classclass Wrapped s => Rewrapped s t
#
Instances149Rewrapped, …
valueop :: Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
#

Given the constructor for a Wrapped type, return a deconstructor that is its inverse.

Assuming the Wrapped instance is legal, these laws hold:

op f . f ≡ id
f . op f ≡ id
Example1 expression
op Identity (Identity 4)4
Example1 expression
op Const (Const "hello")"hello"
valueala
  1. :: (Functor f, Rewrapping s t)
  2. => Unwrapped s -> s
  3. -> (Unwrapped t -> t) -> f s
  4. -> f (Unwrapped s)
#

This combinator is based on ala from Conor McBride's work on Epigram.

As with _Wrapping, the user supplied function for the newtype is ignored.

Example1 expression
ala Sum foldMap [1,2,3,4]10
Example1 expression
ala All foldMap [True,True]True
Example1 expression
ala All foldMap [True,False]False
Example1 expression
ala Any foldMap [False,False]False
Example1 expression
ala Any foldMap [True,False]True
Example1 expression
ala Product foldMap [1,2,3,4]24

You may want to think of this combinator as having the following, simpler, type.

ala :: Rewrapping s t => (Unwrapped s -> s) -> ((Unwrapped t -> t) -> e -> s) -> e -> Unwrapped s
valuealaf
  1. :: (Functor f, Functor g, Rewrapping s t)
  2. => Unwrapped s -> s
  3. -> f t -> g s
  4. -> f (Unwrapped t)
  5. -> g (Unwrapped s)
#

This combinator is based on ala' from Conor McBride's work on Epigram.

As with _Wrapping, the user supplied function for the newtype is ignored.

alaf :: Rewrapping s t => (Unwrapped s -> s) -> ((r -> t) -> e -> s) -> (r -> Unwrapped t) -> e -> Unwrapped s
Example1 expression
alaf Sum foldMap Prelude.length ["hello","world"]10
familytype family Unwrapped s
#
Instances149Unwrapped, …
familytype family Magnified (m :: Type -> Type) :: Type -> Type -> Type
#

This type family is used by Magnify to describe the common effect type.

Instances6Magnified
classclass (Magnified m ~ Magnified n, MonadReader b m, MonadReader a n) => Magnify (m :: Type -> Type) (n :: Type -> Type) b a | m -> b, n -> a, m a -> n, n b -> m where
#

This class allows us to use magnify part of the environment, changing the environment supplied by many different Monad transformers. Unlike zoom this can change the environment of a deeply nested Monad transformer.

Also, unlike zoom, this can be used with any valid Getter, but cannot be used with a Traversal or Fold.

Methods

  • magnify :: ((Functor (Magnified m c), Contravariant (Magnified m c)) => LensLike' (Magnified m c) a b) -> m c -> n cinfixr 2

    Run a monadic action in a larger environment than it was defined in, using a Getter.

    This acts like local, but can in many cases change the type of the environment as well.

    This is commonly used to lift actions in a simpler Reader Monad into a Monad with a larger environment type.

    This can be used to edit pretty much any Monad transformer stack with an environment in it:

    Example1 expression
    (1,2) & magnify _2 (+1)3
    Example1 expression
    flip Reader.runReader (1,2) $ magnify _1 Reader.ask1
    Example1 expression
    flip Reader.runReader (1,2,[10..20]) $ magnify (_3._tail) Reader.ask[11,12,13,14,15,16,17,18,19,20]

    The type can be read as

      magnify :: LensLike' (Magnified m c) a b -> m c -> n c
    

    but the higher-rank constraints make it easier to apply magnify to a Getter in highly-polymorphic code.

    magnify :: Getter s a -> (a -> r) -> s -> r
    magnify :: Monoid r => Fold s a   -> (a -> r) -> s -> r
    
    magnify :: Monoid w                 => Getter s t -> RWS t w st c -> RWS s w st c
    magnify :: (Monoid w, Monoid c) => Fold s a   -> RWS a w st c -> RWS s w st c
    ...
    
Instances6Magnify
familytype family Zoomed (m :: Type -> Type) :: Type -> Type -> Type
#

This type family is used by Zoom to describe the common effect type.

Instances11Zoomed, …