I am trying to implement an immutable data structure that models IT networks and instances (computers). Here is a simplified version:
object Sample {
case class Instance(id: String, flag: Boolean)
case class Network(id: String, instances: Set[Instance])
case class Repo(networks: Map[String, Set[Network]])
// return new Repo with the instance with id == instanceId updated
// how to do this using lenses?
def updateInstanceFlag(networksKey: String, instanceId: String, flag: Boolean): Repo = ???
}
The updateInstanceFlag function should create an updated copy of the data with the corresponding instance (with id instanceId) modified. I tried to implement this using lenses but the code was too complex. Specifically, I had hard time composing the locating of an instance or network by ID with the updating of the data structure. Returning Optional values from queries also contributed to the complexity of the lenses. I used my own implementation of lenses but have no real preference (I know of lenses implementation from Shapeless, Monocle, Scalaz).
I'd appreciate people's thoughts and experiences on how to maintain 'real' immutable data.
Thanks.
Repocase class?