Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala convert Iterable or collection.Seq to collection.immutable.Seq

It appears the toSeq method in Scala collections returns a scala.collection.Seq, I could also return a Traversable or Iterable but need to convert this to a scala.collection.immutable.Seq.

Is there an easy way to do this?

Thanks Richard

like image 663
Richard Todd Avatar asked Jun 05 '13 12:06

Richard Todd


People also ask

Is seq immutable in Scala?

A subtrait of collection. Seq which represents sequences that are guaranteed immutable.

Is seq immutable?

Seq is immutable — Once a Seq is created, it cannot be changed, appended to, rearranged or otherwise modified. Instead, any mutative method called on a Seq will return a new Seq .

What is Iterable type in Scala?

Iterable: A base trait for iterable collections. This is a base trait for all Scala collections that define an iterator method to step through one-by-one the collection's elements.

What is collections in Scala?

Scala has a rich set of collection library. Collections are containers of things. Those containers can be sequenced, linear sets of items like List, Tuple, Option, Map, etc. The collections may have an arbitrary number of elements or be bounded to zero or one element (e.g., Option).


1 Answers

Use the to method to convert between arbitrary collection types in Scala 2.10:

scala> Array(1, 2, 3).toSeq res0: Seq[Int] = WrappedArray(1, 2, 3)  scala> Array(1, 2, 3).to[collection.immutable.Seq] res1: scala.collection.immutable.Seq[Int] = Vector(1, 2, 3) 
like image 61
axel22 Avatar answered Sep 23 '22 14:09

axel22