Monday, 24 August 2020

Scala: for loop as expression

You can use ‘for’ loop as either expression or statement. ‘yield’ keyword is used to convert a ‘for’ loop into an expression.

 

Using for loop to iterate over a list of elements

Let's define a list and iterate over it using for loop.

 

Below statement defines a list of integers.

var days = List(1, 3, 5, 7, 2, 4, 6, 10, 23)

 

Below snippet is used to iterate over list elements.

 

for(day <- days){

    day match {

         case 1 => println("Monday")

         case 2 => println("Tuesday")

         case 3 => println("Wednesday")

         case 4 => println("Thursday")

         case 5 => println("Friday")

         case 6 => println("Saturday")

         case 7 => println("Sunday")

         case _ => println("Invalid Data")

    }

 

}

scala> var days = List(1, 3, 5, 7, 2, 4, 6, 10, 23)
var days: List[Int] = List(1, 3, 5, 7, 2, 4, 6, 10, 23)

scala> 

scala> for(day <- days){
     |     day match {
     |          case 1 => println("Monday")
     |          case 2 => println("Tuesday")
     |          case 3 => println("Wednesday")
     |          case 4 => println("Thursday")
     |          case 5 => println("Friday")
     |          case 6 => println("Saturday")
     |          case 7 => println("Sunday")
     |          case _ => println("Invalid Data")
     |     }
     |  
     | }
Monday
Wednesday
Friday
Sunday
Tuesday
Thursday
Saturday
Invalid Data
Invalid Data

This is example is similar to for-each loop in Java.

 

Using for loop as expression

If you use for loop with yield, a collection of return values from each iteration of the loop are returned by for loop. 

Example

val result = for(day <- days) yield {
    day match {
         case 1 => "Monday"
         case 2 => "Tuesday"
         case 3 => "Wednesday"
         case 4 => "Thursday"
         case 5 => "Friday"
         case 6 => "Saturday"
         case 7 => "Sunday"
         case _ => "Invalid Data"
    }
 
}

As you see above snippet, I added yield after for loop and every iteration of loop return a string.

scala> val result = for(day <- days) yield {
     |     day match {
     |          case 1 => "Monday"
     |          case 2 => "Tuesday"
     |          case 3 => "Wednesday"
     |          case 4 => "Thursday"
     |          case 5 => "Friday"
     |          case 6 => "Saturday"
     |          case 7 => "Sunday"
     |          case _ => "Invalid Data"
     |     }
     |  
     | }
     | 
val result: List[String] = List(Monday, Wednesday, Friday, Sunday, Tuesday, Thursday, Saturday, Invalid Data, Invalid Data)

scala> 

scala> print(result)
List(Monday, Wednesday, Friday, Sunday, Tuesday, Thursday, Saturday, Invalid Data, Invalid Data)

Example: Calculate square of integer from 1 to 10 and store in a collection.

var result =  for { i <- 1 to 10 } yield {i * i}

scala> var result =  for { i <- 1 to 10 } yield {i * i}
var result: IndexedSeq[Int] = Vector(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)

scala> 

scala> print(result)
Vector(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)





Previous                                                    Next                                                    Home

No comments:

Post a Comment