Seq Collections Flashcards

1
Q

apply (sequences)

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

apply(Set)

s = Set(‘a’, ‘b’, ‘c’)
s.apply(‘b’)

A

true

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

10

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a sequence?

A

A sequence is a kind of iterable that has a length and whose elements have fixed index positions, starting from 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

lengthCompare()

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Sequence Index Search Operations

A

indexOf, lastIndexOf, indexOfSlice, lastIndexOfSlice, indexWhere, lastIndexWhere, segmentLength, prefixLength, which return the index of an element equal to a given value or matching some predicate.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Sequence Addition operations +:, :+, padTo

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Sequence Update operations updated,patch

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Sequence Sorting operations sorted, sortWith, sortBy

A

sort sequence elements according to various criteria.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Sequence Reversal operations reverse, reverseIterator, reverseMap

A

yield or process sequence elements in reverse order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Sequence Comparisons startsWith, endsWith, contains, containsSlice, corresponds

A

Relate two sequences or search an element in a sequence.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Sequence Multiset operations intersect, diff, union, distinct

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Difference between update() and updated(). (For Seq)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Subclasses of Seq

A

Immutable: List, Stream, Queue, Stack, Vector, NumericRange, String

Mutable: List, Stream, Queue, Stack, Vector, NumericRange, String, PriorityQueue, LinkedList, DoubleLinkedList, StringBulder, Buffer (and subclasses),

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Difference between LinearSeq and IndexedSeq

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Buffer Subtypes. Also, mutable or immutable?

A

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