for expressions versus foreach in Scala
python uses for
in list comprehensions and generator expressions. Those are very similar to the scala for
expression:
this is python
>>> letters = ['a', 'b', 'c', 'd']
>>> ints = [0, 1, 2, 3]
>>> [l + str(i) for l in letters for i in ints if i % 2 == 0]
['a0', 'a2', 'b0', 'b2', 'c0', 'c2', 'd0', 'd2']
this is scala
scala> val letters = List('a', 'b', 'c', 'd')
scala> val ints = List(0, 1, 2, 3)
scala> for (l <- letters; i <- ints if i % 2 == 0) yield l.toString + i
res0: List[java.lang.String] = List(a0, a2, b0, b2, c0, c2, d0, d2)
Each construct can take a number of generators/iterators, apply filters expressions and yield a combined expression. In python the (expr for v1 in gen1 if expr1 for v2 in gen2 if expr2)
is roughly equivalent to:
for v1 in gen1:
if expr1:
for v2 in gen2:
if expr2:
yield expr
In scala for (v1 <- gen1 if expr1; v2 <- gen2 if expr2) yield expr
is roughly equivalent to:
gen1.withFilter(expr1).flatMap(v1 => gen2.withFilter(expr2).map(v2 => expr))
If you love the python for x in xs
syntax, you'll likely love the scala for
expression.
Scala has some additional syntax and translation twists. Syntax wise for
can be used with braces so that you can put statements on separate lines. You can also perform value assignments.
val res = for {
i <- 1 to 20; i2 = i*i
j <- 1 to 20; j2 = j*j
k <- 1 to 20; k2 = k*k
if i2 + j2 == k2
} yield (i, j, k)
Also v1 <- gen1
really performs a match case v1 => gen1
. If there is no match those elements are ignored from the iteration.
scala> val list = List(Some(1), None, Some(2))
scala> for (Some(i) <- list) yield i
res2: List[Int] = List(1, 2)
I think for
has an important place in the language. I can tell from the fact there is a whole chapter (23) about it in the book you're reading!
Yes, Scala for comprehensions (as they are commonly known) are used a lot, but they are really just syntactic sugar for a particular combination of methods, and many prefer to call these methods directly instead of using the syntactic sugar.
To better understand Scala for comprehensions, please refer to this question. In particular, you'll see that for (x <- xs) f(x)
is the same thing as xs.foreach(x => f(x))
.
Now, you mention that you don't seem much use with foreach
method, but I'll point out that almost all of the methods of Scala collections are (or can be) implemented with just foreach
. See the documentation for Traversable
-- all of its methods can be implemented with only foreach
.
Note that Scala's yield
bears no resemblance to Python's yield
-- you can look up that question too.
With its support for nested iteration, filters, and transformation, I'd say Scala's for
is one of the strong points of the language and very central. I tend to favor it over using foreach
, map
and filter
.
The foreach is a functional style while the for is an imperative style. If you've ever done any lisp or scheme, you're already familiar with functional programming. If you haven't then it might be a bit confusing at first. The first thing I would do is read up on the closure syntax which are anonymous functions you pass into things like foreach. Once you understand that it will all make more sense.
Your questions are largely answered by the following:
Scala's For Comprehensions
Scala Yield
To summaraize: It's largely stylistic. Personally, I favor the functional methodology, but prefer the succinctness of comprehensions when dealing with nested loops.