On 07/03/2014 19:08, Ognen Duzlevski wrote:
I have had the most awful time figuring out these "looped" things. It
seems like it is next to impossible to run a .filter() operation in a
for loop, it seems to work if you yield .filter()
The equivalent of a filter in a for statement is an 'if'. Scala desugars
for comprehensions into the equivalent sequence of map, flatMap and
withFilter invocations, section 23.4 in "Programming with Scala" has a
very good explanation of how the mapping is done.
$ scala -Xprint:parser -e 'println(for(i <- Seq(1,2,3,4) if (i % 2) ==
0; j <- Seq(10,11) if j <= 10) yield i * j)'
[[syntax trees at end of parser]] //
scalacmd2816067526771161181.scala
package <empty> {
object Main extends scala.AnyRef {
def <init>() = {
super.<init>();
()
};
def main(argv: Array[String]): scala.Unit = {
val args = argv;
{
final class $anon extends scala.AnyRef {
def <init>() = {
super.<init>();
()
};
println(Seq(1, 2, 3, 4).withFilter(((i) =>
i.$percent(2).$eq$eq(0))).flatMap(((i) => Seq(10, 11).withFilter(((j) =>
j.$less$eq(10))).map(((j) => i.$times(j))))))
};
new $anon()
}
}
}
}
List(20, 40)
$
--
Alan Burlison
--