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.
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?
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?
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?
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.
Scala's sequence is equivalent to Java's List , while Scala's list is equivalent to Java's LinkedList . Both collections can store information, yet the sequence has a few extra highlights over the list . In Scala, a list is a particular collection that is streamlined and regularly utilized in functional programming.
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.
List is a singly linked list, while Vector is a base-32 integer trie, i.e. it is a kind of search tree with nodes of degree 32.
List
is the default implementation of LinearSeq
, which in turn is the default implementation of Seq
, which in turn is the default implementation of Iterable
, which in turn is the default implementation of Traversable
.
See the diagrams here and choose the most general type as per your requirements.
This document might also help.
I think, in general, you should use Seq
for your parameters and design your methods to work efficiently with List
. This way your methods will work ok with most Seq
implementations and you will not have to convert your seqs prior to use your methods.
Edit
Your question has many questions inside.
List
. It's a stack and it's very fastIndexedSeq
scaladoc says "Indexed sequences support constant-time or near constant-time element access and length computation". If you have an IndexedSeq
parameter and someone passes an IndexedSeq
implementation that does not have "near-constant time element access", then that someone is breaking the contract and it's not your problem. If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With