The foreach Loop in Scala
-
Using
foreach
Loop in Scala -
Using
foreach
Loop Withuntil
Flag in Scala -
Using
foreach
Loop Withif
Condition in Scala -
Using
foreach
Loop Withyield
in Scala -
Using
foreach
Loop to Iterate List in Scala -
Using the
foreach
() Function of Scala -
Using the
foreach
Function With Vector in Scala
foreach
is a loop used to iterate over the elements of an object/container. This tutorial will discuss the foreach
loop and learn how to implement it in Scala.
Using foreach
Loop in Scala
Scala provides a foreach()
function to traverse list elements. We created a foreach
loop that traverses numerical values to the specified limit in this example.
The loop requires two arguments, the first is the loop’s starting, and the second is the end.
Example:
object MyClass {
def main(args: Array[String]) {
print("for loop iterating...\n")
for( a <- 1 to 3 ){
println(a);
}
}
}
Output:
for loop iterating...
1
2
3
Using foreach
Loop With until
Flag in Scala
We will create a foreach
loop with the until
flag to traverse numerical values. It is useful when iterating the elements but excluding the last ones.
The loop requires two arguments, the first is the loop’s starting, and the second is the until
keyword.
Example:
object MyClass {
def main(args: Array[String]) {
print("for loop iterating...\n")
for( a <- 1 until 3 ){
println(a);
}
}
}
Output:
for loop iterating...
1
2
Using foreach
Loop With if
Condition in Scala
If you want to filter the data while traversing, you can use the if
condition with the loop expression. This is Scala’s way to get filtered data with concise code.
Here in the code snippet below, we only used the if
condition in the loop to get even values.
Example:
object MyClass {
def main(args: Array[String]) {
print("for loop iterating...\n")
for( a <- 1 to 10 if a%2==0 ){
println(a);
}
}
}
Output:
for loop iterating...
2
4
6
8
10
Using foreach
Loop With yield
in Scala
If you want to get a collection of items after traversing rather than traversing and displaying elements, then use the yield
keyword with the loop.
The yield
keyword returns a collection of the same traversed type, which means map returns map and list returns a list. Here, we traversed numeric values and got a vector after iterations of the loop.
Example:
object MyClass {
def main(args: Array[String]) {
print("for loop iterating...\n")
var result = for( a <- 1 to 5) yield a
print(result)
}
}
Output:
for loop iterating...
Vector(1, 2, 3, 4, 5)
Using foreach
Loop to Iterate List in Scala
Apart from imperative use, we can use the foreach
loop to iterate list elements. While working with built-in collections, we don’t need to specify any endpoint of the loop or until
keyword as the Scala interpreter handles it and exit the loop when no elements are left in the collection.
Example:
object MyClass {
def main(args: Array[String]) {
print("for loop iterating...\n")
var list = List(1,52,35,6,8,9)
for( i <- list){
println(i)
}
}
}
Output:
for loop iterating...
1
52
35
6
8
9
Using the foreach
() Function of Scala
Scala provides a built-in foreach()
function in the List class to iterate the list elements, so we no longer need to use a loop. As an argument, this function accepts a lambda expression and returns the result.
Here, we passed the print statement to the foreach()
to print the elements to the console.
Example:
object MyClass {
def main(args: Array[String]) {
print("for loop iterating...\n")
var list = List(1,52,35,6,8,9)
list.foreach(println)
}
}
Output:
for loop iterating...
1
52
35
6
8
9
Below is another example where we get the sum of all the elements of the list by just passing an expression. We passed the expression to get a sum rather than a print statement.
object MyClass {
def main(args: Array[String]) {
var sum = 0;
print("for loop iterating...\n");
var list = List(1,5,35,6,8,9);
list.foreach(sum += _)
print(sum);
}
}
Output:
for loop iterating...
64
Using the foreach
Function With Vector in Scala
We can use the foreach
function with Vector similarly to traverse the elements. Vector is a sequence in Scala that holds the elements in key and value pairs.
Example:
object MyClass {
def main(args: Array[String]) {
print("foreach loop iterating...\n");
val nums = Vector((1,8), (2,9), (3,7), (4,6), (5,5))
nums.foreach {
case(key, value) => println(s"key: $key, value: $value")
}
}
}
Output:
foreach loop iterating...
key: 1, value: 8
key: 2, value: 9
key: 3, value: 7
key: 4, value: 6
key: 5, value: 5