Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
C HAPTER 23 ยท For Expressions Revisited You could optimize this example bit by using a withFilter call instead of filter . This would avoid the creation of an intermediate data structure for male persons: scala> persons withFilter (p => !p.isMale) flatMap (p => (p.children map (c => (p.name, c.name)))) res1: List[(String, String)] = List((Julie,Lara), (Julie,Bob)) These queries do their job, but they are not exactly trivial to write or un- derstand. Is there a simpler way? In fact, there is. Remember the for expressions in Section 7.3? Using a for expression, the same example can be written as follows: scala> for (p <- persons; if !p.isMale; c <- p.children) yield (p.name, c.name) res2: List[(String, String)] = List((Julie,Lara), (Julie,Bob)) The result of this expression is exactly the same as the result of the previous