【scala初学】 Seq sequence IndexedSeq and LinearSeq

collections.png

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


在sequences上操做,以下表,分为几种:
api

  • Indexing and length app

    方法:applyisDefinedAtlengthindices, andlengthCompareide

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

   lengthCompare 容许比较两个sequences序列的长度,甚至有一个能够是无限大spa

  • Index search operationsscala

    方法indexOflastIndexOfindexOfSlicelastIndexOfSlice,indexWherelastIndexWhere翻译

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

  • Addition operationsorm

    方法:+::+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 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.Seqs). 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.
    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的集合中

相关文章
相关标签/搜索