Hi all,
I have a situation where I want to update 2 fields of an record as part of the same operation (see below). Currently, I can achieve this by composing 2 lenses together, but it seems that this copies an unnecessary intermediate object. Ideally, I would like to make a single copy of the containing object, containing both changes. Is there a neat way to express this?
-Ben
case class Board(itemLocations: Map[Item#Id, Location],
itemAtLocation: Map[Location#Id, Option[Item]]) {
val _itemLocations = lenser(_.itemLocations)
val _itemAtLocation = lenser(_.itemAtLocation)
def place(m: Item, l: Location): Board = {
require(!itemLocations.contains(m.id)) require(!itemAtLocation(l.id).contains(m))
_itemLocations.modify(_.updated(m.id, l)).compose( _itemAtLocation.modify(_.updated(l.id, Some(m))))(this) }
}