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