Updated March 31, 2023
Definition of Scala Yield
Scala yield is used with the sequence comprehensions like foreach, withFilter, map, FlatMap, and filter. Yield is a keyword in scala that is used at the end of the loop. We can perform any operation on the collection elements by using this for instance if we want to increment the value of collection by one. This will return us to the new collection. For loop maintain a buffer to store the resulted elements and when the operations are done it just yield the result from the buffer. It always returns the same type of collection that we are using to iterate for example: if we are iterating over a List it will yield List, If we have Map, It will return Map only, and so on.
Syntax:
var variable_name = for{ var value <- List}
yield value
In the above syntax, we are using for loop and at the end, we are using yield keyword to return the result.
var finalResult = for{ varobj<- List}
yield obj + 1
In this example, we are getting obj after the successful iteration of the List collection. And at the end we are just adding 1 to the collection elements. So for using this we just need to use yield keyword followed by the logic we want to perform.
How does Yield Keyword Work in Scala?
Scala yield is very different from other languages like python and ruby. In the scala, it is used with the comprehensions. Before yield, we were facing one issue to create a new collection by using the existing collection or transforming the elements with the use of the algorithm. So the solution yield comes into the picture. So this yield keyword it creates our algorithm and helps us to create a new collection from the existing one. So we can just able to create a copy of the collection by yielding elements of the collection without the use of any algorithm. See below;
for (r <- list) yield r
So in this case this will yield all the elements from the list collection. We can also perform an operation on the yielding result like we can make all the elements of the collection twice or for this, we need to write our logic after the yield like see below;
for (r <- list) yield r * 2
So here we are multiplying each element with 2 to make it double from the original. It is not only we can use with the Integer, we can use them with String also like at the end of iterating the elements we can convert the elements of the collection into lower case letters or upper case it just depends upon the requirement.
So we are using for loop and yield together and we call this combination sequence comprehensions or comprehensions in scala. So this yield keyword just returns a new collection from the original collection. So we can say that they maintain one temporary bucket to hold the result from the for loop and once the for loop is done iterating all the elements from the collection they just yield the result.
They will always return the same collection on which we are iteration for example if we are using ArrayBuffer so after yielding the result will be ArrayBuffer only and this same apply to all collection.
object Main extends App{
// Your code here!
//cretaing an array
val arr = Array( 10, 50, 40, 60, 80, 90)
// Using for and yield here
var result = for (x <- arr ) yield x
println(result)
}
In this example, we are creating one array of integers. On this array, we will apply the yield keyword with for loop. So in the next line, we are applying for and yield together on this array and holding the result into the ‘result’ variable. This result variable will also an array that will contain all the yielding elements.
So at the last we just printing the result or else we can also apply foreach here to print.
Examples of Scala Yield
This will be a simple program for beginners to define yield with for loop.
Example #1
Code:
object Main extends App{
// Your code here!
//cretaing an array
val arr = Array( 10, 50, 40, 60, 80, 90)
// Using for and yield here
var result = for (x <- arr ) yield x
// printing output
for(r <-result)
{
// result here ''''''
println("yield value is ::")
println(r)
}
}
Output:
Example #2
In this example, we are modifying all the elements of the collection. We are making them twice the original value.
Code:
object Main extends App{
// Your code here!
//cretaing an array
val arr = Array( 10, 50, 40, 60, 80, 90)
println("elements before yield ::::")
for(y <-arr)
{
// result here ''''''
println(y)
}
// Using for and yield here
var result = for (x <- arr ) yield x *2
// printing output
println("elements after yield ::::")
for(r <-result)
{
// result here ''''''
println(r)
}
}
Output:
Example #3
In this example, we are creating a list of sting and converting all the elements of the collection to uppercase by using yield with toUpperCase() method in scala.
Code:
object Main extends App{
// Your code here!
//cretaing an array
val list1 = List( "one", "two", "three", "four", "five", "six", "seven")
println("elements before yield ::::")
for(l1 <-list1)
{
// result here ''''''
println(l1)
}
// Using for and yield here
var result = for (x <- list1 ) yield x.toUpperCase
// printing output
println("elements after yield ::::")
for(r <-result)
{
// result here ''''''
println(r)
}
}
Output:
Advantages
Some of the advantages are:
- It will not modify the original collection
- It will always return than the same type of collection on which we are operating.
- At the end, we can easily modify the collection elements without the use of any algorithm.
Conclusion
Scala yield is used with for loop, for each, map, etc. It is very different from the yield available in other languages. This combination is called as comprehensions in scala. It is used at the end of the loop after the iteration of collection elements.
Recommended Articles
This is a guide to Scala Yield. Here we also discuss the definition and how does yield keyword work in scala? along with different examples and its code implementation. You may also have a look at the following articles to learn more –