The other approach is to use find to get the first European bird, and then simply get its native country's population. But here we have to get the native country of the bird twice. In this example, that is cheap, but if this information requires an expensive database retrieval, then we may not want to do it twice. Where collectFirst becomes very important is when this operation is very expensive - imagine doing a statistical calculation that provides the value for the match.
case class Bird(name: String)
object PlayCollect {
val birds = Seq(Bird("jaybird"), Bird("owl"), Bird("sparrow"))
val nativeCountries = Map( (Bird("jaybird")->"Malaysia"), (Bird("sparrow")->"Ireland"), (Bird("owl")->"Chile") )
val continents = Map( ("Malaysia" -> "Asia"), ("Ireland" -> "Europe"), ("Chile" -> "South America") )
val population = Map( ("Malaysia"->10000000), ("Ireland" -> 12000000), "Chile"->45000000 )
val popEuBird = new PartialFunction[Bird, Int] {
def apply(b: Bird) = {
continents(nativeCountries(b)) match {
case "Europe" => population(nativeCountries(b))
}
}
def isDefinedAt(b: Bird) = {
continents(nativeCountries(b)) match {
case "Europe" => true
case _ => false
}
}
}
def populationOfNativeCountryOfFirstEuropeanBird: Option[Int] = {
birds collectFirst popEuBird
}
def main (args: Array[String]) {
populationOfNativeCountryOfFirstEuropeanBird.foreach{pop => println(pop)}
}
}
No comments:
Post a Comment