With the following code snippet:
data Circle = Circle
{ center :: Point
, radius :: Double
}
data Point = Point (Double, Double)
someFuncOverPoint :: State Point blahblah
I wonder if there is a function that can make the someFuncOverPoint to focus on Circle:
someMagicFunc :: ??? -> State Point blahblah -> State Circle blahblah
Maybe this can be implemented using lens?
Strictly speaking you can. Indeed, we can create a state by first generating the first
State Point a, and then pass that into aState, so:So here we construct a
Statethat works with a function that maps the initial states0to the next states1and the result, and then we turn that into a 2-tuple with theCircleas state, and the "result"aas well.That being said, it is a bit strange to change the type of the state. Usually the type of the state remains the same over all the actions.