## Saturday, September 05, 2015

### Scala : Another example of collectFirst

Say, we want to find the population of the native country of the first European bird in a list of birds. This can be done by flatmapping over the birds, thus ignoring the non-European birds, and then getting the head of the resulting list. But then, we have to go over each bird, although we need to just get the first bird. If the list is sufficiently large, this is very inefficient.

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)}
}
}

``````