- Learn Scala Programming
- Slava Schmidt
- 380字
- 2021-06-10 19:35:42
Seq
Seq is probably the most ubiquitous collection in the library. Like Map, it has a notion of succession of elements and the elements have indices. It is defined as trait Seq[+A] extends Iterable[A] with PartialFunction[Int, A] with SeqOps[A, Seq, Seq[A]] with Equals. Also similar to map, Seq specifies support for the equality relation and also extends PartialFunction, which accepts an index of the element as a parameter. As there are a lots of classes implementing Seq, we will take a gradual approach and look at them level by level. The direct children of Seq are shown in the following diagram:
scala.Seq, known from previous Scala versions, is now replaced by scala.collection.immutable.Seq.
As with other collections, SeqOps extend IterableOps by adding quite a few methods:
- Element retrieval: apply retrieves an element with a given index.
- Indexing and search: segmentLength, isDefinedAt, indexWhere, indexOf, lastIndexOf, lastIndexWhere, indexOfSlice, lastIndexOfSlice, containsSlice, contains, startsWith, endsWith, indices, and search. These methods allow us to retrieve information about the presence or indexes of elements or subsequences given some predicate or element value.
- Size: length and lengthCompare provide efficient operations to retrieve the length of the collection.
- Addition: prepend, +, appended, :+, prependAll, ++, appendedAll, :++, concat, and union. Can be used to append or prepend one or multiple elements to the collection.
- Filtering: distinct and distinctBy remove duplicates, possibly given some predicate.
- Reversal: reverse and reverseIterator return a new collection with elements in the reversed order.
- Sorting: sorted, sortWith, and sortBy sort this collection by some implicit Ordering, or by a given function, or both.
- Equality: sameElements and corresponds check whether this collection contains the same elements in the same order as given, using equality-checking or the provided comparison function.
- Subcollection retrieval: permutations and combinations. These methods allow us to retrieve a subcollection(s) that satisfies given conditions.
- Updates: diff, intersect, patch, updated, and update (mutable). Modify elements of this collection using another collection or element and returning another collection (except the last method defined on mutable.Seq, which update elements in place).
Each of the Seq direct descendants has its own specific properties and a subtree of implementations. We'll breeze through them now.
- 新編Visual Basic程序設計上機實驗教程
- Advanced Quantitative Finance with C++
- Designing Machine Learning Systems with Python
- C語言程序設計習題解析與上機指導(第4版)
- SQL Server 2012數據庫技術及應用(微課版·第5版)
- Scratch 3.0少兒編程與邏輯思維訓練
- SQL Server從入門到精通(第3版)
- Apache Spark 2.x for Java Developers
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- Rust游戲開發實戰
- C語言程序設計與應用(第2版)
- Practical Microservices
- SSH框架企業級應用實戰
- Drupal 8 Development Cookbook(Second Edition)
- Oracle Database XE 11gR2 Jump Start Guide