?y      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwx,(C) 2013-2016 Edward Kmett, 2015-2016 Artyom BSD-style (see the file LICENSE)SafeQVh[ A type alias for monomorphic s.^ is a type that is often used to make combinators as general as possible. For instance, take (C), which only requires the passed lens to be able to work with the (,) aT functor (lenses and traversals can do that). The fully expanded type is as follows: (?) :: ((a -> (a, b)) -> s -> (a, t)) -> (a -> b) -> s -> (a, t) With , the intent to use the (,) a# functor can be made a bit clearer: (9) :: LensLike ((,) a) s t a b -> (a -> b) -> s -> (a, t) wThis is a type alias for monomorphic traversals which don't change the type of the container (or of the values inside).Traversal s t a b is a generalisation of U which allows many targets (possibly 0). It's achieved by changing the constraint to y instead of z  indeed, the point of yR is that you can combine effects, which is just what we need to have many targets.,Ultimately, traversals should follow 2 laws: Gt pure "a pure fmap (t f) . t g "a getCompose . t (Compose . fmap f . g) JThe 1st law states that you can't change the shape of the structure or do anything funny with elements (traverse elements which aren't in the structure, create new elements out of thin air, etc.). The 2nd law states that you should be able to fuse 2 identical traversals into one. For a more detailed explanation of the laws, see  /http://artyom.me/lens-over-tea-2#traversal-lawsthis blog post) (if you prefer rambling blog posts), or  @https://www.cs.ox.ac.uk/jeremy.gibbons/publications/iterator.pdf#The Essence Of The Iterator Pattern (if you prefer papers).lTraversing any value twice is a violation of traversal laws. You can, however, traverse values in any order.rThis is a type alias for monomorphic lenses which don't change the type of the container (or of the value inside). Lens s t a bk is the lowest common denominator of a setter and a getter, something that has the power of both; it has a z constraint, and since both { and |F are functors, it can be used whenever a getter or a setter is needed.a- is the type of the value inside of structureb" is the type of the replaced values# is the type of the whole structuret. is the type of the structure after replacing a in it with bA SimpleFold s a extracts several as from s); so, it's pretty much the same thing as  (s -> [a])), but you can use it with lens operators. The actual Fold from lens is more general: Wtype Fold s a = forall f. (Contravariant f, Applicative f) => (a -> f a) -> s -> f s 7There are several functions in lens that accept lens's Fold but won't accept ; I'm aware of  Qhttp://hackage.haskell.org/package/lens/docs/Control-Lens-Fold.html#v:takingWhile takingWhile,  Shttp://hackage.haskell.org/package/lens/docs/Control-Lens-Fold.html#v:droppingWhile droppingWhile,  Ohttp://hackage.haskell.org/package/lens/docs/Control-Lens-Fold.html#v:backwards backwards,  Nhttp://hackage.haskell.org/package/lens/docs/Control-Lens-Fold.html#v:foldByOffoldByOf,  Qhttp://hackage.haskell.org/package/lens/docs/Control-Lens-Fold.html#v:foldMapByOf foldMapByOf%. For this reason, try not to export s if at all possible.  3http://hackage.haskell.org/package/microlens-contramicrolens-contra" provides a fully lens-compatible Fold.Lens users: you can convert a  to Fold by applying folded . toListOf to it. 7Functions that operate on getters and folds  such as (), (), ()  use  Getter r s a (with different values of r<) to describe what kind of result they need. For instance, ([) needs the getter to be able to return a single value, and so it accepts a getter of type  Getting a s a. (9) wants the getter to gather values together, so it uses Getting (Endo [a]) s a (it could've used Getting [a] s a instead, but it's faster with   ). The choice of rE depends on what you want to do with elements you're extracting from s. A SimpleGetter s a extracts a from s; so, it's the same thing as (s -> a)E, but you can use it in lens chains because its type looks like this: Gtype SimpleGetter s a = forall r. (a -> Const r a) -> s -> Const r s Since Const r is a functor,  N has the same shape as other lens types and can be composed with them. To get (s -> a) out of a   , choose r ~ a and feed Const :: a -> Const a a to the getter: /-- the actual signature is more permissive: --   ::   a s a -> s -> a   ::   s a -> s -> a   getter = } . getter {  The actual  Nhttp://hackage.haskell.org/package/lens/docs/Control-Lens-Getter.html#t:GetterGetter from lens is more general: Utype Getter s a = forall f. (Contravariant f, Functor f) => (a -> f a) -> s -> f s :I'm not currently aware of any functions that take lens's Getter but won't accept  (, but you should try to avoid exporting  7s anyway to minimise confusion. Alternatively, look at  3http://hackage.haskell.org/package/microlens-contramicrolens-contra), which provides a fully lens-compatible Getter.Lens users: you can convert a   to Getter by applying  to . view to it. This is a type alias for monomorphic setters which don't change the type of the container (or of the value inside). It's useful more often than the same type in lens, because we can't provide real setters and so it does the job of both  Shttp://hackage.haskell.org/package/lens/docs/Control-Lens-Setter.html#t:ASetter-39-ASetter' and  Rhttp://hackage.haskell.org/package/lens/docs/Control-Lens-Setter.html#t:Setter-39-Setter'. ASetter s t a bR is something that turns a function modifying a value into a function modifying a  structure. If you ignore | (as  Identity a is the same thing as a), the type is: *type ASetter s t a b = (a -> b) -> s -> t  The reason | is used here is for  , to be composable with other types, such as .Technically, if you're writing a library, you shouldn't use this type for setters you are exporting from your library; the right type to use is  Nhttp://hackage.haskell.org/package/lens/docs/Control-Lens-Setter.html#t:SetterSetterN, but it is not provided by this package (because then it'd have to depend on  /http://hackage.haskell.org/package/distributive distributiveG). It's completely alright, however, to export functions which take an   as an argument.  ,(C) 2013-2016 Edward Kmett, 2015-2016 Artyom BSD-style (see the file LICENSE)Unsafe+;<=>?AFQTV A lets you convert between strict and lazy versions of a datatype:#let someText = "hello" :: Lazy.TextsomeText ^. strict"hello" :: Strict.Text_It can also be useful if you have a function that works on a strict type but your type is lazy: DstripDiacritics :: Strict.Text -> Strict.Text stripDiacritics = ... (let someText = "Paul ErdQs" :: Lazy.Text$someText & strict %~ stripDiacritics"Paul Erdos" :: Lazy.Text works on  ByteString and StateT/WriterT/RWST if you use  0http://hackage.haskell.org/package/microlens-ghc microlens-ghc, and additionally on Text if you use  5http://hackage.haskell.org/package/microlens-platformmicrolens-platform. is like ! but works in opposite direction:%let someText = "hello" :: Strict.TextsomeText ^. lazy"hello" :: Lazy.Text:Gives access to the 1st field of a tuple (up to 5-tuples).Getting the 1st component:(1,2,3,4,5) ^. _11Setting the 1st component:(1,2,3) & _1 .~ 10(10,2,3)8Note that this lens is lazy, and can set fields even of ~:!set _1 10 undefined :: (Int, Int)$(10,*** Exception: Prelude.undefinedVThis is done to avoid violating a lens law stating that you can get back what you put:/view _1 . set _1 10 $ (undefined :: (Int, Int))10%The implementation (for 2-tuples) is:  f t = (,)  f ( t)   ( t) or, alternatively,  f ~(a,b) = (\a' -> (a',b))  f a (where ~ means a  +https://wiki.haskell.org/Lazy_pattern_match lazy pattern)., , , and  are also available (see below).6This lens lets you read, write, or delete elements in Map-like structures. It returns ' when the value isn't found, just like lookup: Data.Map.lookup k m = m  at k KHowever, it also lets you insert and delete values by setting the value to  value or : Data.Map.insert k a m = m   at k  ! Just a Data.Map.delete k m = m   at k   Nothing Or you could use () instead of ( ): Data.Map.insert k a m = m   at k  a  Note that B doesn't work for arrays or lists. You can't delete an arbitrary element from an array (what would be left in its place?), and you can't set an arbitrary element in a list because if the index is out of list's bounds, you'd have to somehow fill the stretch between the last element and the element you just inserted (i.e. [1,2,3] & at 10 .~ 5d is undefined). If you want to modify an already existing value in an array or list, you should use ! instead. is often used with . See the documentation of  for examples. Note that  isn't strict for Map, even if you're using Data.Map.Strict:EData.Map.Strict.size (Data.Map.Strict.empty & at 1 .~ Just undefined)1AThe reason for such behavior is that there's actually no strict Map  type; Data.Map.Strict2 just provides some strict functions for ordinary Maps.8This package doesn't actually provide any instances for , but there are instances for Map and IntMap in  0http://hackage.haskell.org/package/microlens-ghc microlens-ghc and an instance for HashMap in  5http://hackage.haskell.org/package/microlens-platformmicrolens-platform.!SThis traversal lets you access (and update) an arbitrary element in a list, array, MapB, etc. (If you want to insert or delete elements as well, look at .)An example for lists:[0..5] & ix 3 .~ 10[0,1,2,10,4,5] You can use it for getting, too:[0..5] ^? ix 3Just 3HOf course, the element may not be present (which means that you can use ! as a safe variant of ()):[0..5] ^? ix 10Nothing~Another useful instance is the one for functions  it lets you modify their outputs for specific inputs. For instance, here's J that returns 0 when the list is empty (instead of throwing an exception):  maximum0 =    ! []   0 5The following instances are provided in this package: ! ::  ->  [a] a ! :: ( e) => e ->  (e -> a) a You can also use ! with types from  (http://hackage.haskell.org/package/arrayarray,  -http://hackage.haskell.org/package/bytestring bytestring, and  -http://hackage.haskell.org/package/containers containers by using  0http://hackage.haskell.org/package/microlens-ghc microlens-ghc", or additionally with types from  )http://hackage.haskell.org/package/vectorvector,  'http://hackage.haskell.org/package/texttext, and  7http://hackage.haskell.org/package/unordered-containersunordered-containers by using  5http://hackage.haskell.org/package/microlens-platformmicrolens-platform.%% tries to be a universal   it behaves like &R in most situations, but also adds support for e.g. tuples with same-typed values:(1,2) & each %~ succ(2,3)["x", "y", "z"] ^. each"xyz"However, note that % doesn't work on every instance of . If you have a  which isn't supported by %, you can use &# instead. Personally, I like using % instead of &7 whenever possible  it's shorter and more descriptive. You can use % with these things: % ::  [a] [b] a b % ::  ( a) ( b) a b % ::  (a,a) (b,b) a b % ::  (a,a,a) (b,b,b) a b % ::  (a,a,a,a) (b,b,b,b) a b % ::  (a,a,a,a,a) (b,b,b,b,b) a b % :: ( a,  b) =>  ( a) ( b) a b You can also use % with types from  (http://hackage.haskell.org/package/arrayarray,  -http://hackage.haskell.org/package/bytestring bytestring, and  -http://hackage.haskell.org/package/containers containers by using  0http://hackage.haskell.org/package/microlens-ghc microlens-ghc", or additionally with types from  )http://hackage.haskell.org/package/vectorvector,  'http://hackage.haskell.org/package/texttext, and  7http://hackage.haskell.org/package/unordered-containersunordered-containers by using  5http://hackage.haskell.org/package/microlens-platformmicrolens-platform.&& traverses any  container (list, vector, Map, , you name it):Just 1 ^.. traversed[1]& is the same as 2, but can be faster thanks to magic rewrite rules.'' is a fold for anything  . In a way, it's an opposite of mapped;  the most powerful getter, but can't be used as a setter.++ creates an  O from an ordinary function. (The only thing it does is wrapping and unwrapping |.)$  !"#$%&'()*+,-./$&'()*+-.,$%#" !/   !$%-9 .8,(C) 2013-2016 Edward Kmett, 2015-2016 Artyom BSD-style (see the file LICENSE) Trustworthy +;<=QVې&J(J) is flipped (): x J f = f  x NIt's often useful when writing lenses. For instance, let's say you're writing ! for Map; if the key is found in the map, you have to apply a function to it and then change the map based on the new value  which requires a lambda, like this: !\ key f map = case Map.lookup key map of Just val -> (\val' -> Map.insert key val' map)  f val Nothing ->  map With (Jr) you can get rid of parentheses and move the long lambda expression to the right of the value (like when you use ): !? key f map = case Map.lookup key map of Just val -> f val J3 \val' -> Map.insert key val' map Nothing ->  map K(K[) applies a function to the target; an alternative explanation is that it is an inverse of +2, which turns a setter into an ordinary function. Q K  is the same thing as  .See L$ if you want a non-operator synonym.#Negating the 1st element of a pair:(1,2) & _1 %~ negate(-1,2) Turning all Lefts in a list to upper case::(mapped._Left.mapped %~ toUpper) [Left "foo", Right "bar"][Left "FOO",Right "bar"]LL is a synonym for (K).Getting  in a roundabout way: L Q :: z f => (a -> b) -> f a -> f b L Q =  1Applying a function to both components of a pair: L f! :: (a -> b) -> (a, a) -> (b, b) L f" = \f t -> (f (fst t), f (snd t)) Using L  as a replacement for :over _2 show (10,20) (10,"20")M(M+) appends a value monoidally to the target.)("hello", "goodbye") & both <>~ " world!""("hello world!", "goodbye world!")N(N?) assigns a value to the target. It's the same thing as using (K) with : l N x = l K  x See O$ if you want a non-operator synonym.0Here it is used to change 2 fields of a 3-tuple:(0,0,0) & _1 .~ 1 & _3 .~ 3(1,0,3)OO is a synonym for (N).$Setting the 1st component of a pair: O  :: x -> (a, b) -> (x, b) O  = \x t -> (x, snd t) Using it to rewrite (): O Q :: z f => a -> f b -> f a O Q = () P(P) is a version of (N) that wraps the value into  before setting. l ?~ b = l .~ Just b %It can be useful in combination with :Map.empty & at 3 ?~ xfromList [(3,x)]QQV is a setter for everything contained in a functor. You can use it to map over lists, Maybe , or even IO' (which is something you can't do with & or %).Here Q$ is used to turn a value to all non- values in a list:,[Just 3,Nothing,Just 5] & mapped.mapped .~ 0[Just 0,Nothing,Just 0]Keep in mind that while Q is a more powerful setter than %R, it can't be used as a getter! This won't work (and will fail with a type error): [(1,2),(3,4),(5,6)] W Q . f RThis is a version of (KG) which modifies the structure and returns it along with the new value:(1, 2) & _1 <%~ negate (-1, (-1, 2))Simpler type signatures: (R) :: * s t a b -> (a -> b) -> s -> (b, t) (R) ::  b => $ s t a b -> (a -> b) -> s -> (b, t) DSince it does getting in addition to setting, you can't use it with  / (but you can use it with lens and traversals).SThis is a version of (KG) which modifies the structure and returns it along with the old value:(1, 2) & _1 <<%~ negate (1, (-1, 2))Simpler type signatures: (S) :: * s t a b -> (a -> b) -> s -> (a, t) (S) ::  a => $ s t a b -> (a -> b) -> s -> (a, t) TThis is a version of (NG) which modifies the structure and returns it along with the old value:(1, 2) & _1 <<.~ 0 (1, (0, 2))Simpler type signatures: (T) :: # s t a b -> b -> s -> (a, t) (T) ::  a =>  s t a b -> b -> s -> (a, t) U(U) applies a getter to a value; in other words, it gets a value out of a structure using a getter (which can be a lens, traversal, fold, etc.).Getting 1st field of a tuple: (U ) :: (a, b) -> a (U ) =  When (U>) is used with a traversal, it combines all results using the [ 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 y , and the y instance for {3 uses monoid concatenation to combine effects  of {.A non-operator version of (U ) is called view$, and it's a bit more general than (U) (it works in  MonadReader8). If you need the general version, you can get it from  0http://hackage.haskell.org/package/microlens-mtl microlens-mtl; otherwise there's   available in Lens.Micro.Extras.VV$ creates a getter from any function: a U V f = f a It's most useful in chains, because it lets you mix lenses and ordinary functions. Suppose you have a record which comes from some third-party library and doesn't have any lens accessors. You want to do something like this: value ^. _1 . field . at 2  However, field1 isn't a getter, and you have to do this instead: field (value ^. _1) ^. at 2 but now valueK is in the middle and it's hard to read the resulting code. A variant with V is prettier and more readable: value ^. _1 . to field . at 2 Ws ^.. t% returns the list of all values that t gets from s.A  contains either 0 or 1 values:Just 3 ^.. _Just[3])Gathering all values in a list of tuples:[(1,2),(3,4)] ^.. each.each [1,2,3,4]XX is a synonym for (W).Ys ^? t returns the 1st element t returns, or  if tD doesn't return anything. It's trivially implemented by passing the  monoid to the getter.Safe : [] ^? eachNothing[1..3] ^? eachJust 1 Converting  to :Left 1 ^? _RightNothingRight 1 ^? _RightJust 1A non-operator version of (Y ) is called preview , and  like view!  it's a bit more general than (Y) (it works in  MonadReader8). If you need the general version, you can get it from  0http://hackage.haskell.org/package/microlens-mtl microlens-mtl; otherwise there's  available in Lens.Micro.Extras.Z(Z) is an unsafe variant of (Y)  instead of using J to indicate that there were no elements returned, it throws an exception.[<Apply an action to all targets and discard the result (like  or ):,traverseOf_ both putStrLn ("hello", "world")helloworld_Works with anything that allows getting, including lenses and getters (so, anything except for  ). Should be faster than a when you don't need the result.\[D with flipped arguments. Useful if the loop body  is a lambda or a doP block, or in some other cases  for instance, you can avoid accidentally using  on a tuple or  by switching to \ %+. Or you can write custom loops like these: \ f (a, b) $ \x -> ... \ % [1..10] $ \x -> ... \ (% . m) $ \x -> ... ]]h checks whether a getter (any getter, including lenses, traversals, and folds) returns at least 1 value.%Checking whether a list is non-empty: has each []FalseYou can also use it with e.g. l4 (and other 0-or-1 traversals) as a replacement for ,  and other isConstructorName functions:has _Left (Left 1)True^^3 creates a fold out of any function that returns a " container (for instance, a list):[1..5] ^.. folding tail [2,3,4,5]__ creates a  from a getter and a setter. The resulting lens isn't the most effective one (because of having to traverse the structure twice when modifying), but it shouldn't matter much.#A (partial) lens for list indexing:  ix :: Int ->  [a] a ix i = _ (o i) -- getter (\s b -> take i s ++ b : drop (i+1) s) -- setter Usage:  >>> [1..9] U ix 3 4 >>> [1..9] & ix 3 K negate [1,2,3,-4,5,6,7,8,9] When getting, the setter is completely unused; when setting, the getter is unused. Both are used only when the value is being modified. For instance, here we define a lens for the 1st element of a list, but instead of a legitimate getter we use ~%. Then we use the resulting lens for setting8 and it works, which proves that the getter wasn't used:3[1,2,3] & lens undefined (\s b -> b : tail s) .~ 10[10,2,3]`` lets you relabel  a  by equating . to an arbitrary value (which you can choose):Just 1 ^. non 01Nothing ^. non 00The most useful thing about `A is that relabeling also works in other direction. If you try to O+ the forbidden  value, it'll be turned to :Just 1 & non 0 .~ 0Nothing&Setting anything else works just fine:Just 1 & non 0 .~ 5Just 5*Same happens if you try to modify a value:Just 1 & non 0 %~ subtract 1NothingJust 1 & non 0 %~ (+ 1)Just 2`$ is often useful when combined with |. For instance, if you have a map of songs and their playcounts, it makes sense not to store songs with 0 plays in the map; `5 can act as a filter that wouldn't pass such entries.5Decrease playcount of a song to 0, and it'll be gone:GfromList [("Soon",1),("Yesterday",3)] & at "Soon" . non 0 %~ subtract 1fromList [("Yesterday",3)]6Try to add a song with 0 plays, and it won't be added:3fromList [("Yesterday",3)] & at "Soon" . non 0 .~ 0fromList [("Yesterday",3)]1But it will be added if you set any other number:3fromList [("Yesterday",3)] & at "Soon" . non 0 .~ 1%fromList [("Soon",1),("Yesterday",3)]`^ is also useful when working with nested maps. Here a nested map is created when it's missing:?Map.empty & at "Dez Mona" . non Map.empty . at "Soon" .~ Just 1-fromList [("Dez Mona",fromList [("Soon",1)])]Cand here it is deleted when its last entry is deleted (notice that ` is used twice here):ofromList [("Dez Mona",fromList [("Soon",1)])] & at "Dez Mona" . non Map.empty . at "Soon" . non 0 %~ subtract 1 fromList []HTo understand the last example better, observe the flow of values in it:the map goes into  at "Dez Mona"the nested map (wrapped into Just ) goes into  non Map.emptyJust+ is unwrapped and the nested map goes into  at "Soon"Just 1 is unwrapped by non 0/Then the final value  i.e. 1  is modified by  subtract 16 and the result (which is 0) starts flowing backwards:non 0 sees the 0 and produces a Nothing at "Soon" sees Nothing1 and deletes the corresponding value from the map%the resulting empty map is passed to  non Map.empty/, which sees that it's empty and thus produces Nothing at "Dez Mona" sees Nothing! and removes the key from the mapa%Apply an action to all targets (like  or ):+traverseOf both readFile ("file1", "file2")*(<contents of file1>, <contents of file2>)traverseOf _1 id (Just 1, 2) Just (1, 2)traverseOf _1 id (Nothing, 2)Nothing9You can also just apply the lens/traversal directly (but a might be more readable): both readFile ("file1", "file2")*(<contents of file1>, <contents of file2>)"If you don't need the result, use [.baD with flipped arguments. Useful if the loop body  is a lambda or a do block.ccL turns a traversal into a lens that behaves like a single-element traversal:[1,2,3] ^. singular each1![1,2,3] & singular each %~ negate[-1,2,3]4If there is nothing to return, it'll throw an error:[] ^. singular each3*** Exception: Lens.Micro.singular: empty traversal;However, it won't fail if you are merely setting the value:[] & singular each %~ negatedd` lets you chain traversals together; if the 1st traversal fails, the 2nd traversal will be used..([1,2],[3]) & failing (_1.each) (_2.each) .~ 0 ([0,0],[3])+([],[3]) & failing (_1.each) (_2.each) .~ 0([],[0])Note that the resulting traversal won't be valid unless either both traversals don't touch each others' elements, or both traversals return exactly the same results. To see an example of how d& can generate invalid traversals, see  ahttp://stackoverflow.com/questions/27138856/why-does-failing-from-lens-produce-invalid-traversalsthis Stackoverflow question.ee; is a traversal that filters elements passing  through it:(1,2,3,4) ^.. each [1,2,3,4]"(1,2,3,4) ^.. each . filtered even[2,4]3It also can be used to modify elements selectively:*(1,2,3,4) & each . filtered even %~ (*100) (1,200,3,400)The implementation of e] is very simple. Consider this traversal, which always traverses  just the value it's given: id ::  a a id f s = f s =And this traversal, which traverses nothing (in other words, doesn't traverse the value it's given):  ignored ::  a a ignored f s =  s eAnd now combine them into a traversal that conditionally traverses the value it's given, and you get e: filtered :: (a -> Bool) -> + a a filtered p f s = if p s then f s else  s By the way, note that e can generate illegal traversals  sometimes this can bite you. In particular, an optimisation that should be safe becomes unsafe. (To the best of my knowledge, this optimisation never happens automatically. If you just use eT to modify/view something, you're safe. If you don't define any traversals that use e, you're safe too.) Let's use evens as an example: evens = e  If evensF was a legal traversal, you'd be able to fuse several applications of evens like this: L evens f  L evens g = L evens (f  g) Unfortunately, in case of evens# this isn't a correct optimisation:the left-side variant applies g' to all even numbers, and then applies f) to all even numbers that are left after f (because f1 might've turned some even numbers into odd ones)the right-side variant applies f and g to all even numbersOf course, when you are careful and know what you're doing, you won't try to make such an optimisation. However, if you export an illegal traversal created with e and someone tries to use it, they might mistakenly assume that it's legal, do the optimisation, and silently get an incorrect result.If you are using e with some another traversal that doesn't overlap with -whatever the predicate checks-, the resulting traversal will be legal. For instance, here the predicate looks at the 1st element of a tuple, but the resulting traversal only gives you access to the 2nd: pairedWithEvens :: - [(Int, a)] [(Int, b)] a b pairedWithEvens = %  e (  )   hSince you can't do anything with the 1st components through this traversal, the following holds for any f and g: L pairedWithEvens f  L pairedWithEvens g = L pairedWithEvens (f  g) ff* traverses both fields of a tuple. Unlike  Ohttp://hackage.haskell.org/package/lens/docs/Control-Lens-Traversal.html#v:bothboth9 from lens, it only works for pairs  not for triples or .("str","ing") ^. both"string"("str","ing") & both %~ reverse ("rts","gni")ggK traverses the 1st element of something (usually a list, but can also be a Seq, etc):[1..5] ^? _headJust 1cIt can be used to modify too, as in this example where the 1st letter of a sentence is capitalised:,"mary had a little lamb." & _head %~ toTitle"Mary had a little lamb."fThe reason it's a traversal and not a lens is that there's nothing to traverse when the list is empty: [] ^? _headNothingThis package only lets you use g on lists, but if you use  0http://hackage.haskell.org/package/microlens-ghc microlens-ghc you get instances for  ByteString and Seq, and if you use  5http://hackage.haskell.org/package/microlens-platformmicrolens-platform$ you additionally get instances for Text and Vector.hh, gives you access to the tail of a list (or Seq, etc):[1..5] ^? _tailJust [2,3,4,5] You can modify the tail as well:[4,1,2,3] & _tail %~ reverse [4,3,2,1]%Since lists are monoids, you can use h with plain (UE) (and then it'll return an empty list if you give it an empty list):[1..5] ^. _tail [2,3,4,5] [] ^. _tail[]If you want to traverse each element of the tail, use h with %:&"I HATE CAPS." & _tail.each %~ toLower"I hate caps."This package only lets you use h on lists, but if you use  0http://hackage.haskell.org/package/microlens-ghc microlens-ghc you get instances for  ByteString and Seq, and if you use  5http://hackage.haskell.org/package/microlens-platformmicrolens-platform$ you additionally get instances for Text and Vector.ii; gives you access to all-but-the-last elements of the list:"Hello." ^. _init"Hello"See documentation for h, as i and h are pretty similar.jj2 gives you access to the last element of the list:"Hello." ^? _last'.'See documentation for g, as j and g are pretty similar.kThis generalizes  to an arbitrary 8. (Note that it doesn't work on folds, only traversals.)  mapAccumL "a k  ll# targets the value contained in an , provided it's a .Gathering all Lefts in a structure (like the / function, but not necessarily just for lists):*[Left 1, Right 'c', Left 3] ^.. each._Left[1,3]Checking whether an  is a  (like ):has _Left (Left 1)Truehas _Left (Right 1)False*Extracting a value (if you're sure it's a ):Left 1 ^?! _Left1Mapping over all s:5(each._Left %~ map toUpper) [Left "foo", Right "bar"][Left "FOO",Right "bar"]Implementation: l f (Left a) =   f a l _ (Right b) =  ( b) mm# targets the value contained in an , provided it's a .See documentation for l.nn" targets the value contained in a , provided it's a .See documentation for lO (as these 2 are pretty similar). In particular, it can be used to write these:#Unsafely extracting a value from a :   = (Z n) Checking whether a value is a :   = ] n  Converting a 5 to a list (empty or consisting of a single element):   = (W n) Gathering all  s in a list:   = (W %  n) oo targets a () if the  is a (, and doesn't target anything otherwise:Just 1 ^.. _Nothing[]Nothing ^.. _Nothing[()]5It's not particularly useful (unless you want to use ] o as a replacement for '), and provided mainly for consistency.Implementation: o f Nothing =    f () o _ j =  j ? !%&'+JKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmno?J +KLMNOPRSTQ UVWXYZ[\]'^_`abcdef&%!ghijklmno J1K4M4N4P4R4S4T4U8W8Y8Z8d5,(C) 2013-2016 Edward Kmett, 2015-2016 Artyom BSD-style (see the file LICENSE) Trustworthy)ww is a synonym for (U):view _1 (1, 2)1The reason it's not in  Lens.Micro is that view& in lens has a more general signature: 0view :: MonadReader s m => Getting a s a -> m a "So, you would be able to use this wu with functions, but not in various reader monads. For most people this shouldn't be an issue; if it is for you, use view from  0http://hackage.haskell.org/package/microlens-mtl microlens-mtl.xx is a synonym for (Y):preview _head [1,2,3]Just 1The reason it's not in  Lens.Micro is that preview& in lens has a more general signature: Cpreview :: MonadReader s m => Getting (First a) s a -> m (Maybe a) Just like with w , you would be able to use this xL with functions, but not in reader monads; if this is an issue for you, use preview from  0http://hackage.haskell.org/package/microlens-mtl microlens-mtl.wxwx  !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm nopqrstuvwxyz{|}~  µlens-0.4.9-DJbCdaTr4vu74o1cRJdT48 Lens.MicroLens.Micro.InternalLens.Micro.TypeLens.Micro.Extras<<%~^.^..^? Data.MonoidEndoview&.~?~non Control.Arrowsecond Data.Functor<$preview Data.Foldable traverse_for_Data.Traversabletraverse mapAccumL Data.EitherleftsisLeftbase Data.FunctionGHC.Stack.Types HasCallStack LensLike'LensLike Traversal' TraversalLens'Lens SimpleFoldGetting SimpleGetterASetter'ASetterStrictstrictlazySnoc_SnocCons_ConsField5_5Field4_4Field3_3Field2_2Field1_1AtatIxedixIxValueIndexEacheach traversedfoldedfoldringfoldrOf foldMapOfsetsphantom#..#ixAt$fEachNonEmptyNonEmptyab$fEachMaybeMaybeab $fEach[][]ab$fEachComplexComplexab$fEach(,,,,)(,,,,)aq$fEach(,,,)(,,,)aq$fEach(,,)(,,)aq$fEach(,)(,)aq$fIxed[] $fIxed(->)$fField1(,,,,)(,,,,)aa'$fField1(,,,)(,,,)aa'$fField1(,,)(,,)aa'$fField1(,)(,)aa'$fField2(,,,,)(,,,,)bb'$fField2(,,,)(,,,)bb'$fField2(,,)(,,)bb'$fField2(,)(,)bb'$fField3(,,,,)(,,,,)cc'$fField3(,,,)(,,,)cc'$fField3(,,)(,,)cc'$fField4(,,,,)(,,,,)dd'$fField4(,,,)(,,,)dd'$fField5(,,,,)(,,,,)ee' $fCons[][]ab $fSnoc[][]ab<&>%~over<>~setmapped<%~<<.~totoListOf^?! traverseOf_forOf_hasfoldinglens traverseOfforOfsingularfailingfilteredboth_head_tail_init_last mapAccumLOf_Left_Right_Just_Nothing$fMonoidTraversed$fApplicativeBazaar$fFunctorBazaar$fMonadFailStateT $fMonadStateT$fApplicativeStateT$fFunctorStateTGHC.Base ApplicativeFunctorData.Functor.ConstConstData.Functor.IdentityIdentitygetConstGHC.Err undefined<$> Data.Tuplefst<*>puresndNothingJustGHC.List!!maximumghc-prim GHC.TypesInt GHC.ClassesEq TraversableMaybe GHC.Float RealFloat Data.ComplexComplexFoldable>>=reversefmapconstMonoidFirstheadEithermapM_ Data.Maybe isNothingisJustmapMGHC.Realeven.LeftRightfromJust maybeToList catMaybesStateT runStateTBazaar Traversed getTraversed