In this article, we will see functional style of for
expression. Let’s get started with the simplest form of for
expression.
‘for’ Example
If you have come from java or c++, ‘<-‘ may look a bit odd, for now, think of it as ‘in’. Left side of ‘<-‘, we have declared a variable a
, it is of type val
and can’t be re-assigned within the block. In each loop, a character is assigned to it.
ForExample:
object ForExample { for (a <- "abc") print(a) def main(args: Array[String]): Unit = { } }
Output:
abc
Let’s see one more example. Here we define an array, traverse through each element and print.
ForExample:
object ForExample { for (a <- "abc") print(a) println var array = Array("d", "e", "f") for (a <- array) print(a) def main(args: Array[String]): Unit = { } }
Output:
abc def
Use of ‘to’ in ‘for’ loop also called Generator
The to
in this example is actually a method that takes one Int
argument.
(0).to(2).1
object ForExample { for (a <- "abc") print(a) println var array = Array("d", "e", "f") for (a <- array) print(a) println array = Array("x", "y", "z") for (a <- 0 to 2) print(array(a)) def main(args: Array[String]): Unit = { } }
Output:
abc def xyz
Let’s further analyze the to
function. In the below example, we use it as a method. Yo can eliminate the dot and parenthesis, if you explicitly specify the receiver of the method call. We call print(0 to 2)
and it prints the object’s toString
Range(0, 1, 2).
object ForExample { for (a <- "abc") print(a) println var array = Array("d", "e", "f") for (a <- array) print(a) println array = Array("x", "y", "z") for (a <- 0 to 2) print(array(a)) println (0).to(2).foreach(print) println print(0 to 2) def main(args: Array[String]): Unit = { } }
Output:
abc def xyz 012 Range(0, 1, 2)
If have seen how to feed a range of values using the to
method. These ranges are also called as generators.
Multiple Generators
Let’s now see how to create multiple ranges.
Instead of inner for loops, you can provide the ranges in the same for loop. See the highlighted one.
MultipleRanges:
object MultipleRanges { val array = Array("a", "b", "c", "x", "y", "z") for (a <- 0 to 2) for (b <- 3 to 5) print(array(a) + array(b)) println for (a <- 0 to 2; b <- 3 to 5) print(array(a) + array(b)) def main(args: Array[String]): Unit = { } }
Output:
axayazbxbybzcxcycz
Example of Guard
We can add filters to the for loop also called Guards. In the below example, we filter the odd indexed elements.
GuardExample:
object GuardExample { val array = Array("a0", "a1", "a2", "b0", "b1", "b2") for (a <- 0 to 5; if a % 2==0) print(array(a)) def main(args: Array[String]): Unit = { } }
Output:
a0a2b1
Multiple Guards
We can add multiple Guards. We now add one more filter, to filter elements which doesn’t start with ‘a’.
object GuardExample { val array = Array("a0", "a1", "a2", "b0", "b1", "b2") for (a <- 0 to 5; if a % 2==0) print(array(a)) println for (a <- 0 to 5; if a % 2==0; if array(a).charAt(0) == 'a') print(array(a)) def main(args: Array[String]): Unit = { } }
Output:
a0a2b1 a0a2
If you think the ranges and filters in the for loop are getting bigger, you can use curly braces and have each statement in a separate line. It adds to readability.
object GuardExample { val array = Array("a0", "a1", "a2", "b0", "b1", "b2") for (a <- 0 to 5; if a % 2 == 0) print(array(a)) println for (a <- 0 to 5; if a % 2 == 0; if array(a).charAt(0) == 'a') print(array(a)) println for { a <- 0 to 5; if a % 2 == 0; if array(a).charAt(0) == 'a' } print(array(a)) def main(args: Array[String]): Unit = { } }
Download the source code
In this article, we have seen some examples of for loop in scala. You can download the source code here: forExample.zip