【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的集合中

相關文章
相關標籤/搜索