Is there a lens operator to combine %~ and fmap

217 views Asked by At

I have the following code

u & currentDay %~ fmap (addDays 1)

currentDay returns a Maybe which is why I need the fmap. Is there already an operator to combine %~ and fmap (like %~<$> ;-)) or a clever way to do so ?

1

There are 1 answers

0
cchalmers On BEST ANSWER

There is no existing operator for this but you could easily define your own:

%$~ :: Functor f => ASetter s t (f a) (f b) -> (a -> b) -> s -> t
l %$~ f = over a (fmap f)

The standard way to do this is to use the mapped setter:

u & currentDay . mapped %~ addDays 1

Since your mapping over a Maybe you could also use _Just prism:

u & currentDay . _Just %~ addDays 1