This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Inits and tails

1 reply
etorreborre 2
Joined: 2011-02-23,
User offline. Last seen 42 years 45 weeks ago.

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.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Inits and tails

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.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland