Suppose we have a list of lenses [Lens' (S a) a] on a data structure S a. I want to modify the focus of each lens in the data structure in the same way.
I could do this like so:
s :: S a
s = _
ls :: [Lens' (S a) a]
ls = [a, b, c]
a, b, c = _
f :: a -> a
f = _
s' :: S a
s' = s
& a %~ f
& b %~ f
& c %~ f
That's OK, but what if I have 10, 100 lenses? I would like to have something like
s' :: S a
s' = s & ls ??? f
(???) :: *
where I cannot find the operator (???).
Maybe it is also possible to convert ls to a traversal and simply use (%~), I
don't know.
Do you have an idea?
The first problem is that you can't actually define the list
lsas you've done in your question: that type is illegal. As discussed in that question, to put a lens into a container you need to reify the lens. Once you've done that, you turn each lens into a function by applying(%~ f), and then it's a simple fold to compose those functions.What you get out is not a lens or a traversal, but merely a function of type
S a -> S a(the same as you get froma %~ f). I found a discussion of why it's not generally possible to combine multiple lenses, setters, or traversals into a single traversal in the way you mentioned you might hope for.Here's an implementation of the ideas I outlined above.