Why does TraversableOnce.toSeq return a stream?

Here is an example:

scala> val xs = List(1,2,3).toIterator.toSeq
xs: Seq[Int] = Stream(1, ?)

A sequence is a materialized set (the default is a List), so I expected to toSeqreturn List, notStream

The implementation is done in TraversableOnce,

  def toSeq: Seq[A] = toStream

why is it not redefined in TraversableLike?

+4
source share
1 answer

Scala supports infinite iterators, and Streamis the easiest Seqfor possible infinite data.

Iterator.from(1).toSeq

ends (if only part of the collection is used), but

Iterator.from(1).toList

will never end.

, , . toSeq , , .

"" :

. toIterable, , TraversableOnce . : .

http://www.scala-lang.org/api/current/?_ga=1.200291566.1390086478.1406220121#scala.collection.Iterator

+6

All Articles