Using a lens library I can apply a modification function to individual targets, like so:
Prelude Control.Lens> (1, 'a', 2) & _1 %~ (*3)
(3,'a',2)
Prelude Control.Lens> (1, 'a', 2) & _3 %~ (*3)
(1,'a',6)
How can I combine those individual lenses (_1
and _3
) to be able to perform this update to both of the targets at once? I expect something in the spirit of the following:
Prelude Control.Lens> (1, 'a', 2) & ??? %~ (*3)
(3,'a',6)
Using
untainted
from theSettable
type class inControl.Lens.Internal.Setter
, it is possible to combine two setters, but the result will also only be a setter and not a getter.You can test this:
EDIT
You don't actually need to use internal functions. You can use the fact that Mutator is a monad:
There is a variation on what you are asking for which is more general:
Just focus on the type signature with lens type synonyms:
It takes two lenses and combines them into a lens to a pair of fields. This is slightly more general and works for combining lenses that point to fields of different types. However, then you'd have to mutate the two fields separately.
I just wanted to throw this solution out there in case people were looking for something like this.