Seq trait 代表sequences. 一个序列继承自iterable,,有一个length方法,并且他的元素有固定的索引位置,位置从0开始。
在sequences上操作,如下表,分为几种:
Indexing and length
方法:
apply
,isDefinedAt
,length
,indices
, andlengthCompare
apply方法用于建立索引,因此类型Seq[T]的一个序列是一个partial函数,他获取一个int参数,并且产生了一个类型T的序列。综上,Seq[T]扩展了 PartialFunction[Int, T]。
允许比较两个sequences序列的长度,甚至有一个可以是无限大lengthCompare
Index search operations
方法:
indexOf
,lastIndexOf
,indexOfSlice
,lastIndexOfSlice
,indexWhere
,lastIndexWhere
,segmentLength
,prefixLength
返回索引或者匹配一些断言Addition operations
方法:
+:
,:+
,padTo
返回一些新的序列,通过头尾增加元素
Update operations
方法:
updated
,patch
返回一个新的序列,通过更新
Sorting operations
方法:
sorted
,sortWith
,sortBy
,排序,根据多个标准
reversal operations
方法:
reverse
,reverseIterator
,reverseMap
倒序产生或者处理一个序列
Comparisons
方法:
startsWith
,endsWith
,contains
,containsSlice
,corresponds
比较连个元素或者在一个序列中查找一个元素
Multiset
方法:
intersect
,diff
,union
,distinct
在两个序列的元素上做类set的操作,或者移除重复值(这个估计是指单序列中)
如果一个序列是可变的(mutable),会提供一个额外的方法,update(书生:区别updated),它能让序列的元素更新。正如在Scala中,语法如seq(idx)=elem 仅仅是seq.update(idx,elem)的一个简写,可以说update给予了一些语法上的便利。
updates: 返回一个被修改后的新的序列代替原序列,对所有序列有效。
update: 修改序列中的元素, 只对可变mutable序列有效
Class Seq 函数
序列
WHAT IT IS | WHAT IT DOES |
---|---|
Indexing and Length: | |
xs(i) | (or, written out, xs apply i ). The element of xs at index i . |
xs isDefinedAt i | Tests whether i is contained in xs.indices . |
xs.length | The length of the sequence (same as size ). |
xs.lengthCompare ys | Returns -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.indices | The index range of xs , extending from 0 to xs.length - 1 . |
Index Search: | |
xs indexOf x | The index of the first element in xs equal to x (several variants exist). |
xs lastIndexOf x | The index of the last element in xs equal to x (several variants exist). |
xs indexOfSlice ys | The first index of xs such that successive elements starting from that index form the sequence ys . |
xs lastIndexOfSlice ys | The last index of xs such that successive elements starting from that index form the sequence ys . |
xs indexWhere p | The 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 p | The length of the longest prefix of elements in xs that all satisfy the predicate p . |
Additions: | |
x +: xs | A new sequence that consists of x prepended to xs . |
xs :+ x | A 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.Seq s). Changes the element of xs at index i to x . |
Sorting: | |
xs.sorted | A new sequence obtained by sorting the elements of xs using the standard ordering of the element type of xs . |
xs sortWith lt | A new sequence obtained by sorting the elements of xs using lt as comparison operation. |
xs sortBy f | A 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.reverse | A sequence with the elements of xs in reverse order. |
xs.reverseIterator | An iterator yielding all the elements of xs in reverse order. |
xs reverseMap f | A sequence obtained by mapping f over the elements of xs in reverse order. |
Comparisons: | |
xs startsWith ys | Tests whether xs starts with sequence ys (several variants exist). |
xs endsWith ys | Tests whether xs ends with sequence ys (several variants exist). |
xs contains x | Tests whether xs has an element equal to x . |
xs containsSlice ys | Tests 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 ys | The multi-set intersection of sequences xs and ys that preserves the order of elements in xs . |
xs diff ys | The multi-set difference of sequences xs and ys that preserves the order of elements in xs . |
xs union ys | Multiset union; same as xs ++ ys . |
xs.distinct | A subsequence of xs that contains no duplicated element. |
scala.collection.immutable.List
and
scala.collection.immutable.Stream
,频繁调用indexed序列的
scala.Array
and
scala.collection.mutable.ArrayBuffer
.
Vector提供了一个令人感兴趣的折中,在indexed和linear,他能有效的确保稳定的index和linear访问的时间总开销 。 因此,他是一个非常好的混合访问模式。
下一篇翻译Buffer ,他仅出现在可变mutable的集合中
转载于:https://blog.51cto.com/yjplxq/1432054