Skip to content

Instantly share code, notes, and snippets.

@softprops
Created October 21, 2011 13:25
Show Gist options
  • Save softprops/1303840 to your computer and use it in GitHub Desktop.
Save softprops/1303840 to your computer and use it in GitHub Desktop.
Stolen from the scala-user mailing list
case class Var[T](i: T) extends AtomicReference[T](i) {
def mutate(m: T => T): T = {
@tailrec def apply(): T = {
val c = get
val n = m(c)
if (compareAndSet(c, n)) n else apply()
}
apply()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment