Skip to content

Instantly share code, notes, and snippets.

@misberner
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misberner/eec5e74f759353da3384 to your computer and use it in GitHub Desktop.
Save misberner/eec5e74f759353da3384 to your computer and use it in GitHub Desktop.
case class MyClass(intAttr: Int, listAttr: List[String], mapAttr: Map[String,Int]) {
def updated(
intAttr : Int => Int = identity,
listAttr : List[String] => List[String] = identity,
mapAttr : Map[String,Int] => Map[String,Int] = identity) = {
MyClass(intAttr(this.intAttr), listAttr(this.listAttr), mapAttr(this.mapAttr))
}
}
object Test extends App {
override def main(args: Array[String]) {
val x = MyClass(42, List("foo", "bar"), Map("baz" -> 37, "qux" -> 1000))
val y = x.updated(intAttr = _ * 100, listAttr = "42" :: _) // MyClass(4200, ...)
val z = y.updated(listAttr = _.reverse, mapAttr = _.updated("baz", 73))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment