Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala - What is the difference between size and length of a Seq?

What is the difference between size and length of a Seq? When to use one and when the other?

scala> var a :Seq[String] = Seq("one", "two") a: Seq[String] = List(one, two)  scala> a.size res6: Int = 2  scala> a.length res7: Int = 2 

It's the same?

Thanks

like image 278
YoBre Avatar asked Apr 09 '14 15:04

YoBre


People also ask

What does Seq mean in Scala?

Advertisements. Scala Seq is a trait to represent immutable sequences. This structure provides index based access and various utility methods to find elements, their occurences and subsequences. A Seq maintains the insertion order.

What is the difference between Seq and list in Scala?

A Seq is an Iterable that has a defined order of elements. Sequences provide a method apply() for indexing, ranging from 0 up to the length of the sequence. Seq has many subclasses including Queue, Range, List, Stack, and LinkedList. A List is a Seq that is implemented as an immutable linked list.

Is Seq ordered in Scala?

Definitions: Sequence in Scala is a collection that stores elements in a fixed order.

Does Scala Seq preserve order?

It depends on the collection you were using in the first place. If you had a list you'll get your order. If on the other hand you had a set, then probably not.


2 Answers

Nothing. In the Seq doc, at the size method it is clearly stated: "The size of this sequence, equivalent to length.".

like image 177
Peter Avatar answered Sep 28 '22 08:09

Peter


size is defined in GenTraversableOnce, whereas length is defined in GenSeqLike, so length only exists for Seqs, whereas size exists for all Traversables. For Seqs, however, as was already pointed out, size simply delegates to length (which probably means that, after inlining, you will get identical bytecode).

like image 24
Jörg W Mittag Avatar answered Sep 28 '22 08:09

Jörg W Mittag