collections.png

  Seq trait 代表sequences. 一个序列继承自iterable,,有一个length方法,并且他的元素有固定的索引位置,位置从0开始。


在sequences上操作,如下表,分为几种:

  • Indexing and length 

    方法:applyisDefinedAtlengthindices, andlengthCompare

   apply方法用于建立索引,因此类型Seq[T]的一个序列是一个partial函数,他获取一个int参数,并且产生了一个类型T的序列。综上,Seq[T]扩展了 PartialFunction[Int, T]。

   lengthCompare 允许比较两个sequences序列的长度,甚至有一个可以是无限大

  • Index search operations

    方法indexOflastIndexOfindexOfSlicelastIndexOfSlice,indexWherelastIndexWhere

    segmentLengthprefixLength
    返回索引或者匹配一些断言

  • Addition operations

    方法:+::+padTo

    返回一些新的序列,通过头尾增加元素

  • Update operations

    方法:updatedpatch

    返回一个新的序列,通过更新

  • Sorting operations

    方法:sortedsortWithsortBy,

    排序,根据多个标准

  • reversal operations

    方法:reversereverseIteratorreverseMap

    倒序产生或者处理一个序列

  • Comparisons

    方法:startsWithendsWithcontainscontainsSlicecorresponds

    比较连个元素或者在一个序列中查找一个元素

  • Multiset

    方法:intersectdiffuniondistinct

    在两个序列的元素上做类set的操作,或者移除重复值(这个估计是指单序列中)



  如果一个序列是可变的(mutable),会提供一个额外的方法,update(书生:区别updated),它能让序列的元素更新。正如在Scala中,语法如seq(idx)=elem 仅仅是seq.update(idx,elem)的一个简写,可以说update给予了一些语法上的便利。

updates: 返回一个被修改后的新的序列代替原序列,对所有序列有效。

update: 修改序列中的元素, 只对可变mutable序列有效


Class Seq 函数



序列

WHAT IT ISWHAT IT DOES
Indexing and Length:
xs(i)(or, written out, xs apply i). The element of xs at index i.
xs isDefinedAt iTests whether i is contained in xs.indices.
xs.lengthThe length of the sequence (same as size).
xs.lengthCompare ysReturns -1 if xs is shorter than ys, +1 if it is longer, and 0 is they have the same length. Works even if one if the sequences is infinite.
xs.indicesThe index range of xs, extending from 0 to xs.length - 1.
Index Search:
xs indexOf xThe index of the first element in xs equal to x (several variants exist).
xs lastIndexOf xThe index of the last element in xs equal to x (several variants exist).
xs indexOfSlice ysThe first index of xs such that successive elements starting from that index form the sequence ys.
xs lastIndexOfSlice ysThe last index of xs such that successive elements starting from that index form the sequence ys.
xs indexWhere pThe index of the first element in xs that satisfies p (several variants exist).
xs segmentLength (p, i)The length of the longest uninterrupted segment of elements in xs, starting with xs(i), that all satisfy the predicate p.
xs prefixLength pThe length of the longest prefix of elements in xs that all satisfy the predicate p.
Additions:
x +: xsA new sequence that consists of x prepended to xs.
xs :+ xA new sequence that consists of x appended to xs.
xs padTo (len, x)The sequence resulting from appending the value x to xs until length len is reached.
Updates:
xs patch (i, ys, r)The sequence resulting from replacing r elements of xs starting with i by the patch ys.
xs updated (i, x)A copy of xs with the element at index i replaced by x.
xs(i) = x(or, written out, xs.update(i, x), only available for mutable.Seqs). Changes the element of xs at index i to x.
Sorting:
xs.sortedA new sequence obtained by sorting the elements of xs using the standard ordering of the element type of xs.
xs sortWith ltA new sequence obtained by sorting the elements of xs using lt as comparison operation.
xs sortBy fA new sequence obtained by sorting the elements of xs. Comparison between two elements proceeds by mapping the function f over both and comparing the results.
Reversals:
xs.reverseA sequence with the elements of xs in reverse order.
xs.reverseIteratorAn iterator yielding all the elements of xs in reverse order.
xs reverseMap fA sequence obtained by mapping f over the elements of xs in reverse order.
Comparisons:
xs startsWith ysTests whether xs starts with sequence ys (several variants exist).
xs endsWith ysTests whether xs ends with sequence ys (several variants exist).
xs contains xTests whether xs has an element equal to x.
xs containsSlice ysTests whether xs has a contiguous subsequence equal to ys.
(xs corresponds ys)(p)Tests whether corresponding elements of xs and ys satisfy the binary predicate p.
Multiset Operations:
xs intersect ysThe multi-set intersection of sequences xs and ys that preserves the order of elements in xs.
xs diff ysThe multi-set difference of sequences xs and ys that preserves the order of elements in xs.
xs union ysMultiset union; same as xs ++ ys.
xs.distinctA subsequence of xs that contains no duplicated element.
    Trait  Seq有两个子trait   LinearSeq 和   IndexedSeq。 这些都没有增加新的函数,但是每个都提供了不同的执行特征:linear 序列包括有效head和tail方法, 然而一个indexed 序列 包含有效的apply,length,(如果可变)update方法。 频繁调用linear序列的是 scala.collection.immutable.List  and scala.collection.immutable.Stream ,频繁调用indexed序列的 scala.Array and  scala.collection.mutable.ArrayBuffer .

Vector提供了一个令人感兴趣的折中,在indexed和linear,他能有效的确保稳定的index和linear访问的时间总开销 。 因此,他是一个非常好的混合访问模式。


下一篇翻译Buffer ,他仅出现在可变mutable的集合中