Skip to content

Instantly share code, notes, and snippets.

@dwhitney
Created July 5, 2011 21:48
Show Gist options
  • Select an option

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

Select an option

Save dwhitney/1066049 to your computer and use it in GitHub Desktop.
import scala.actors._
import Actor._
object Bank{
val accounts = Map("from" -> new Account("from", 500), "to" -> new Account("to", 500) )
def transfer(fromNo: String, toNo: String, amount: Int): Unit = {
val (from, to) = (accounts(fromNo), accounts(toNo))
val balance = (from !? Balance).asInstanceOf[Int]
if(balance > amount){
to ! Credit(amount)
from ! Debit(amount)
}
}
}
case class Credit(amount: Int)
case class Debit(amount: Int)
case object Balance
class Account(val accountNo: String, beginningBalance: Int) extends Actor{
private var _balance = beginningBalance
def act = {
loop{
receive{
case Credit(amount) => _balance = _balance + amount
case Debit(amount) => _balance = _balance - amount
case Balance => reply(_balance)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment