When I evaluate a for in Scala, I get an immutable IndexedSeq (a collection with array-like performance characteristics, such as efficient random access):
scala> val s = for (i <- 0 to 9) yield math.random + i
s: scala.collection.immutable.IndexedSeq[Double] = Vector(0.6127056766832756, 1.7137598183155291, ...
Does a for with a yield always return an IndexedSeq, or can it also return some other type of collection class (a LinearSeq, for example)? If it can also return something else, then what determines the return type, and how can I influence it?
I’m using Scala 2.8.0.RC3.
Thanks michael.kebe for your comment.
This explains how
foris translated to operations withmap,flatMap,filterandforeach. So my example:is translated to something like this (I’m not sure if it’s translated to
maporflatMapin this case):The result type of operations like
mapon collections depends on the collection you call it on. The type of0 to 9is aRange.Inclusive:The result of the
mapoperation on that is anIndexedSeq(because of the builder stuff inside the collections library).So, to answer my question: the result of a
for (...) yield ...depends on what type is inside the parantheses. If I want aListas the result, I could do this: