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

## Wednesday, September 02, 2015

### Scala : collectFirst example

On occasion, you want to find the first occurrence of an item in a list, then transform it to another type. Using partial functions with collectFirst, we can accomplish this.

collectFirst will first call isDefinedAt to determine if apply should be called for the current item in the list. Thus it will skip over the non matching elements in the list without incurring a match exception.

trait Animal
case class Mammal(name: String) extends Animal
case class Bird(name: String) extends Animal

val animals = Seq(Mammal("elephant"), Mammal("tiger"), Bird("raven"), Mammal("monkey"), Bird("peacock"), Bird("sparrow"))

val matchBird = new PartialFunction[Animal, Bird] {
def apply(p: Animal) = {
p match {
case b@Bird(name) => b
}
}

def isDefinedAt(p: Animal) = {
p match {
case Bird(name) => true
case _ => false
}
}
}

animals collectFirst matchBird

res11: Option[Bird] = Some(Bird(raven))