How to use monocle to modify a nested map and another field in scala

坚强是说给别人听的谎言 提交于 2019-12-05 18:55:04

You can get a slightly shorter version using index instead of at:

(mem composeOptional index("a")).modify(_ + 1) andThen pointer.modify(_ + 1)

However, mergeLens also known as horizontal composition does not satisfy the Lens law if the two Lenses point to the same field:

  import monocle.macros.GenLens

  case class Person(name: String, age: Int)
  val age = GenLens[Person](_.age)

  val age2 = mergeLens(age, age)
  val john = Person("John", 25)
  age2.get(age2.set((5, 10))(john)) != (5, 10)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!