tags:

views:

2843

answers:

5

There are several ways to construct an immutable list in scala (see contrived example code below). You can use a mutable ListBuffer, create a var list and modify it, use a tail recursive method, and probably others that I don't know about.

Instinctively, I use the ListBuffer but don't have a good reason for doing so. Is there a preferred or idiomatic method for creating a list, or are there situations that are best for one method over another?

import scala.collection.mutable.ListBuffer

// THESE are all the same as: 0 to 3 toList.
def listTestA() ={
    var list:List[Int] = Nil

    for(i <- 0 to 3) list = list ::: List(i)

    list
}


def listTestB()={
    val list = new ListBuffer[Int]()

    for (i <- 0 to 3) list += i

    list.toList
}


def listTestC() ={

    def _add(l:List[Int], i:Int):List[Int] = i match {
     case 3 => l ::: List(3)
     case _ => _add(l ::: List(i), i +1)
    }
    _add(Nil, 0)

}
+10  A: 

ListBuffer is a mutable list which has constant-time append, and constant-time conversion into a List.

List is immutable and has constant-time prepend and linear-time append.

How you construct your list depends on the algorithm you'll use the list for and the order in which you get the elements to create it.

For instance, if you get the elements in the opposite order of when they are going to be used, then you can just use a List and do prepends. Whether you'll do so with a tail-recursive function, foldLeft, or something else is not really relevant.

If you get the elements in the same order you use them, then a ListBuffer is most likely a preferable choice, if performance is critical.

But, if you are not on a critical path and the input is low enough, you can always reverse the list later, or just foldRight, or reverse the input, which is linear-time.

What you DON'T do is use a List and append to it. This will give you much worse performance than just prepending and reversing at the end.

Daniel
+2  A: 

Uhmm.. these seem too complex to me. May I propose

def listTestD = (0 to 3).toList

or

def listTestE = for (i <- (0 to 3).toList) yield i
Alexander Azarov
Thanks for the answer, but the question is what do you do in the non-trivial case. I put a comment in the code explaining they were all equivalent to 0 to 3 toList.
agilefall
Oops, sorry then! Frankly, I never use ListBuffer.
Alexander Azarov
A: 

I always prefer List and I use "fold/reduce" before "for comprehension". However, "for comprehension" is preferred if nested "folds" are required. Recursion is the last resort if I can not accomplish the task using "fold/reduce/for".

so for your example, I will do:

((0 to 3) :\ List[Int]())(_ :: _)

before I do:

(for (x <- 0 to 3) yield x).toList

Note: I use "foldRight(:\)" instead of "foldLeft(/:)" here because of the order of "_"s. For a version that does not throw StackOverflowException, use "foldLeft" instead.

Walter Chang
I strongly disagree; your preferred form just looks like line noise.
Matt R
Well, all I can say is if you stick with Scala and functional programming long enough, you will learn to love it.
Walter Chang
Will I? I first learned Haskell in 1999, and have been dabbling in Scala for a couple of years. I think folds are great, but if applying a fold in any given situation requires writing a cryptic string of punctuation symbols, I'd consider a different approach.
Matt R
@Matt R: I agree. There is such a thing as overdoing it, and this is one of them.
ryeguy
+2  A: 

And for simple cases:

val list = List(1,2,3)

:)

Don't forget the cons operator! 1 :: 2 :: 3 :: Nil
André Laszlo
Or should I say "operator"?
André Laszlo
+1  A: 

The Scala collection classes are going to be redesigned as of Scala 2.8, so be prepared to change the way you create lists very soon.

What is the forward compatible way of creating a List? I have no idea since I haven't read the 2.8 docs yet.

A PDF document describing the proposed changes of the collection classes

André Laszlo
Most of the changes are in the way things are implemented internally, and in advanced things like projections. How you create a list isn't affected.
Marcus Downing
Ok, that's good to know. You will also get affected if you use any class in the collection.jcl package.
André Laszlo