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* 7println("\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/ 3println("\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/ 3println("\nValues from yield")for(e <- values) println(e)}
}

Output



TOP Interview Coding Problems/Challenges

  • Run-length encoding (find/print frequency of letters in a string)

  • Sort an array of 0's, 1's and 2's in linear time complexity

  • Checking Anagrams (check whether two string is anagrams or not)

  • Relative sorting algorithm

  • Finding subarray with given sum

  • Find the level in a binary tree with given sum K

  • Check whether a Binary Tree is BST (Binary Search Tree) or not

  • 1[0]1 Pattern Count

  • Capitalize first and last letter of each word in a line

  • Print vertical sum of a binary tree

  • Print Boundary Sum of a Binary Tree

  • Reverse a single linked list

  • Greedy Strategy to solve major algorithm problems

  • Job sequencing problem

  • Root to leaf Path Sum

  • Exit Point in a Matrix

  • Find length of loop in a linked list

  • Toppers of Class

  • Print All Nodes that don't have Sibling

  • Transform to Sum Tree

  • Shortest Source to Destination Path



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 * 7println ( "\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 / 3println ( "\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 / 3println ( "\nValues from yield" )for ( e <- values ) println ( e )}
}

Output



TOP Interview Coding Problems/Challenges

  • Run-length encoding (find/print frequency of letters in a string)

  • Sort an array of 0's, 1's and 2's in linear time complexity

  • Checking Anagrams (check whether two string is anagrams or not)

  • Relative sorting algorithm

  • Finding subarray with given sum

  • Find the level in a binary tree with given sum K

  • Check whether a Binary Tree is BST (Binary Search Tree) or not

  • 1[0]1 Pattern Count

  • Capitalize first and last letter of each word in a line

  • Print vertical sum of a binary tree

  • Print Boundary Sum of a Binary Tree

  • Reverse a single linked list

  • Greedy Strategy to solve major algorithm problems

  • Job sequencing problem

  • Root to leaf Path Sum

  • Exit Point in a Matrix

  • Find length of loop in a linked list

  • Toppers of Class

  • Print All Nodes that don't have Sibling

  • Transform to Sum Tree

  • Shortest Source to Destination Path



评论和讨论

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


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

scala中yield


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部