Should I use List[A] or Seq[A] or something else?

Viewed 7948

I was writing a class that contained some functional-esque methods. First I wrote them using List as parameters and return types. Then I thought "Hey, you could also use a more generic type!" so I replaced the Lists with Seq, hoping that I could make my stuff faster one day by feeding them something else than lists.

So which general purpose stack-like data-structure shall I write my methods and algorithms for? Is there a general pattern I can stick to? All this is because the methods might need to get optimized in the future in case they will form a bottle-neck.

Update

I'll try to be a bit more precise: Given you know which operations you are using, like reversing, .tail, direct element access, or for comprehensions. Can I choose a type that will force efficiency on those operations?

Update 2

I'm quite aware of the performance of concrete data structures for various tasks. What I'm not aware of is which data structure might appear as a sub-class of some super type.

For example shall I use TraversableOnce or IndexedSeq instead of List or Array? Will it buy me anything?

Additional Question

What is your default List-like data-structure signature? Do you write

def a(b: List[A]): List[A] 

or

def a(b: TraversableOnce[A]): TraversableOnce[A]

Can you explain why?

3 Answers
Related