Scala Arrays vs IndexedSeq

1,575 views
Skip to first unread message

Edmondo Porcu

unread,
Sep 30, 2011, 8:09:33 AM9/30/11
to scala-user
Dear members,
can you please help me and other newbies in understanding the difference ?:))

Best Regards
Edmondo

Dennis Haupt

unread,
Sep 30, 2011, 9:49:59 AM9/30/11
to Edmondo Porcu, scala...@googlegroups.com
arrays are stupid java arrays, indexedsequences are real classes

-------- Original-Nachricht --------
> Datum: Fri, 30 Sep 2011 14:09:33 +0200
> Von: Edmondo Porcu <edmond...@gmail.com>
> An: scala-user <scala...@googlegroups.com>
> Betreff: [scala-user] Scala Arrays vs IndexedSeq

Daniel Sobral

unread,
Sep 30, 2011, 9:58:36 AM9/30/11
to Edmondo Porcu, scala-user
On Fri, Sep 30, 2011 at 09:09, Edmondo Porcu <edmond...@gmail.com> wrote:
> Dear members,
> can you please help me and other newbies in understanding the difference
> ?:))

A "Scala Array" is a Java Array. It is a mutable data structure which
does not belong to any collection at all -- neither in Scala, nor in
Java. Scala adds some methods to it through an implicit conversion,
but it doesn't change it's basic characteristics.

Of particular note, an Array is the only parameterized data structure
on JVM which does not suffer from boxing, which provides it superior
performance in many ways.

IndexedSeq, on the other hand, is a trait in Scala extended by
collections which provide effective constant time indexed access to
their elements, and whose elements have their positions (their
indices) deterministically defined. There are generic, mutable and
immutable versions of this trait, and an implicit conversion from
Array to it.


--
Daniel C. Sobral

I travel to the future all the time.

Dennis Haupt

unread,
Sep 30, 2011, 10:19:05 AM9/30/11
to Daniel Sobral, edmond...@gmail.com, scala...@googlegroups.com
which is what i said :)

-------- Original-Nachricht --------
> Datum: Fri, 30 Sep 2011 10:58:36 -0300
> Von: Daniel Sobral <dcso...@gmail.com>
> An: Edmondo Porcu <edmond...@gmail.com>
> CC: scala-user <scala...@googlegroups.com>
> Betreff: Re: [scala-user] Scala Arrays vs IndexedSeq

Rex Kerr

unread,
Sep 30, 2011, 10:30:36 AM9/30/11
to Edmondo Porcu, scala-user
Since you also asked about performance:

Arrays are Java arrays.  They are unwrapped and just as fast using primitive types.

IndexedSeq is a superclass of any implementation of a collection that looks array-like in that you can index into it reasonably efficiently.  (IndexedSeqOptimized guarantees that indexing into each element in turn will be at least as fast as iterating over the collection; IndexedSeq should have pretty fast indexing, but indexing might be slower.)  Since it is part of the collections hierarchy, it uses generics, and therefore has to box primitives (one extra object for each primitive value, at least for things like ints and floats where you can't just store every possibility).  This makes it much slower than arrays for primitive operations, but much more powerful.  (For something like Strings, it is much more powerful _and_ at worst only marginally slower!)

So, basically, high-performance code operating on primitives should use arrays.  Scala has implicit conversions that can make arrays look like IndexedSeqs, but you may as well pick an IndexedSeq that you like and use that instead to gain other benefits (resizability, e.g. ArrayBuffer; or immutability, e.g. Vector).

  --Rex
Reply all
Reply to author
Forward
0 new messages