×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

The yield keyword in Scala | for/yield examples

By IncludeHelp Last updated : October 10, 2024

Scala yield keyword

The yield keyword in Scala is used along with the for loop. It stores a variable at each for loop iteration. The stored variables combine to create a new structure of the same time as the one on which the for loop is working. For example, using the yield on the map will give a map structure similarly for the list, array vectors, etc.

syntax

The syntax of yield is,

for (loop condition) yield variable; 

Example

Example, using yield with for loop that loops from 1 to 5,

for (i <- 1 to 5) yield (i*5)

Output

scala.collection.immutable.IndexedSeq[Int] = Vector(5, 10, 15, 20, 25)

for loop/yield examples Scala array

Iterating elements of an array using for loop and then using the yield keyword to store elements at each iteration.

In this example, we are given an array and using the for loop we can iterate over the elements of the array. A using the yield keyword, we store these elements to another array based on some expression.

Example

object MyClass {
    def main(args: Array[String]) {
        val arr = Array(1, 2, 3, 4, 5)
        
        println("The array is ")
        for(i <- 0 until arr.length)
            printf("    "+ {arr(i)})
        
        var values = for (i <- arr) yield i* 7
        println("\nValues from yield")
        for(e <- values) println(e)
    }
}

Output

The array is 
    1    2    3    4    5
Values from yield
7
14
21
28
35

for loop/yield over Scala list

Iterating elements of a list using the follow and then using the yield keyword to create a separate list that stores the required element.

In this example, we will iterate over a list of elements. And then we will use the yield keyword to create a list of elements that are yielded using some expression.

Example

object MyClass {
    def main(args: Array[String]) {
        val arr = List(12, 21, 36, 42, 57)
        
        println("The List is ")
        for(i <- 0 until arr.length)
            printf("    "+ {arr(i)})
            
        var values = for (i <- arr) yield i/ 3
        println("\nValues from yield")
            for(e <- values) println(e)
    }
}

Output

The List is 
    12    21    36    42    57
Values from yield
4
7
12
14
19

for loop/yield with if condition

You can additionally add a condition( if condition)  that guides over the iterations of a for loop. Before the loop iterates, the condition is checked, based on this condition the loop iteration is executed or not.

In this example, we will iterator over only even elements of the array.

Example

object MyClass {
    def main(args: Array[String]) {
        val arr = List(12, 21, 36, 42, 57)
        
        println("The List is ")
        for(i <- 0 until arr.length)
            printf("    "+ {arr(i)})
            
        var values = for (i <- arr if i%2 == 0) yield i/ 3
        println("\nValues from yield")
        for(e <- values) println(e)
    }
}

Output

The List is 
    12    21    36    42    57
Values from yield
4
12
14

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.