- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Inits and tails
Wed, 2011-05-18, 13:45
Hi,
Just one quick question. Why are the inits and tails methods on
TraversableLike (http://www.scala-lang.org/api/current/
index.html#scala.collection.TraversableLike) returning an Iterator and
not a more specific collection (possibly the same container)?
Thanks,
Eric.
On 5/18/11 5:45 AM, etorreborre wrote:
> Just one quick question. Why are the inits and tails methods on
> TraversableLike (http://www.scala-lang.org/api/current/
> index.html#scala.collection.TraversableLike) returning an Iterator and
> not a more specific collection (possibly the same container)?
I have found in practice that more often than not, deriving a CC[CC[T]]
from a CC[T] is not what I wanted. It is intuitively appealing but the
fact is that just because I have some Foos, doesn't mean I will benefit
from a Foo of Foos. So I went with Iterator because it imposes the
lowest cost on the general case and you can convert it to anything you want.
Another good reason is that eagerly constructing inits and tails
consumes N^2 memory. Try calling tails on a decent sized collection,
then .toWhatever it.