July 2017
Intermediate to advanced
796 pages
18h 55m
English
FltatMap takes a function as an argument. The function given to flatMap() doesn't work on nested lists but it produces a new collection. It can be defined formally as follows:
def flatMap[B](f: (A) ⇒ GenTraversableOnce[B]): Traversable[B]
Let's see an example as follows:
//Applying function on nested lists and then combining output back togetherscala> List(List(2,4), List(6,8)) flatMap(x => x.map(x => x * x))res4: List[Int] = List(4, 16, 36, 64)
We have just about finished covering the uses of Scala collection features. Also note that methods such as Fold(), Reduce(), Aggregate(), Collect(), Count(), Find(), and Zip() can be used to pass from one collection to another (for example, toVector, toSeq, toSet, toArray). However, we will ...
Read now
Unlock full access