In 2.10.x
List().to[Vector]
And
List().toVector
In 2.9.x, fastest may be
Vector.empty ++ list
For my purposes, it appears that Vector is usually preferable to List. In the few cases where List might be slightly preferable, the difference is not enough to worry about (for me, anyway). So I'm wondering if Vector is going to be put into predef so I don't have to clutter my code withimport scala.collection.immutable.Vector
That's surprising considering the byte code. How/what did you use to benchmark? I may run my own to validate.
On Wed, Sep 12, 2012 at 9:51 PM, Russ P. <russ.p...@gmail.com> wrote:
> I'm wondering if Vector is going
> to be put into predef so I don't have to clutter my code with
> import scala.collection.immutable.Vector
Eh? It's in Predef already.
I'm sure that's all good information, Alex, but what bothers me is that I should have to remember stuff like that. I would much rather focus on my problem domain than on arbitrary details of what to call a List or how to do a match on a List. That I should need to remember arbitrary details like that to compensate for a design mistake made long ago is not a good sign.
Also, call me sloppy, but my chances of recalling these rules when I actually need them are negligible.
On Thu, Sep 13, 2012 at 5:11 PM, Russ P. <russ.p...@gmail.com> wrote:I'm sure that's all good information, Alex, but what bothers me is that I should have to remember stuff like that. I would much rather focus on my problem domain than on arbitrary details of what to call a List or how to do a match on a List. That I should need to remember arbitrary details like that to compensate for a design mistake made long ago is not a good sign.I wouldn't call the popularity of s.c.i.List a design mistake, it's the classic data structure for functional programming. It just so happens that the data structure that underlies s.c.i.Vector was discovered/invented fairly recently, and turns out to be more broadly applicable, while still retaining many of the classic cons list's attractive qualities.
I shouldn't have called it a design mistake. It's just a slightly unfortunate clash of names across domains. Having been formally trained as an aerospace engineer, I think of a vector as an n x 1 matrix that can be used to represent position, velocity, force, and other physical concepts, such as a state vector. An entire academic field known as "state estimation and control theory" is based on the concept of a state vector. The CS concept of a vector is related but not quite the same because the contents are not necessarily even numbers, and there is no inherent concept of vector addition or vector/matrix multiplication, for example.
My use of Scala collections comes down to mainly Lists, Sets, and Maps. Now I am realizing that Vectors are usually preferable to Lists, but for my purposes they are just an improved form of List. Efficient random access is usually more important to me than a slight difference in iteration efficiency.
Most of my use of Vectors will be for lists rather than for representation of physical vectors. The name List would be more appropriate in most cases, and the use of Vectors for lists creates a slight cognitive dissonance for me, but I'll get over it. Maybe I could come up with some elegant type aliases, but that would probably just confuse readers of my code.
was vector really only invented recently? i mean, basically it's just a tree of lists. i can't really imagine that it hasn't been used before scala 2.9
The former will only match instances of s.c.i.List, and no other kind of collection. This is because "::" and "Nil" are the names of its implementation classes, not special syntax.
The latter pattern match uses an extractor defined on s.c.i.Seq's companion object (or one of its supers), and it's implemented to operate on arbitrary implementations of Seq, including List, Vector, Stream, Array, etc. If you want to see how the extractor works, it's a method called "unapplySeq".
Extractors can be bewildering at first, so don't get discouraged if they don't make sense to you; just think of them as how you customize pattern matching.
Seq is abstract, so this generality of its extractor makes a certain intuitive sense. At least, it's intuitive once you've internalized the basic look and feel of Scala. :)
HTH!
-0xe1a
You may want to profile this. For Vector/List +: may be faster than Seq(...). For List specifically :: should be best.
else Some(t.head -> t.tail)
In 2.10.
That's fair. The byte code I inspected looked fine. One time I used anecdotal evidence and everything was OK.