Let's say I have a list of records, and I want to summarize it by taking the median. More concretely, say I have
data Location = Location { x :: Double, y :: Double }
I have a list of measurements, and I want to summarize it into a median Location
, so something like:
Location (median (map x measurements)) (median (map y measurements))
That is fine, but what if I have something more nested, such as:
data CampusLocation = CampusLocation { firstBuilding :: Location
,secondBuilding :: Location }
I have a list of CampusLocation
s and I want a summary CampusLocation
, where the median is applied recursively to all fields.
What is the cleanest way to do this in Haskell? Lenses? Uniplate?
Edit: Bonus:
What if instead of a record containing fields we want to summarize, we had an implicit list instead? For example:
data ComplexCampus = ComplexCampus { buildings :: [Location] }
How can we summarize a [ComplexCampus]
into a ComplexCampus
, assuming that each of the buildings
is the same length?
forall f. Traversable f => (f a -> b) -> (f s -> t)
. No idea if anyone thought of those yet. – BalckeFunctor
, which he says is enough) would be a "cotraversal" then. To make the types in the question actuallydistributive
, they would need to changeDouble
into a type parameter. – BalckeProfunctor p => p a b -> p s t
where we recover the Van Laarhoven formulation by demanding that p is a "representable" Profunctor. So, you're asking for a "corepresentable" Profunctor in your lens. – Bicapsular