To augment Dave’s answer, here is a translation schema for ‘for-comprehensions’ from Scala language specification:
A comprehension
for (enums) yield e
evaluates expressione
for each binding generated by the enumerators enums. An enumerator sequence always starts with a generator; this can be followed by further generators, value definitions, or guards.A generator
p <- e
produces bindings from an expressione
which is matched in some way against patternp
. A value definitionval p = e
binds the value namep
(or several names in a patternp
) to the result of evaluating the expressione
. A guardif e
contains a boolean expression which restricts enumerated bindings.The precise meaning of generators and guards is defined by translation to invocations
of four methods:map
,filter
,flatMap
, andforeach
. These methods can be implemented in different ways for different carrier types.The translation scheme is as follows. In a first step, every generator
p <- e
, where p is not irrefutable (ยง8.1) for the type ofe
is replaced byp <- e.filter { case p => true; case _ => false }
Then, the following rules are applied repeatedly until all comprehensions have been
eliminated.
A for-comprehension
for (p <- e) yield e0
is translated toe.map { case p => e0 }
.A for-comprehension
for (p <- e) e0
is translated toe.foreach { case p => e0 }
.A for-comprehension
for (p <- e; p0 <- e0 . . .) yield e00
, where . . . is a (possibly empty) sequence of generators or guards, is translated
to:e.flatMap { case p => for (p0 <- e0 . . .) yield e00 }
.A for-comprehension
for (p <- e; p0 <- e0 . . .) e00
where . . . is a (possibly empty) sequence of generators or guards, is translated to:
e.foreach { case p => for (p0 <- e0 . . .) e00 }
.A generator
p <- e
followed by a guardif g
is translated to a single generator:
p <- e.filter((x1, . . . , xn) => g )
wherex1
, . . . ,xn
are the free variables
ofp
.A generator
p <- e
followed by a value definitionval p0 = e0
is translated
to the following generator of pairs of values, wherex
andx0
are fresh names:val (p, p0) <- for(x@p <- e) yield { val x0@p0 = e0; (x, x0) }