Section 24.16 |
Chapter 24 · The Scala Collections API |
597 |
|
Table 24.12 · continued |
it.nonEmpty |
Tests whether the collection contains elements |
|
(alias of hasNext). |
it.size |
The number of elements returned by it. Note: it |
|
will be at its end after this operation! |
it.length |
Same as it.size. |
it.hasDefiniteSize |
Returns true if it is known to return finitely |
|
many elements (by default the same as isEmpty). |
Element retrieval index search: |
it find p |
An option containing the first element returned by |
|
it that satisfies p, or None if no element qualifies. |
|
Note: The iterator advances to just after the |
|
element, or, if none is found, to the end. |
it indexOf x |
The index of the first element returned by it that |
|
equals x. Note: The iterator advances past the |
|
position of this element. |
it indexWhere p |
The index of the first element returned by it that |
|
satisfies p. Note: The iterator advances past the |
|
position of this element. |
Subiterators: |
|
it take n |
An iterator returning of the first n elements of it. |
|
Note: it will advance to the position after the |
|
n’th element, or to its end, if it contains less than |
|
n elements. |
it drop n |
The iterator that starts with the (n + 1)’th element |
|
of it. Note: it will advance to the same position. |
it slice (m, n) |
The iterator that returns a slice of the elements |
|
returned from it, starting with the m’th element |
|
and ending before the n’th element. |
it takeWhile p |
An iterator returning elements from it as long as |
|
condition p is true. |
it dropWhile p |
An iterator skipping elements from it as long as |
|
condition p is true, and returning the remainder. |
it filter p |
An iterator returning all elements from it that |
|
satisfy the condition p. |
it withFilter p |
Same as it filter p. Needed so that iterators |
|
can be used in for expressions. |
Cover · Overview · Contents · Discuss · Suggest · Glossary · Index
Section 24.16 |
Chapter 24 · The Scala Collections API |
598 |
|
Table 24.12 · continued |
it filterNot p |
An iterator returning all elements from it that do |
|
not satisfy the condition p. |
Subdivisions: |
|
it partition p |
Splits it into a pair of two iterators; one |
|
returning all elements from it that satisfy the |
|
predicate p, the other returning all elements from |
|
it that do not. |
Element conditions: |
|
it forall p
it exists p
it count p
Folds:
A boolean indicating whether the predicate p holds for all elements returned by it.
A boolean indicating whether the predicate p holds for some element in it.
The number of elements in it that satisfy the predicate p.
(z /: it)(op) |
Applies binary operation op between successive |
|
elements returned by it, going left to right, |
|
starting with z. |
(it :\ z)(op) |
Applies binary operation op between successive |
|
elements returned by it, going right to left, |
|
starting with z. |
it.foldLeft(z)(op) |
Same as (z /: it)(op). |
it.foldRight(z)(op) |
Same as (it :\ z)(op). |
it reduceLeft op |
Applies binary operation op between successive |
|
elements returned by non-empty iterator it, |
|
going left to right. |
it reduceRight op |
Applies binary operation op between successive |
|
elements returned by non-empty iterator it, |
|
going right to left. |
Specific folds: |
|
it.sum |
The sum of the numeric element values returned |
|
by iterator it. |
it.product |
The product of the numeric element values |
|
returned by iterator it. |
it.min |
The minimum of the ordered element values |
|
returned by iterator it. |
Cover · Overview · Contents · Discuss · Suggest · Glossary · Index
Section 24.16 |
Chapter 24 · The Scala Collections API |
599 |
it.max
Zippers:
it zip jt
it zipAll (jt, x, y)
it.zipWithIndex
Update:
it patch (i, jt, r)
Comparison:
it sameElements jt
Strings:
it addString (b, start, sep, end)
it mkString (start, sep, end)
The maximum of the ordered element values returned by iterator it.
An iterator of pairs of corresponding elements returned from iterators it and jt.
An iterator of pairs of corresponding elements returned from iterators it and jt, where the shorter iterator is extended to match the longer one by appending elements x or y.
An iterator of pairs of elements returned from it with their indicies.
The iterator resulting from it by replacing r elements starting with i by the patch iterator jt.
A test whether iterators it and jt return the same elements in the same order. Note: At least one of it and jt will be at its end after this operation.
Adds a string to StringBuilder b that shows all elements returned by it between separators sep enclosed in strings start and end. start,sep, and end are all optional.
Converts the collection to a string that shows all elements returned by it between separators sep enclosed in strings start and end. start,sep, and end are all optional.
Buffered iterators
Sometimes you want an iterator that can “look ahead” so that you can inspect the next element to be returned without advancing past that element. Consider, for instance, the task to skip leading empty strings from an iterator that returns a sequence of strings. You might be tempted to write something like the following method:
Cover · Overview · Contents · Discuss · Suggest · Glossary · Index
Section 24.16 |
Chapter 24 · The Scala Collections API |
600 |
// This won’t work
def skipEmptyWordsNOT(it: Iterator[String]) { while (it.next().isEmpty) {}
}
But looking at this code more closely, it’s clear that this is wrong: the code will indeed skip leading empty strings, but it will also advance it past the first non-empty string!
The solution to this problem is to use a buffered iterator, an instance of trait BufferedIterator. BufferedIterator is a subtrait of Iterator, which provides one extra method, head. Calling head on a buffered iterator will return its first element, but will not advance the iterator. Using a buffered iterator, skipping empty words can be written like this:
def skipEmptyWords(it: BufferedIterator[String]) = while (it.head.isEmpty) { it.next() }
Every iterator can be converted to a buffered iterator by calling its buffered method. Here’s an example:
scala> val it = Iterator(1, 2, 3, 4) it: Iterator[Int] = non-empty iterator
scala> val bit = it.buffered
bit: java.lang.Object with scala.collection. BufferedIterator[Int] = non-empty iterator
scala> bit.head res10: Int = 1
scala> bit.next() res11: Int = 1
scala> bit.next() res11: Int = 2
Note that calling head on the buffered iterator, bit, did not advance it. Therefore, the subsequent call, bit.next(), returned again the same value as bit.head.
Cover · Overview · Contents · Discuss · Suggest · Glossary · Index
Section 24.17 |
Chapter 24 · The Scala Collections API |
601 |
24.17Creating collections from scratch
You have already seen syntax like List(1, 2, 3), which creates a list of three integers, and Map('A' -> 1, 'C' -> 2), which creates a map with two bindings. This is actually a universal feature of Scala collections. You can take any collection name and follow it by a list of elements in parentheses. The result will be a new collection with the given elements. Here are some more examples:
Traversable() |
|
// An empty traversable object |
|
List() |
|
// The empty list |
|
List(1.0, 2.0) |
|
// A |
list with elements 1.0, 2.0 |
Vector(1.0, 2.0) |
// A |
vector with elements 1.0, |
2.0 |
Iterator(1, 2, 3) |
// An iterator returning three |
integers. |
Set(dog, cat, |
bird) |
// A |
set of three animals |
|
HashSet(dog, cat, bird) |
// A |
hash set of the same animals |
Map('a' -> 7, |
'b' -> 0) |
// A |
map from characters to integers |
“Under the covers” each of the above lines is a call to the apply method of some object. For instance, the third line above expands to:
List.apply(1.0, 2.0)
So this is a call to the apply method of the companion object of the List class. That method takes an arbitrary number of arguments and constructs a list from them. Every collection class in the Scala library has a companion object with such an apply method. It does not matter whether the collection class represents a concrete implementation, like List, Stream, or Vector, or whether it is an trait such as Seq, Set, or Traversable. In the latter case, calling apply will produce some default implementation of the trait. Here are some examples:
scala> List(1, 2, 3)
res17: List[Int] = List(1, 2, 3)
scala> Traversable(1, 2, 3)
res18: Traversable[Int] = List(1, 2, 3)
scala> mutable.Traversable(1, 2, 3)
res19: scala.collection.mutable.Traversable[Int] = ArrayBuffer(1, 2, 3)
Cover · Overview · Contents · Discuss · Suggest · Glossary · Index