Scala 的收益率是多少? [英] What is Scala's yield?

查看:22
本文介绍了Scala 的收益率是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解 Ruby 和 Python 的收益.Scala 的 yield 有什么作用?

I understand Ruby and Python's yield. What does Scala's yield do?

推荐答案

用于序列推导(如 Python 的列表推导式和生成器,您也可以在其中使用 yield).

It is used in sequence comprehensions (like Python's list-comprehensions and generators, where you may use yield too).

它与 for 结合使用,并在结果序列中写入一个新元素.

It is applied in combination with for and writes a new element into the resulting sequence.

简单示例(来自 scala-lang)

/** Turn command line arguments to uppercase */
object Main {
  def main(args: Array[String]) {
    val res = for (a <- args) yield a.toUpperCase
    println("Arguments: " + res.toString)
  }
}

F# 中对应的表达式为

The corresponding expression in F# would be

[ for a in args -> a.toUpperCase ]

from a in args select a.toUpperCase 

在 Linq.

Ruby 的 yield 有不同的效果.

Ruby's yield has a different effect.

这篇关于Scala 的收益率是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