#### [SOLVED] What is Scala's yield?

By Geo

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

Yield is similar to for loop which has a buffer that we cannot see and for each increment, it keeps adding next item to the buffer. When the for loop finishes running, it would return the collection of all the yielded values. Yield can be used as simple arithmetic operators or even in combination with arrays. Here are two simple examples for your better understanding

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

res: scala.collection.immutable.IndexedSeq[Int] = Vector(3, 6, 9, 12, 15)

scala> val nums = Seq(1,2,3)
nums: Seq[Int] = List(1, 2, 3)

scala> val letters = Seq('a', 'b', 'c')
letters: Seq[Char] = List(a, b, c)

scala> val res = for {
|     n <- nums
|     c <- letters
| } yield (n, c)

res: Seq[(Int, Char)] = List((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c))

Hope this helps!!

#### @jwvh 2018-08-12 14:24:33

I thought clarifying the doubt is important and not to give the different answer since even I'm also a beginner who is learning this language. Thanks for the suggestion.

#### @Daniel C. Sobral 2009-06-29 17:33:01

I think the accepted answer is great, but it seems many people have failed to grasp some fundamental points.

First, Scala's for comprehensions are equivalent to Haskell's do notation, and it is nothing more than a syntactic sugar for composition of multiple monadic operations. As this statement will most likely not help anyone who needs help, let's try again… :-)

Scala's for comprehensions is syntactic sugar for composition of multiple operations with map, flatMap and filter. Or foreach. Scala actually translates a for-expression into calls to those methods, so any class providing them, or a subset of them, can be used with for comprehensions.

First, let's talk about the translations. There are very simple rules:

1. This

for(x <- c1; y <- c2; z <-c3) {...}

is translated into

c1.foreach(x => c2.foreach(y => c3.foreach(z => {...})))

2. This

for(x <- c1; y <- c2; z <- c3) yield {...}

is translated into

c1.flatMap(x => c2.flatMap(y => c3.map(z => {...})))

3. This

for(x <- c; if cond) yield {...}

is translated on Scala 2.7 into

c.filter(x => cond).map(x => {...})

or, on Scala 2.8, into

c.withFilter(x => cond).map(x => {...})

with a fallback into the former if method withFilter is not available but filter is. Please see the section below for more information on this.

4. This

for(x <- c; y = ...) yield {...}

is translated into

c.map(x => (x, ...)).map((x,y) => {...})

When you look at very simple for comprehensions, the map/foreach alternatives look, indeed, better. Once you start composing them, though, you can easily get lost in parenthesis and nesting levels. When that happens, for comprehensions are usually much clearer.

I'll show one simple example, and intentionally omit any explanation. You can decide which syntax was easier to understand.

l.flatMap(sl => sl.filter(el => el > 0).map(el => el.toString.length))

or

for {
sl <- l
el <- sl
if el > 0
} yield el.toString.length

### withFilter

Scala 2.8 introduced a method called withFilter, whose main difference is that, instead of returning a new, filtered, collection, it filters on-demand. The filter method has its behavior defined based on the strictness of the collection. To understand this better, let's take a look at some Scala 2.7 with List (strict) and Stream (non-strict):

scala> var found = false
found: Boolean = false

scala> List.range(1,10).filter(_ % 2 == 1 && !found).foreach(x => if (x == 5) found = true else println(x))
1
3
7
9

scala> found = false
found: Boolean = false

scala> Stream.range(1,10).filter(_ % 2 == 1 && !found).foreach(x => if (x == 5) found = true else println(x))
1
3

The difference happens because filter is immediately applied with List, returning a list of odds -- since found is false. Only then foreach is executed, but, by this time, changing found is meaningless, as filter has already executed.

In the case of Stream, the condition is not immediatelly applied. Instead, as each element is requested by foreach, filter tests the condition, which enables foreach to influence it through found. Just to make it clear, here is the equivalent for-comprehension code:

for (x <- List.range(1, 10); if x % 2 == 1 && !found)
if (x == 5) found = true else println(x)

for (x <- Stream.range(1, 10); if x % 2 == 1 && !found)
if (x == 5) found = true else println(x)

This caused many problems, because people expected the if to be considered on-demand, instead of being applied to the whole collection beforehand.

