What is Scala's yield?

functional-programming, scala, yield

Solution

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.

Problem

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

Original source

Related problems