I'm struggling with using the lens
library for a particular problem. I'm trying to pass
- an updated data structure
- a lens focussed on part of that updated structure
to another function, g
. I pass both the lens and the data structure because g
needs some shared information from the data structure as well as a piece of information. (If it helps, the data structure contains information on a joint probability distribution, but g
only works on either marginal and needs to know which marginal I'm looking at. The only difference between the two marginals is their mean with the rest of their definition being shared in the data structure).
My first attempt looked like this
f :: Functor f => Params -> ((Double -> f Double) -> Params -> f Params) -> a
f p l = g (l %~ upd $ p) l
where upd = ...
g p x = go p p^.x
but that fails during compilation because f
gets inferred as being Identity
for the update and Const Double
for the getter.
What's the best way to accomplish what I want to do? I can imagine being able to do one of the following:
- make a copy of the lens so that the type inference can be different in each case
- rather than passing the updated structure and the lens, I pass the original structure and a lens which returns a modified value (if I only want to update the part of the structure that the lens looks at).
- making a better design choice for my functions/data structure
- something completely different
Thanks for any help!