sliding
方法(或屬性)屬於 scala.collection.AbstractIterator
類(class),其相關用法說明如下。
用法:
def sliding[B >: A](size: Int, step: Int): GroupedIterator[B]
返回一個迭代器,它顯示了這個迭代器的 "sliding window" 視圖。第一個參數是窗口大小,第二個參數step
是每次迭代時窗口前進多遠。 step
默認為 1
。
返回的 GroupedIterator
可以配置為將部分結果填充到 size
大小或完全抑製部分結果。
示例用法:
// Returns List(ArraySeq(1, 2, 3), ArraySeq(2, 3, 4), ArraySeq(3, 4, 5))
(1 to 5).iterator.sliding(3).toList
// Returns List(ArraySeq(1, 2, 3, 4), ArraySeq(4, 5))
(1 to 5).iterator.sliding(4, 3).toList
// Returns List(ArraySeq(1, 2, 3, 4))
(1 to 5).iterator.sliding(4, 3).withPartial(false).toList
// Returns List(ArraySeq(1, 2, 3, 4), ArraySeq(4, 5, 20, 25))
// Illustrating that withPadding's argument is by-name.
val it2 = Iterator.iterate(20)(_ + 5)
(1 to 5).iterator.sliding(4, 3).withPadding(it2.next).toList
值參數:
- size
每組的元素數量
- step
連續組的第一個元素之間的距離
返回:
如果剩餘要分組的元素少於
size
元素,則生成大小為size
的Seq[B]
的GroupedIterator
除了最後一個元素(可能是唯一的元素)將被截斷.可以配置此行為。注意:
重用:調用此方法後,應丟棄調用它的迭代器,隻使用返回的迭代器。使用舊迭代器是未定義的,可能會發生變化,並且也可能導致對新迭代器的更改。
繼承自:
- collection.Iterator.sliding
源碼:
- Iterator.scala
相關用法
- Scala AbstractIterator.mkString用法及代碼示例
- Scala AbstractIterator.collectFirst用法及代碼示例
- Scala AbstractIterator.grouped用法及代碼示例
- Scala AbstractIterator.addString用法及代碼示例
- Scala AbstractIterable.partitionMap用法及代碼示例
- Scala AbstractIterable.inits用法及代碼示例
- Scala AbstractIterable.groupMap用法及代碼示例
- Scala AbstractIterable.sizeIs用法及代碼示例
- Scala AbstractIterable.sliding用法及代碼示例
- Scala AbstractIterable.mkString用法及代碼示例
- Scala AbstractIterable.sizeCompare用法及代碼示例
- Scala AbstractIterable.groupMapReduce用法及代碼示例
- Scala AbstractIterable.scanRight用法及代碼示例
- Scala AbstractIterable.unzip用法及代碼示例
- Scala AbstractIterable.collectFirst用法及代碼示例
- Scala AbstractIterable.transpose用法及代碼示例
- Scala AbstractIterable.lazyZip用法及代碼示例
- Scala AbstractIterable.groupBy用法及代碼示例
- Scala AbstractIterable.addString用法及代碼示例
- Scala AbstractIterable.unzip3用法及代碼示例
- Scala AbstractIterable.tails用法及代碼示例
- Scala AbstractIndexedSeqView.collectFirst用法及代碼示例
- Scala AbstractIndexedSeqView.partitionMap用法及代碼示例
- Scala AbstractIndexedSeqView.sliding用法及代碼示例
- Scala AbstractIndexedSeqView.addString用法及代碼示例
注:本文由純淨天空篩選整理自scala-lang.org大神的英文原創作品 AbstractIterator.sliding。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。