^?

Perform a safe head of a Fold or Traversal or retrieve Just the result from a Getter or Lens. When using a Traversal as a partial Lens, or a Fold as a partial Getter this can be a convenient way to extract the optional value. Note: if you get stack overflows due to this, you may want to use firstOf instead, which can deal more gracefully with heavily left-biased trees. This is because ^? works by using the First monoid, which can occasionally cause space leaks.
>>> Left 4 ^?_Left
Just 4
>>> Right 4 ^?_Left
Nothing
>>> "world" ^? ix 3
Just 'l'
>>> "world" ^? ix 20
Nothing
This operator works as an infix version of preview.
(^?) ≡ flip preview
It may be helpful to think of ^? as having one of the following more specialized types:
(^?) :: s -> Getter s a     -> Maybe a
(^?) :: s -> Fold s a       -> Maybe a
(^?) :: s -> Lens' s a      -> Maybe a
(^?) :: s -> Iso' s a       -> Maybe a
(^?) :: s -> Traversal' s a -> Maybe a
s ^? t returns the 1st element t returns, or Nothing if t doesn't return anything. It's trivially implemented by passing the First monoid to the getter. Safe head:
>>> [] ^? each
Nothing
>>> [1..3] ^? each
Just 1
Converting Either to Maybe:
>>> Left 1 ^? _Right
Nothing
>>> Right 1 ^? _Right
Just 1
A non-operator version of (^?) is called preview, and – like view – 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 preview available in Lens.Micro.Extras.
Flipped infix version of preview.
Returns Just the first referenced value. Returns Nothing if there are no referenced values.
(^?) :: s -> Fold s t a b -> Maybe a
Returns Just the first referenced value. Returns Nothing if there are no referenced values.
Deprecated: Use corresponding function from lens or microlens package
q ^? x. if the result of x isPrefixOf q, return True
Calculate the exclusive or of two booleans with confidence lower bounds.
Perform an *UNSAFE* head of a Fold or Traversal assuming that it is there.
>>> Left 4 ^?! _Left
4
>>> "world" ^?! ix 3
'l'
(^?!) :: 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
(^?!) is an unsafe variant of (^?) – instead of using Nothing to indicate that there were no elements returned, it throws an exception.
Perform an *UNSAFE* head of an affine fold assuming that it is there.
>>> Left 4 ^?! _Left
4
>>> "world" ^?! ix 3
'l'
>>> [] ^?! _head
*** Exception: (^?!): empty affine fold
...