Nested iteration in Scala

Viewed 24333

What is the difference (if any) between two code fragments below?

Example from Ch7 of Programming i Scala

def grep(pattern: String) = 
  for (
    file <- filesHere
    if file.getName.endsWith(".scala");
    line <- fileLines(file)
    if line.trim.matches(pattern)
  ) println(file + ": " + line.trim)

and this one

def grep2(pattern: String) = 
  for (
    file <- filesHere
    if file.getName.endsWith(".scala")
  ) for (
    line <- fileLines(file)
    if line.trim.matches(pattern)
  ) println(file + ": " + line.trim)

Or

for (i <- 1 to 2)
  for (j <- 1 to 2)
    println(i, j)

and

for (
  i <- 1 to 2;
  j <- 1 to 2
) println(i, j)
2 Answers
Related