Let us explore Sequences as part of scala collections
Here is the official documentation link – http://docs.scala-lang.org/overviews/collections/introduction
- Sequence have length
- Elements in Sequence can be accessed using prefix
- eg: scala.Array, scala.collection.immutable.List etc
- Classes are divided into Seq classes and Buffer classes
- Vector and List are considered as Seq classes and they are more frequently used
- Array is special type of collection analogous to Java Array
- We will get into the details of Array and List
- Array is mutable while List is immutable
- Seq have 2 sub traits – IndexedSeq and LinearSeq
- Classes such as Array, Vector implement IndexedSeq and classes such as List implement LinearSeq
- Accessing elements in Array (IndexedSeq) is faster while adding or deleting elements before or end of the collection is faster in List (LinearSeq)
- We will cover Buffer classes later, as and when it is necessary
Operations in Class Seq
WHAT IT IS | WHAT IT DOES |
---|---|
Indexing and Length: | |
xs(i) |
(or, written out, xs apply i ). The element of xs at index i . |
xs isDefinedAt i |
Tests whether i is contained in xs.indices . |
xs.length |
The length of the sequence (same as size ). |
xs lengthCompare n |
Returns -1 if xs is shorter than n , +1 if it is longer, and 0 if it is of length n . Works even if the sequence is infinite, for example Stream.from(1) lengthCompare 42 equals +1 . |
xs.indices |
The index range of xs , extending from 0 to xs.length - 1 . |
Index Search: | |
xs indexOf x |
The index of the first element in xs equal to x (several variants exist). |
xs lastIndexOf x |
The index of the last element in xs equal to x (several variants exist). |
xs indexOfSlice ys |
The first index of xs such that successive elements starting from that index form the sequence ys . |
xs lastIndexOfSlice ys |
The last index of xs such that successive elements starting from that index form the sequence ys . |
xs indexWhere p |
The index of the first element in xs that satisfies p (several variants exist). |
xs segmentLength (p, i) |
The length of the longest uninterrupted segment of elements in xs , starting with xs(i) , that all satisfy the predicate p . |
xs prefixLength p |
The length of the longest prefix of elements in xs that all satisfy the predicate p . |
Additions: | |
x +: xs |
A new sequence that consists of x prepended to xs . |
xs :+ x |
A new sequence that consists of x appended to xs . |
xs padTo (len, x) |
The sequence resulting from appending the value x to xs until length len is reached. |
Updates: | |
xs patch (i, ys, r) |
The sequence resulting from replacing r elements of xs starting with i by the patch ys . |
xs updated (i, x) |
A copy of xs with the element at index i replaced by x . |
xs(i) = x |
(or, written out, xs.update(i, x) , only available for mutable.Seq s). Changes the element of xs at index i to x . |
Sorting: | |
xs.sorted |
A new sequence obtained by sorting the elements of xs using the standard ordering of the element type of xs . |
xs sortWith lt |
A new sequence obtained by sorting the elements of xs using lt as comparison operation. |
xs sortBy f |
A new sequence obtained by sorting the elements of xs . Comparison between two elements proceeds by mapping the function f over both and comparing the results. |
Reversals: | |
xs.reverse |
A sequence with the elements of xs in reverse order. |
xs.reverseIterator |
An iterator yielding all the elements of xs in reverse order. |
xs reverseMap f |
A sequence obtained by mapping f over the elements of xs in reverse order. |
Comparisons: | |
xs startsWith ys |
Tests whether xs starts with sequence ys (several variants exist). |
xs endsWith ys |
Tests whether xs ends with sequence ys (several variants exist). |
xs contains x |
Tests whether xs has an element equal to x . |
xs containsSlice ys |
Tests whether xs has a contiguous subsequence equal to ys . |
(xs corresponds ys)(p) |
Tests whether corresponding elements of xs and ys satisfy the binary predicate p . |
Multiset Operations: | |
xs intersect ys |
The multi-set intersection of sequences xs and ys that preserves the order of elements in xs . |
xs diff ys |
The multi-set difference of sequences xs and ys that preserves the order of elements in xs . |
xs union ys |
Multiset union; same as xs ++ ys . |
xs.distinct |
A subsequence of xs that contains no duplicated element. |
Start the discussion at discuss.itversity.com