scala中yield_Scala中的yield关键字| for / yield示例

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.


    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





评论和讨论

广告:您是博主吗? 加入我们的Blogging论坛


翻译自: https://www.includehelp.com/scala/yield-keyword-in-scala-for-yield-examples.aspx

scala中yield

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值