Seq Collections Flashcards Preview

Scala > Seq Collections > Flashcards

Flashcards in Seq Collections Deck (16):
1

apply (sequences)

s = Seq(1, 2, 3)
s.apply(1)

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.

2

apply(Set)

s = Set('a', 'b', 'c')
s.apply('b')

true

For sets, apply is a membership test. For instance, Set('a', 'b', 'c')('b') gives true whereas Set()('a') gives false.

3

apply(map)
m = Map('a' -> 1, 'b' -> 10, 'c' -> 100)
m.apply('b')

10

For maps, apply is a selection. For instance, Map('a' -> 1, 'b' -> 10, 'c' -> 100)('b') gives 10.

4

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.

5

lengthCompare()

The lengthCompare method allows you to compare the lengths of a sequences with an Int even if the sequences has infinite length.

6

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.

7

Sequence Addition operations +:, :+, padTo

return new sequences obtained by adding elements at the front or the end of a sequence.

8

Sequence Update operations updated,patch

return a new sequence obtained by replacing some elements of the original sequence.

9

Sequence Sorting operations sorted, sortWith, sortBy

sort sequence elements according to various criteria.

10

Sequence Reversal operations reverse, reverseIterator, reverseMap

yield or process sequence elements in reverse order.

11

Sequence Comparisons startsWith, endsWith, contains, containsSlice, corresponds

Relate two sequences or search an element in a sequence.

12

Sequence Multiset operations intersect, diff, union, distinct

Perform set-like operations on the elements of two sequences or remove duplicates.

13

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.

14

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),

15

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.

16

Buffer Subtypes. Also, mutable or immutable?

ListBuffer and ArrayBuffer. Buffers are mutable. Good for efficiently updating elements, insertion and removal of elements.