I was just reading Daniel Spiewak's blog regading the use of Scala's flatmap operation. It seems to be one of those confusing parts of Scala so I thought I'd post a clear example of how and why you'd want to use a flatmap on a list of things.






import junit.framework.TestCase
import scala.collection.mutable.ListBuffer

class Person {

val pets = new ListBuffer[String]()
}

class ScalaTest extends TestCase {

def testFlatMap() {

// let's create some people, add some pets to them
// the end result is we went to know all the pet's that these people have
val p = new Person;
p.pets += "cat"
p.pets += "dog"

val d = new Person;
d.pets += "fish"
d.pets += "bird"
d.pets += "dog"

val people = p :: d :: Nil

val pets = people.flatMap(_.pets)

// show me all the pets that we have in our people collection
println(pets) // List(cat, dog, fish, bird, dog)

// get just the unique pets that everyone has
println(pets.toSet) // Set(cat, dog, fish, bird)

}

}

Ready for More?

Follow Me @jimplush