Scala 2.8 introduced withFilter, which is always non-strict, no matter the strictness of the collection. The following example shows List with both methods on Scala 2.8:

scala> var found = false
found: Boolean = false

scala> List.range(1,10).filter(_ % 2 == 1 && !found).foreach(x => if (x == 5) found = true else println(x))
1
3
7
9

scala> found = false
found: Boolean = false

scala> List.range(1,10).withFilter(_ % 2 == 1 && !found).foreach(x => if (x == 5) found = true else println(x))
1
3

This produces the result most people expect, without changing how filter behaves. As a side note, Range was changed from non-strict to strict between Scala 2.7 and Scala 2.8.

#### @Eastsun 2010-01-26 01:15:41

There is a new method withFilter in scala 2.8. for(x <- c; if cond) yield {...} is translate to c.withFilter(x => cond).map(x => {...}) in scala2.8.

#### @Daniel C. Sobral 2010-01-26 12:44:16

@Eastsun True enough, though there's also automatic fallback. withFilter is supposed to be non-strict as well, even for strict collections, which deserves some explanation. I'll consider this...

#### @Ralph 2011-10-12 11:20:18

@Daniel: There is a great treatment of this very subject in "Programming in Scala", by Odersky, et al. (I'm sure you know that already). +1 for showing it.

#### @Dominik 2016-05-23 14:49:04

The first 2 points are correct with: 1. for(x <- c; y <- x; z <-y) {...} is translated into c.foreach(x => x.foreach(y => y.foreach(z => {...}))) 2. for(x <- c; y <- x; z <- y) yield {...} is translated into c.flatMap(x => x.flatMap(y => y.map(z => {...})))

#### @prostynick 2016-06-25 15:46:34

Is this for(x <- c; y = ...) yield {...} really translated into c.map(x => (x, ...)).map((x,y) => {...})? I think it's translated into c.map(x => (x, ...)).map(x => { ...use x._1 and x._2 here...}) or I'm missing something?

#### @Daniel C. Sobral 2018-07-30 16:26:57

@andrew-marshall Great edit! I never thought of using eight spaces to align code sections on lists. You did miss a double quote. ;)

#### @Mario Galic 2015-12-23 22:23:44

Consider the following for-comprehension

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

"For each integer i, if it is greater than 3, then yield (produce) i and add it to the list A."

In terms of mathematical set-builder notation, the above for-comprehension is analogous to

$A = \left \{ i \in \mathbb{Z} : i>3 \right \}$

"For each integer $i$, if it is greater than $3$, then it is a member of the set $A$."

or alternatively as

"$A$ is the set of all integers $i$, such that each $i$ is greater than $3$."

#### @Daniel Earwicker 2009-06-27 09:52:58

Unless you get a better answer from a Scala user (which I'm not), here's my understanding.

It only appears as part of an expression beginning with for, which states how to generate a new list from an existing list.

Something like:

var doubled = for (n <- original) yield n * 2

So there's one output item for each input (although I believe there's a way of dropping duplicates).

This is quite different from the "imperative continuations" enabled by yield in other languages, where it provides a way to generate a list of any length, from some imperative code with almost any structure.

(If you're familiar with C#, it's closer to LINQ's select operator than it is to yield return).

#### @Russel Yang 2013-04-02 21:21:04

it should be "var doubled = for (n<- original) yield n*2".

#### @dotnetN00b 2013-10-25 23:59:31

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 )

These two pieces of code are equivalent.

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

println( res3 )
println( res4 )

These two pieces of code are also equivalent.

Map is as flexible as yield and vice-versa.

#### @Michael Peng 2013-10-10 18:24:46

yield is more flexible than map(), see example below

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 will print result like: List(5, 6), which is good

while map() will return result like: List(false, false, true, true, true), which probably is not what you intend.

#### @dotnetN00b 2013-10-25 23:52:18

That comparison is wrong. You are comparing two different things. The expression in yield is in no way doing the same thing as the expression in map. Also, it does not show the "flexibility" of yield compared to map at all.

#### @philix 2013-11-16 14:21:37

yield al + 1 > 3 would do the same trick

#### @Richard Gomes 2012-07-07 12:27:09

The keyword yield in Scala is simply syntactic sugar which can be easily replaced by a map, as Daniel Sobral already explained in detail.

