yield
is mostly used in a for-yield loop to produce a new same-type collection. For example:
scala> val a = Array(2,3,5)
a: Array[Int] = Array(2, 3, 5)
scala> val result = for (elem <- a) yield 2 * elem
result: Array[Int] = Array(4, 6, 10)
This all works fine, the for loop takes an array and returns an array.
But then I noticed this:
scala> 1 to 10
res0: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
This generates a range type collection, but when you use this in conjunction with for-yield loop, this happened:
scala> for (i <- (1 to 10)) yield i + 2
res2: scala.collection.immutable.IndexedSeq[Int] = Vector(3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
Type that comes in is range, but the type it sends out is Vector. Why is this happenning? Am I missing anything?