scala中yield
Scala yield关键字 (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.
Scala中的yield关键字与for循环一起使用。 它在每个for循环迭代中存储一个变量。 存储的变量组合在一起,以创建与for循环在同一时间运行的新结构。 例如,在地图上使用yield会为列表,数组向量等提供类似的地图结构。
The syntax of yield is,
yield的语法是
for (loop condition) yield variable;
Examples, using yield with for loop that loops from 1 to 5,
示例,使用yield与for循环,循环从1到5,
for (i
Output
for loop/yield examples over 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
for loop/yield examples 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
for loop/yield examples 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
for (i
Output
for loop/yield examples over 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
for loop/yield examples 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
for loop/yield examples 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
翻译自: https://www.includehelp.com/scala/yield-keyword-in-scala-for-yield-examples.aspx
scala中yield