What's a succinct way to implement the ensureRecAFlag function using lenses?
- If RecB contains a RecA with
recaFlag=Truethen do nothing - Else, if RecB contains a RecA with
recaName="internal_code"then setrecaFlag=Truefor that RecA - Else, if RecB contains a RecA with
recaName="id"then setrecaFlag=Truefor that RecA - Else, do nothing
ensureRecAFlag :: RecB -> RecB
ensureRecAFlag = _todo
data RecA = RecA
{ recaName :: !Text
, recaValue :: !Int
, recaFlag :: !Bool
}
$(makeLensesWith abbreviatedFields ''RecA)
data RecB = RecB
{ recbName :: !Text
, recbRecAList :: ![RecA]
}
$(makeLensesWith abbreviatedFields ''RecB)
I propose that you make a monoid that captures your change hierarchy.
Now you can inspect records independently and inject their modified forms into this type.
Your top-level function is now straightforward.
This solution has no lenses, but it does have some nice properties: it does just one traversal of the list; it uses only beginner-level Haskell features so it is straightforward to read and update as requirements change (if not necessarily easy); and it is structured in a way that makes it convenient to return the exact object passed when nothing changes rather than a newly-allocated copy.