A for-comprehension expresses a sequence of bindings followed by a yield. Scala translates the bindings into flatMap calls and the final binding and yield into map, making the construct a syntax convenience rather than a new operation. section 4.3.2
Rewriting map2
The explicit form a.flatMap(aa => b.map(bb => f(aa, bb))) can be written as: for aa <- a bb <- b yield f(aa, bb) Both forms combine two Option values and apply f only through the composition provided by map and flatMap. section 4.3.2
def map2[A, B, C](a: Option[A], b: Option[B])(f: (A, B) => C): Option[C] =
for
aa <- a
bb <- b
yield f(aa, bb)
Compare the for-comprehension directly with its equivalent map and flatMap implementation. section 4.3.2
