Scala | yield Keyword Last Updated : 07 Jun, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report yield keyword will returns a result after completing of loop iterations. The for loop used buffer internally to store iterated result and when finishing all iterations it yields the ultimate result from that buffer. It doesn't work like imperative loop. The type of the collection that is returned is the same type that we tend to were iterating over, Therefore a Map yields a Map, a List yields a List, and so on. Syntax: var result = for{ var x <- List} yield x Note: The curly braces have been used to keep the variables and conditions and result is a variable wherever all the values of x are kept within the form of collection. Example: Scala // Scala program to illustrate yield keyword // Creating object object GFG { // Main method def main(args: Array[String]) { // Using yield with for var print = for( i <- 1 to 10) yield i for(j<-print) { // Printing result println(j) } } } Output: 1 2 3 4 5 6 7 8 9 10 In above example, the for loop used with a yield statement actually creates a sequence of list. Example: Scala // Scala program to illustrate yield keyword // Creating object object GFG { // Main method def main(args: Array[String]) { val a = Array( 8, 3, 1, 6, 4, 5) // Using yield with for var print=for (e <- a if e > 4) yield e for(i<-print) { // Printing result println(i) } } } Output: 8 6 5 In above example, the for loop used with a yield statement actually creates a array. Because we said yield e, it’s a Array[n1, n2, n3, ....]. e <- a is our generator and if (e > 4) could be a guard that filters out number those don't seem to be greater than 4. Comment More infoAdvertise with us Next Article Scala | yield Keyword D DivyaPareek Follow Improve Article Tags : Python Scala Practice Tags : python Similar Reads yield Keyword - Python In Python, the yield keyword is used to create generators, which are special types of iterators that allow values to be produced lazily, one at a time, instead of returning them all at once. This makes yield particularly useful for handling large datasets efficiently, as it allows iteration without 5 min read The yield Keyword in Ruby We can send a block to our method and it can call that block multiple times. This can be done by sending a proc/lambda, but is easier and faster with yield. During a method invocation The yield keyword in corporation with a block allows to pass a set of additional instructions. When yield is called 2 min read Scala | unapplySeq() method The unapplySeq() method is an Extractor method. It extracts an Object of particular type and then again reconstructs it into a Sequence of extracted values and the length of this Sequence is not specified at the time of compilation. So, in order to reconstruct an Object that contains a Sequence, you 2 min read Scala Extractors In Scala Extractor is defined as an object which has a method named unapply as one of its part. This method extracts an object and returns back the attributes. This method is also used in Pattern matching and Partial functions. Extractors also explains apply method, which takes the arguments and con 6 min read Scala Map keys() method with example The keys() method is utilized to give an iterator over all the keys of the map. Method Definition: def keys: Iterable[A] Return Type: It returns an iterator over all the keys of the map. Example #1: Scala // Scala program of keys() // method // Creating object object GfG { // Main method def main(ar 1 min read Like