ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 02.01.2026

Просмотров: 3470

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Section 24.3

Chapter 24 · The Scala Collections API

539

String operations mkString, addString, and stringPrefix, which provide alternative ways of converting a collection to a string.

View operations consisting of two overloaded variants of the view method. A view is a collection that’s evaluated lazily. You’ll learn more about views in Section 24.15.

Table 24.1 · Operations in trait Traversable

What it is

What it does

Abstract method:

 

xs foreach f

Executes function f for every element of xs.

Addition:

 

xs ++ ys

A collection consisting of the elements of both xs

 

and ys. ys is a TraversableOnce collection, i.e.,

 

either a Traversable or an Iterator.

Maps:

 

xs map f

xs flatMap f

xs collect f

Conversions:

The collection obtained from applying the function f to every element in xs.

The collection obtained from applying the collection-valued function f to every element in xs and concatenating the results.

The collection obtained from applying the partial function f to every element in xs for which it is defined and collecting the results.

xs.toArray

Converts the collection to an array.

xs.toList

Converts the collection to a list.

xs.toIterable

Converts the collection to an iterable.

xs.toSeq

Converts the collection to a sequence.

xs.toIndexedSeq

Converts the collection to an indexed sequence.

xs.toStream

Converts the collection to a stream (a lazily

 

computed sequence).

xs.toSet

Converts the collection to a set.

xs.toMap

Converts a collection of key/value pairs to a map.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Section 24.3

Chapter 24 · The Scala Collections API

540

Table 24.1 · continued

Copying:

 

xs copyToBuffer buf

Copies all elements of the collection to buffer

 

buf.

xs copyToArray(arr, s, len)

Copies at most len elements of arr, starting at

 

index s. The last two arguments are optional.

Size info:

 

xs.isEmpty

Tests whether the collection is empty.

xs.nonEmpty

Tests whether the collection contains elements.

xs.size

The number of elements in the collection.

xs.hasDefiniteSize

True if xs is known to have finite size.

Element retrieval:

 

xs.head

The first element of the collection (or, some

 

element, if no order is defined).

xs.headOption

The first element of xs in an option value, or

 

None if xs is empty.

xs.last

The last element of the collection (or, some

 

element, if no order is defined).

xs.lastOption

The last element of xs in an option value, or None

 

if xs is empty.

xs find p

An option containing the first element in xs that

 

satisfies p, or None if no element qualifies.

Subcollections:

 

xs.tail

The rest of the collection except xs.head.

xs.init

The rest of the collection except xs.last.

xs slice (from, to)

A collection consisting of elements in some index

 

range of xs (from from, up to and excluding to).

xs take n

A collection consisting of the first n elements of

 

xs (or, some arbitrary n elements, if no order is

 

defined).

xs drop n

The rest of the collection except xs take n.

xs takeWhile p

The longest prefix of elements in the collection

 

that all satisfy p.

xs dropWhile p

The collection without the longest prefix of

 

elements that all satisfy p.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Section 24.3

Chapter 24 · The Scala Collections API

541

 

Table 24.1 · continued

xs filter p

The collection consisting of those elements of xs

 

that satisfy the predicate p.

xs withFilter p

A non-strict filter of this collection. All

 

operations on the resulting filter will only apply

 

to those elements of xs for which the condition p

 

is true.

xs filterNot p

The collection consisting of those elements of xs

 

that do not satisfy the predicate p.

Subdivisions:

 

xs splitAt n

Splits xs at a position, giving the pair of

 

collections (xs take n, xs drop n).

xs span p

Splits xs according to a predicate, giving the pair

 

of collections (xs takeWhile p,

 

xs.dropWhile p).

xs partition p

Splits xs into a pair of collections; one with

 

elements that satisfy the predicate p, the other

 

with elements that do not, giving the pair of

 

collections (xs filter p, xs.filterNot p).

xs groupBy f

Partitions xs into a map of collections according

 

to a discriminator function f.

Element conditions:

 

xs forall p

xs exists p

xs count p

Folds:

A boolean indicating whether the predicate p holds for all elements of xs.

A boolean indicating whether the predicate p holds for some element in xs.

The number of elements in xs that satisfy the predicate p.

(z /: xs)(op)

Applies binary operation op between successive

 

elements of xs, going left to right, starting with z.

(xs :\ z)(op)

Applies binary operation op between successive

 

elements of xs, going right to left, starting with z.

xs.foldLeft(z)(op)

Same as (z /: xs)(op).

xs.foldRight(z)(op)

Same as (xs :\ z)(op).

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index



Section 24.4

Chapter 24 · The Scala Collections API

542

Table 24.1 · continued

xs reduceLeft op

Applies binary operation op between successive

 

elements of non-empty collection xs, going left

 

to right.

xs reduceRight op

Applies binary operation op between successive

 

elements of non-empty collection xs, going right

 

to left.

Specific folds:

 

xs.sum

The sum of the numeric element values of

 

collection xs.

xs.product

The product of the numeric element values of

 

collection xs.

xs.min

The minimum of the ordered element values of

 

collection xs.

xs.max

The maximum of the ordered element values of

 

collection xs.

Strings:

 

xs addString (b, start,

Adds a string to StringBuilder b that shows all

sep, end)

elements of xs between separators sep enclosed

 

in strings start and end. start, sep, and end

 

are all optional.

xs mkString (start,

Converts the collection to a string that shows all

sep, end)

elements of xs between separators sep enclosed

 

in strings start and end. start, sep, and end

 

are all optional.

xs.stringPrefix

The collection name at the beginning of the string

 

returned from xs.toString.

Views:

 

xs.view

Produces a view over xs.

xs view (from, to)

Produces a view that represents the elements in

 

some index range of xs.

24.4 Trait Iterable

The next trait from the top in Figure 24.1 is Iterable. All methods in this trait are defined in terms of an an abstract method, iterator, which

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index