page contents

yield如何工作?

轩辕小不懂 发布于 2021-09-07 15:02
阅读 515
收藏 0
分类:资源下载
1878
Nen
Nen
- 程序员

yield用于循环迭代中生成新值,yield是comprehensions的一部分,是多个操作(foreach, map, flatMap, filter or withFilter)的composition语法糖。(深入了解请参考问题Q14)

代码示例:

// <-表示循环遍历

scala> for (i <- 1 to 5) yield i * 2 

res0: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10)

请先 登录 后评论