On the other hand, yield is absolutely misleading if you are looking for generators (or continuations) similar to those in Python. See this SO thread for more information: What is the preferred way to implement 'yield' in Scala?

#### @Alexey Romanov 2009-06-27 12:31:35

Yes, as Earwicker said, it's pretty much the equivalent to LINQ's select and has very little to do with Ruby's and Python's yield. Basically, where in C# you would write

from ... select ???

for ... yield ???

It's also important to understand that for-comprehensions don't just work with sequences, but with any type which defines certain methods, just like LINQ:

• If your type defines just map, it allows for-expressions consisting of a single generator.
• If it defines flatMap as well as map, it allows for-expressions consisting of several generators.
• If it defines foreach, it allows for-loops without yield (both with single and multiple generators).
• If it defines filter, it allows for-filter expressions starting with an if in the for expression.

#### @Eldritch Conundrum 2012-02-25 17:09:42

in C#'s LINQ, the correct order is "from ... select ???"

#### @Jordan Parmer 2014-01-15 20:24:40

@Eldritch Conundrum - Which interestingly enough is the same order in which the original SQL spec outlines. Somewhere along the way the SQL language inverted the order, but it makes complete sense to first describe what you are pulling from followed by what you expect to get out of it.

#### @Dario 2009-06-27 09:42:19

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.

#### @Geo 2009-06-27 12:23:53

So why would I use yield instead of map? This map code is equivalent val res = args.map(_.toUpperCase) , right?

#### @Nathan Shively-Sanders 2009-06-27 12:44:01

In case you like the syntax better. Also, as alexey points out, comprehensions also provide nice syntax for accessing flatMap, filter and foreach.

#### @oxbow_lakes 2009-06-27 13:24:55

I would much prefer the args map {_ toUpperCase} syntax personally as it "feels" much more OO. It seems more in keeping with the Scala aim of delivering via library support what is only available in other languages via bespoke constructs and keywords

#### @Alexey Romanov 2009-06-27 13:35:01

Right. If you just have a simple map -- one generator with no if -- I'd certainly say calling map is more readable. If you have a several generators depending on each other, and/or filters, you may prefer a for expression.

#### @Daniel C. Sobral 2009-06-29 17:03:46

Please note that the example given is not equivalent to the map expression: it's the same. A for comprehension is translated to calls to map, flatMap and filter.

#### @Jean 2011-07-26 07:26:43

I think I got it:val unorderedGuesses = guessesForSymbol.map { case (symbol, guesses) => secretsForSymbol.getOrElse(symbol,0) min(guesses) } not sure it brings much to the table

#### @thSoft 2011-12-09 15:01:36

This is true in this case, but it is actually a more general construct, see the most upvoted answer (@Tempus IMHO you should consider accepting it).

#### @Richard Gomes 2012-06-30 14:10:49

The answer starts like this: "It is used in sequence comprehensions (like Python's list-comprehensions and generators, where you may use yield too)." This mistakenly leads one to think that yield in Scala is similar to yield in Python. This is not the case. In Python, yield is used in the context of coroutines (or continuations) whilst it is not the case in Scala. For more clarification, please visit this thread: stackoverflow.com/questions/2201882/…

#### @devssh 2018-07-23 11:39:57

As I was learning scala, I didn't understand why a flatMap which maps and then flattens was necessary, but given time I am unable to go on without the convenience it provides. Just try executing List(List(1, 2), List(3,4,5), List(6)).flatMap(identity) which will show the amazing map reduce or group by this provides. Yield expands for to either flatMap or withFilter (cond) map(func) and no yield expands for to foreach

### [SOLVED] What is the yield keyword used for in C#?

• 2008-09-02 13:15:24
• Herms
• 293189 View
• 767 Score
• Tags:   c# yield

### [SOLVED] In practice, what are the main uses for the new "yield from" syntax in Python 3.3?

• 2012-03-14 19:33:41
• Paulo Scardine
• 90182 View
• 342 Score
• Tags:   python yield

### [SOLVED] What do all of Scala's symbolic operators mean?

• 2011-10-25 12:00:11
• 0__
• 106167 View
• 391 Score
• Tags:   scala operators

### [SOLVED] What does "yield break;" do in C#?

• 2008-10-23 23:07:02
• skb
• 129253 View
• 466 Score