Skip to content

Instantly share code, notes, and snippets.

@robhinds
Created June 19, 2018 17:28
Show Gist options
  • Save robhinds/4f7f2ca59c6b78e1616c4e7b3b26cb00 to your computer and use it in GitHub Desktop.
Save robhinds/4f7f2ca59c6b78e1616c4e7b3b26cb00 to your computer and use it in GitHub Desktop.
class StatisticsActor extends Actor {
import StatisticsActor._
self ! Init
def receive: Receive = {
case Init => context become ready(Map[String, Stats]())
}
def ready(stats: Map[String, Stats]): Receive = {
case UpdateStats(c, i) =>
val newStats = c.foldLeft(stats) { (a, country) =>
a + (country.name -> a.getOrElse(country.name, Stats()).update(i))
}
context become ready(newStats)
case GetAllStats => sender() ! stats
}
}
object StatisticsActor {
case object Init
case class UpdateStats(c: Seq[Country], i: Option[Int] = None)
case object GetAllStats
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment