【scala初学】 Seq sequence IndexedSeq and LinearSeq

简介:

collections.png

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


在sequences上操作,如下表,分为几种:

  • Indexing and length 

    方法:applyisDefinedAtlengthindices, andlengthCompare

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

   lengthCompare 允许比较两个sequences序列的长度,甚至有一个可以是无限大

  • Index search operations

    方法indexOflastIndexOfindexOfSlicelastIndexOfSlice,indexWherelastIndexWhere

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

  • Addition operations

    方法:+::+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访问的时间总开销 。 因此,他是一个非常好的混合访问模式。










本文转自 wws5201985 51CTO博客,原文链接:http://blog.51cto.com/yjplxq/1432054,如需转载请自行联系原作者
目录
相关文章
|
文字识别 数据可视化 Serverless
阿里云多模态数据信息提取解决方案评测报告
本文基于阿里云多模态数据信息提取解决方案,对其进行全面评测。该方案利用百炼大模型等技术,支持文本、图像、音频和视频处理,显著提升效率并降低成本。体验中,文本和图片信息提取功能表现出色,部署便捷且准确率高。优势包括易用性、多模态支持和高性价比,但文档完善性和模型定制性等方面仍有提升空间。建议增强模型可定制性、跨模态融合能力及丰富文档案例,以进一步优化用户体验。
1023 30
|
SQL Oracle 数据库
使用访问指导(SQL Access Advisor)优化数据库业务负载
本文介绍了Oracle的SQL访问指导(SQL Access Advisor)的应用场景及其使用方法。访问指导通过分析给定的工作负载,提供索引、物化视图和分区等方面的优化建议,帮助DBA提升数据库性能。具体步骤包括创建访问指导任务、创建工作负载、连接工作负载至访问指导、设置任务参数、运行访问指导、查看和应用优化建议。访问指导不仅针对单条SQL语句,还能综合考虑多条SQL语句的优化效果,为DBA提供全面的决策支持。
452 11
|
人工智能 安全 搜索推荐
AI与能源管理:智能电网的未来
本文探讨了AI与智能电网的融合及其对能源管理的深远影响。智能电网利用先进的信息、通信和AI技术,实现电力的自主、智能化、高效管理。AI在精准预测电力需求、实时监测与故障诊断、智能能源调度、个性化能源服务和优化可再生能源利用等方面发挥关键作用,推动能源管理的高效、智能和可持续发展。
playwright Test 录制视频
playwright Test 录制视频
692 3
|
关系型数据库 MySQL 数据库
MYSQL索引的分类与创建语法详解
理解并合理应用这些索引类型,能够有效提高MySQL数据库的性能和查询效率。每种索引类型都有其特定的优势,适当地使用它们可以为数据库操作带来显著的性能提升。
733 3
|
Java 数据安全/隐私保护 开发者
【SpringBoot】讲清楚日志文件&&lombok
【SpringBoot】讲清楚日志文件&&lombok
301 5
|
小程序 前端开发 Android开发
微信小程序(van-tabs) 去除横向滚动条样式(附加源码解决方案+报错图)
微信小程序(van-tabs) 去除横向滚动条样式(附加源码解决方案+报错图)
1108 1
|
存储 安全 算法
Spring Security系列教程22--Spring Security中的密码加密
前言 截止到现在,一一哥 已经带各位学习了很多关于Spring Security的知识点,但是Spring Security作为一个安全框架,其中必然就应该带有安全加密方面的内容,所以本篇文章,一一哥 就带各位来学习Spring Security中的密码加密机制。 Lets go! 一. 密码加密简介 1. 散列加密概述 我们开发时进行密码加密,可用的加密手段有很多,比如对称加密、非对称加密、信息摘要等。在一般的项目里,常用的就是信息摘要算法,也可以被称为散列加密函数,或者称为散列算法、哈希函数。这是一种可以从任何数据中创建数字“指纹”的方法,常用的散列函数有 MD5 消息摘要算法、安全散列
2271 1
|
JavaScript 前端开发 API
vue配置和使用mockjs
vue配置和使用mockjs
315 0
|
大数据 网络架构
使用海外静态IP代理时网速就会变慢是什么原因?如何解决?
使用海外IP代理可能会导致网速变慢,这是因为代理服务器位于国外,需要经过更多的中转和路由,以及更长的网络延迟时间。
使用海外静态IP代理时网速就会变慢是什么原因?如何解决?

热门文章

最新文章