I need a function like <<%~
which would act with Traversal
s in similar fashion to ^?
, like this:
(<<?%~) :: Traversal s t a b -> (a -> b) -> s -> (Maybe a, t)
> ix 0 <<?%~ succ $ [1,2]
(Just 1,[2,2])
> ix 1 <<?%~ succ $ [1,2]
(Just 2,[1,3])
> ix 2 <<?%~ succ $ [1,2]
(Nothing,[1,2])
How should I implement it? The obvious way is to apply ^?
and %~
separately, but I'd like a solution in one go.
If we don't want to require a Monoid
constraint on the targets, we have to specify ourselves the Monoid
that will be used for combining the old elements in a traversal. As the goal is something analogous to ^?
, the appropriate monoid is First
.
(<<?%~) :: LensLike ((,) (First a)) s t a b -> (a -> b) -> s -> (Maybe a, t)
l <<?%~ f = first getFirst . (l $ \a -> (First (Just a), f a))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With