Created
June 19, 2018 17:28
-
-
Save robhinds/4f7f2ca59c6b78e1616c4e7b3b26cb00 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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