我理解Ruby和Python的优点。Scala的yield是做什么的?


当前回答

为了理解,考虑以下内容

val A = for (i <- Int.MinValue to Int.MaxValue; if i > 3) yield i

大声读出来可能会有帮助

对于每一个整数i,如果它大于3,那么yield (produce) i并将其添加到列表a中。

在数学集合构建符号方面,上面的for-comprehension类似于

这可以理解为

"对于每一个整数,如果它大于,那么它就是集合中的成员"

或者作为替代

"是所有整数的集合,每个都大于"

其他回答

val doubledNums = for (n <- nums) yield n * 2
val ucNames = for (name <- names) yield name.capitalize

注意,这两个for表达式都使用了yield关键字:

在for之后使用yield是“秘密武器”,它表示“我想从我在for表达式中迭代的现有集合中生成一个新的集合,使用所示的算法。”

从这里开始

它用于序列推导式(就像Python的列表推导式和生成器一样,在这里你也可以使用yield)。

它与for结合应用,并将一个新元素写入结果序列。

简单示例(来自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#中对应的表达式为

[ for a in args -> a.toUpperCase ]

or

from a in args select a.toUpperCase 

在Linq中。

Ruby的产量有不同的影响。

Yield比map()更灵活,参见下面的示例

val aList = List( 1,2,3,4,5 )

val res3 = for ( al <- aList if al > 3 ) yield al + 1 
val res4 = aList.map( _+ 1 > 3 ) 

println( res3 )
println( res4 )

yield将像这样输出结果:List(5,6),这很好

而map()将返回如下结果:List(false, false, true, true, true),这可能不是你想要的。

val aList = List( 1,2,3,4,5 )

val res3 = for ( al <- aList if al > 3 ) yield al + 1
val res4 = aList.filter(_ > 3).map(_ + 1)

println( res3 )
println( res4 )

这两段代码是等价的。

val res3 = for (al <- aList) yield al + 1 > 3
val res4 = aList.map( _+ 1 > 3 )

println( res3 ) 
println( res4 )

这两段代码也是等价的。

Map和yield一样灵活,反之亦然。

为了理解,考虑以下内容

val A = for (i <- Int.MinValue to Int.MaxValue; if i > 3) yield i

大声读出来可能会有帮助

对于每一个整数i,如果它大于3,那么yield (produce) i并将其添加到列表a中。

在数学集合构建符号方面,上面的for-comprehension类似于

这可以理解为

"对于每一个整数,如果它大于,那么它就是集合中的成员"

或者作为替代

"是所有整数的集合,每个都大于"