官术网_书友最值得收藏!

  • Learn Scala Programming
  • Slava Schmidt
  • 347字
  • 2021-06-10 19:35:40

IterableOps

IterableOps extends IterableOnceOps and contains methods that is impossible to implement without a possibility to iterate over the collection multiple times.

They fall into the following categories:

  • Element retrieval: head, headOption, last, and lastOption return the first or last element of the collection throwing NoSuchElementException or returning None for an empty collection.
  • Size: sizeCompare is an optimization that allows us to efficiently compare the size of the collection with given value.
  • Subcollection retrieval: partitionpartitionWithsplitAt, takeRight, dropRight, grouped, sliding, tail, init, groupBygroupMap, groupMapReducetails, and inits. These split the collection as defined by some predicate or index, take or drop elements from the end, group elements by some criteria or predicate possibly applying transformative function, and discard first or last elements.
  • Mapping: scanRight produces a collection containing the cumulative results of applying the giving function starting from the end of the collection.
  • Addition: concat, ++ returns another collection containing all elements of this collection and a collection provided as an argument.
  • Zippers: zip, zipAll, unzip, and unzip3 combine the elements of the collection with the elements of another collection into a product, or split them into separate collections.
  • Transformation: transpose transforms the collection of collections by turning rows into columns and vice versa.

The following methods that defined the abstract in IterableOnceOps got a concrete default implementation in IterableOps: filter, filterNot, take, takeWhile, span, drop, dropWile, slice, scanLeft, map, flatMap, flatten, collect, and zipWithIndexisTraversableAgain is overriden to return true.

It's worth noting that Iterable and IterableOnce do not support a general-equality operation, it is defined on specific collection subtypes. Because of this, it is impossible to compare these types directly using the equality operation, as the following example suggests:

scala> Set(1,2,3) == Seq(1,2,3)
res4: Boolean = false

Also there are three special methods that deserve our additional attention because they introduce types we haven't met yet:

  • def withFilter(p: A => Boolean): collection.WithFilter[A, CC]
  • def iterableFactory: IterableFactory[CC]
  • def view: View[A]

Let's discuss them quickly before moving on to more specific collection types.

主站蜘蛛池模板: 怀来县| 宁陵县| 都江堰市| 若羌县| 安西县| 探索| 武陟县| 鲁山县| 吴桥县| 泸溪县| 四平市| 河南省| 长宁县| 江陵县| 商南县| 垣曲县| 固镇县| 西林县| 贵州省| 连州市| 博白县| 太康县| 淮北市| 运城市| 磴口县| 卓资县| 阿勒泰市| 望奎县| 承德市| 蒲江县| 日照市| 宁南县| 仙居县| 宝丰县| 犍为县| 安陆市| 大洼县| 定结县| 宜都市| 两当县| 聊城市|