planState.copy(plan = flatMapped) is the more idiomatic way to change just one field in a case class. Or since you reference planState.plan and planState.visited, can do a val PlanState(plan, visited) = planState at the beginning and use where necessary