3

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.

4
  • This is not very clear to me, can't you just return a new Repo case class? Commented Aug 12, 2014 at 6:20
  • Using case class copy to update a nested data structure can become very complex since you need to copy at every level - in the example above I'd need to copy an instance, the network containing the instance and finally the Repo. It gets worst the deeper the nesting of the data. Commented Aug 12, 2014 at 6:28
  • Now I see what you mean. Commented Aug 12, 2014 at 6:31
  • May I suggest prepending "update nested" to the title of the question? I think it will make its intent much clearer. Commented Aug 17, 2014 at 13:08

1 Answer 1

3

If I can convince you to change the instances field of Network into a Map as well, you can do it quite reasonably:

object Sample {

  case class Instance(id: String, flag: Boolean)
  case class Network(id: String, instances: Map[String, 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(repo: Repo, networksKey: String,
      instanceId: String, flag: Boolean): Repo = {

    val nets0 = repo.networks
    val net0 = nets0(networksKey)  // TODO fail gracefully
    val inst0 = net0.instances(instanceId) // TODO fail gracefully
    val inst1 = inst0.copy(flag = flag)
    val net1 = net0 + (instanceId -> inst1)
    val nets1 = nets0 + (networksKey -> net1)

    repo.copy(networks = nets1)
  }

}

Since the symmetry in how the code descends and reascends has probably become apparent, it might be worth factoring some part out in a method that can update an instance flag on a single network:

def updateInstanceFlag(net: Network, instanceId: String,
    flag: Boolean): Repo = {

  val inst1 = net.instances(instanceId).copy(flag = flag)
  net.copy(instances = net.instances + (instanceId -> inst1))
}

def updateInstanceFlag(repo: Repo, networksKey: String,
    instanceId: String, flag: Boolean): Repo = {

  val net1 = updateInstanceFlag(repo.networks(networksKey), instanceId, flag)
  repo.copy(networks = repo.networks + (networksKey -> net1))
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Changing the instances data structure is fine, however I think this approach might also not scale well with a complex and deeply nested data structure
No it won't. But that's really hard to get there. You might want to consider creating generic update functions for each level and pass them closures to update the level.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.