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

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

问题描述

我了解Ruby和Python的收益率。 Scala的收益率是什么?

解决方案

它用于 sequence comprehensions (比如Python的列表解析和生成器,也可以使用 yield )。 p>

它与结合用于,并在结果序列中写入一个新元素。



简单示例(来自 scala-lang

  / **将命令行参数转换为大写* / 
对象Main {
def main(args:Array [字符串]){
val res = for(a < - args)产生a.toUpperCase
println(Arguments:+ res.toString)
}
}

F#中的对应表达式为

  [for a args  - > a.toUpperCase] 



<$ p $在Linq中选择a.toUpperCase

p>

Ruby的 yield 有不同的效果。


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

解决方案

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

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

Simple example (from 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)
  }
}

The corresponding expression in F# would be

[ for a in args -> a.toUpperCase ]

or

from a in args select a.toUpperCase 

in Linq.

Ruby's yield has a different effect.

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

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