You can use over (inner.traverse.foo) to write to a nested field. This will not report a failure the way you want, because it managed to match all 0 targets.
traverse (from Data.Traversable , re-exported using Control.Lens ), gives you Traversal to navigate Maybe .
You can read with (^?) To find out if the objective of the lens exists.
We can solve this in several ways, using existing lens combinators for reading and writing separately, but we could just build such a combinator directly:
import Data.Monoid (Any(..)) import Control.Monad (guard) overish :: LensLike ((,) Any) stab -> (a -> b) -> s -> Maybe t overish lfs = case l (\a -> (Any True, fa)) s of (Any r, t) -> t <$ guard r
You can write this with l %%~ \a -> (Any True, fa) .
You can easily check if there are Traversal objects without using nullOf , but this will require two passes and a higher type of rank:
overish :: Traversal stab -> (a -> b) -> s -> Maybe t overish lfs = over lfs <$ guard (not (nullOf ls))
This is just a check to see what the goal is, and then apply the installer to it, if there are goals.
Then you can just use
overish (inner.traverse.foo) succ
Edward KMETT
source share