An analogue of "<<% ~" that does not require a monoid to bypass

I need a function like <<%~ that will act with Traversal same way ^? , eg:

 (<<?%~) :: Traversal stab -> (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 do I implement it? The obvious way is to use ^? and %~ individually, but I would like to solve one solution.

+5
source share
1 answer

If we do not want to require a Monoid constraint for purposes, we must specify the Monoid , which will be used to merge the old elements bypass. Since the goal is something similar ^? , the corresponding monoid is First .

 (<<?%~) :: LensLike ((,) (First a)) stab -> (a -> b) -> s -> (Maybe a, t) l <<?%~ f = first getFirst . (l $ \a -> (First (Just a), fa)) 
+4
source

Source: https://habr.com/ru/post/1258057/


All Articles