Flashcards in Seq Collections Deck (16):
s = Seq(1, 2, 3)
For sequences, apply is positional indexing, where elements are always numbered from 0. That is, Seq(1, 2, 3)(1) gives 2.
For a Seq, the apply operation means indexing; hence a sequence of type Seq[T] is a partial function that takes an Int argument (an index) and which yields a sequence element of type T.
s = Set('a', 'b', 'c')
For sets, apply is a membership test. For instance, Set('a', 'b', 'c')('b') gives true whereas Set()('a') gives false.
m = Map('a' -> 1, 'b' -> 10, 'c' -> 100)
For maps, apply is a selection. For instance, Map('a' -> 1, 'b' -> 10, 'c' -> 100)('b') gives 10.
What is a sequence?
A sequence is a kind of iterable that has a length and whose elements have fixed index positions, starting from 0.
The lengthCompare method allows you to compare the lengths of a sequences with an Int even if the sequences has infinite length.
Sequence Index Search Operations
indexOf, lastIndexOf, indexOfSlice, lastIndexOfSlice, indexWhere, lastIndexWhere, segmentLength, prefixLength, which return the index of an element equal to a given value or matching some predicate.
Sequence Addition operations +:, :+, padTo
return new sequences obtained by adding elements at the front or the end of a sequence.
Sequence Update operations updated,patch
return a new sequence obtained by replacing some elements of the original sequence.
Sequence Sorting operations sorted, sortWith, sortBy
sort sequence elements according to various criteria.
Sequence Reversal operations reverse, reverseIterator, reverseMap
yield or process sequence elements in reverse order.
Sequence Comparisons startsWith, endsWith, contains, containsSlice, corresponds
Relate two sequences or search an element in a sequence.
Sequence Multiset operations intersect, diff, union, distinct
Perform set-like operations on the elements of two sequences or remove duplicates.
Difference between update() and updated(). (For Seq)
update() changes a sequence element in place, and is only available for mutable sequences. updated() is available for all sequences and always returns a new sequence instead of modifying the original.
Subclasses of Seq
Immutable: List, Stream, Queue, Stack, Vector, NumericRange, String
Mutable: List, Stream, Queue, Stack, Vector, NumericRange, String, PriorityQueue, LinkedList, DoubleLinkedList, StringBulder, Buffer (and subclasses),
Difference between LinearSeq and IndexedSeq
Usually LinearSeq is better. (List or Stream)
IndexedSeq better for apply, length, and mutable operations. (Array)
These do not add any new operations, but each offers different performance characteristics: A linear sequence has efficient head and tailoperations, whereas an indexed sequence has efficient apply, length, and (if mutable) updateoperations.