>>> (a,b)^._2 b
>>> ("hello","world")^._2 "world"
>>> import Data.Complex >>> ((0, 1 :+ 2), 3)^._1._2.to magnitude 2.23606797749979
(^.) :: s -> Getter s a -> a (^.) :: Monoid m => s -> Fold s m -> m (^.) :: s -> Iso' s a -> a (^.) :: s -> Lens' s a -> a (^.) :: Monoid m => s -> Traversal' s m -> m
(^. _1) :: (a, b) -> a (^. _1) = fstWhen (^.) is used with a traversal, it combines all results using the Monoid instance for the resulting type. For instance, for lists it would be simple concatenation:
>>> ("str","ing") ^. each "string"The reason for this is that traversals use Applicative, and the Applicative instance for Const uses monoid concatenation to combine “effects” of Const. A non-operator version of (^.) is called view, and it's a bit more general than (^.) (it works in MonadReader). If you need the general version, you can get it from microlens-mtl; otherwise there's view available in Lens.Micro.Extras.
(^.) :: s -> Getter s t a b -> aAccess the value referenced by a getter or lens.
(^.) :: Monoid a => s -> Fold s t a b -> aAccess the monoidal summary referenced by a traversal or a fold.
>>> [[1,2],[3]]^..id [[[1,2],[3]]] >>> [[1,2],[3]]^..traverse [[1,2],[3]] >>> [[1,2],[3]]^..traverse.traverse [1,2,3]
>>> (1,2)^..both [1,2]
toList xs ≡ xs ^.. folded (^..) ≡ flip toListOf
(^..) :: s -> Getter s a -> a :: s -> Fold s a -> a :: s -> Lens' s a -> a :: s -> Iso' s a -> a :: s -> Traversal' s a -> a :: s -> Prism' s a -> [a]
>>> Just 3 ^.. _Just [3]Gathering all values in a list of tuples:
>>> [(1,2),(3,4)] ^.. each.each [1,2,3,4]
(^..) :: s -> Fold s t a b -> [a]Returns a list of all of the referenced values in order.
x ^. myLenswould extract a piece of x :: b, specified by myLens :: Lens' b a. The result has type a.
xVar ^^. myLenswould extract a piece out of xVar :: BVar s b (a BVar holding a b), specified by myLens :: Lens' b a. The result has type BVar s a (a BVar holding a a) This is the main way to pull out values from BVar of container types. If you have control of your data type definitions, consider using splitBV, which lets you break out BVars of values into BVars of their individual fields automatically without requiring lenses. NOTE: Usage of ^^. on many fields from the same item is usually the main source of overhead in backprop code, if you are looking to optimize your code. See <https://backprop.jle.im/07-performance.html this performance guide> for more information, and details on mitigating this overhead. WARNING: Do not use with any lenses that operate "numerically" on the contents (like multiplying).