| Yield keyword in Scala?
We use Yield keyword as a Loop in scala. Yield keyword will returns a result once the loop iteration is complete. Here for loop used buffer internally stored iterated result and when iterations is finisihed it yields the result from that buffer. And this will not work like imperative loop. Because type of collection which is returned is of same type that we tend to were iterating over, Therefore a Map yields a Map, a List yields a List, and so on. And syntax for Yield keyword is given below
var result = for{ var x <- List }
yield x
In above code we have used curly braces that keep the variable conditions and result is variable where all the values of x are kept within the form of collection.
Below are some silent features of Yield:-
(1)It is used as a Loop.
(2)It produced value for each iteration.
(3)It supports the use of Map, FlatMap, and Filters along with nomads. | | |