Seq, LinearSeq, and IndexedSeq

A sequence has some differences from the usual Iterable, and it has a defined length and order. Seq has two sub-traits such as LinearSeq and IndexedSeq. Let's have a quick overview on them.

LinearSeq is a base trait for linear sequences. Linear sequences have reasonably efficient head, tail, and isEmpty methods. If these methods provide the fastest way to traverse the collection, a collection Coll that extends this trait should also extend LinearSeqOptimized[A, Coll[A]]. LinearSeq has three concrete methods:

  • isEmpty: This checks if the list is empty or not
  • head: This returns the first element in the list/sequence
  • tail: This returns all the elements of the list but the first one. Each of the subcollections that inherit the LinearSeq will have its own implementation of these methods to ensure good performance. Two collections that inherit/extend are streams and lists.

Finally, IndexedSeq has two methods that it's defined in terms of them:

  • Apply: This finds elements by index.
  • length: This returns the length of the sequence. Finding an element by its index requires well performing implementation by the subcollections. Two of these indexed sequences are Vector and ArrayBuffer.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.36.72