Skip to content

Instantly share code, notes, and snippets.

@dwhitney
Created July 6, 2011 15:00
Show Gist options
  • Select an option

  • Save dwhitney/1067443 to your computer and use it in GitHub Desktop.

Select an option

Save dwhitney/1067443 to your computer and use it in GitHub Desktop.
private val map = new java.util.HashMap[String, Account]()
map.put("from", Account("from", 500))
map.put("to", Account("to", 500))
val accountsRef = Ref(map)
def transfer(fromNo: String, toNo: String, amount: Int): Unit = {
atomic{ implicit t =>
var accounts = accountsRef()
val (from, to) = (accounts.get(fromNo), accounts.get(toNo))
if(from.balance > amount){
accounts = accounts.put(to.accountNo, to.credit(amount))
accounts = accounts.put(from.accountNo, from.debit(amount))
accountsRef() = accounts
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment