Scala的List不仅可以指定循环区间,而且还能根据步长筛选元素。

List中的步长,by关键字:

scala> 1 to 100 by 3
res64: scala.collection.immutable.Range = Range(1, 4, 7, 10, 13, 16, 19, 22, 25,28, 31, 34, 37, 40, 43, 46, 49, 52, 55, 58, 61, 64, 67, 70, 73, 76, 79, 82, 85,88, 91, 94, 97, 100)scala> 1 to 100 by 2
res65: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9, 11, 13, 15, 17, 1
9, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 5
9, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 9
9)scala> 1 to 100 by 1
res66: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1
1, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 3
1, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 5
1, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 7
1, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 9
1, 92, 93, 94, 95, 96, 97, 98, 99, 100)

可以看出,每个元素都相差了by后面的数字个步长

如果我们利用这个求奇数的平方和,1*1+3*3+5*5+ … + 99*99,结果为166650

scala> List(1 to 100 by 2:_*) map (i=>i*i) sum
res59: Int = 166650

如果用filter也能做

scala> (1 to 100) filter (_%2!=0) map (i=>i*i) sum
res60: Int = 166650

看一下源代码里怎么定义这个by的

/** The `Range` class represents integer values in range*  ''[start;end)'' with non-zero step value `step`.*  It's a special case of an indexed sequence.*  For example:**  {{{*     val r1 = 0 until 10*     val r2 = r1.start until r1.end by r1.step + 1*     println(r2.length) // = 5*  }}}**  @param start      the start of this range.*  @param end        the exclusive end of the range.*  @param step       the step for the range.**  @author Martin Odersky*  @author Paul Phillips*  @version 2.8*  @since   2.5*  @see [[http://docs.scala-lang.org/overviews/collections/concrete-immutable-collection-classes.html#ranges "Scala's Collection Library overview"]]*  section on `Ranges` for more information.**  @define Coll Range*  @define coll range*  @define mayNotTerminateInf*  @define willNotTerminateInf*  @define doesNotUseBuilders*    '''Note:''' this method does not use builders to construct a new range,*         and its complexity is O(1).*/
@SerialVersionUID(7618862778670199309L)
class Range(val start: Int, val end: Int, val step: Int)
extends IndexedSeq[Int]with collection.CustomParallelizable[Int, ParRange]with Serializable
{override def par = new ParRange(this)// This member is designed to enforce conditions://   (step != 0) && (length <= Int.MaxValue),// but cannot be evaluated eagerly because we have a pattern where ranges// are constructed like:    "x to y by z"// The "x to y" piece should not trigger an exception. So the calculation// is delayed, which means it will not fail fast for those cases where failing// was correct.private lazy val numRangeElements: Int = Range.count(start, end, step, isInclusive)protected def copy(start: Int, end: Int, step: Int): Range = new Range(start, end, step)/** Create a new range with the `start` and `end` values of this range and*  a new `step`.**  @return a new range with a different step*/def by(step: Int): Range = copy(start, end, step)def isInclusive = false@inline final override def foreach[@specialized(Unit) U](f: Int => U) {if (length > 0) {val last = this.lastvar i = startwhile (i != last) {f(i)i += step}f(i)}}

这里重写了foreach方法,其实就是遍历每个元素时给i+=step。

其实返回的是一个range对象

scala> List(1 to 100 by 2)
res77: List[scala.collection.immutable.Range] = List(Range(1, 3, 5, 7, 9, 11, 13
, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53
, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93
, 95, 97, 99))

想要转换为int,则需要

scala> List(1 to 100 by 2:_*)
res78: List[Int] = List(1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 3
1, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 7
1, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99)

-EOF-

Scala中List的步长by相关推荐

  1. Scala 中的 Array 数组 详解

    Scala 中的 Array 数组 详解 1. 一维数组的声明与遍历 2. 二维数组的声明与遍历 3. 可变长度数组 ArrayBuffer 4.数组其余方法详解 Scala 语言中提供的数组是用来存 ...

  2. Akka 系列(五):Java 和 Scala 中的 Future

    随着CPU的核数的增加,异步编程模型在并发领域中的得到了越来越多的应用,由于Scala是一门函数式语言,天然的支持异步编程模型,今天主要来看一下Java和Scala中的Futrue,带你走入异步编程的 ...

  3. scala 中List的简单使用

    /*** scala 中List的使用**/object ListUse {def main(args: Array[String]): Unit = {def decorator(l:List[In ...

  4. Scala中没有break和continue, 如何退出循环

    Java是指令式风格,Scala是函数式风格. 在Scala中,应该尽量适用循环,而是应用函数的方式来处理. Scala并没有提供break和continue语句来退出循环,那么如果我们又确实要怎么办 ...

  5. Scala中Manifest、ClassTag、TypeTag的学习

    2019独角兽企业重金招聘Python工程师标准>>> Manifest介绍 Manifest是scala2.8引入的一个特质,用于编译器在运行时也能获取泛型类型的信息. 在JVM上 ...

  6. Scala中集合类型与java中集合类型转换

    对于java中的集合元素并不能在scala中拿来就用的,需要进行相应的转换. 1. 转换规则如下 从下面可以看出,有些可以相互转换的,有些只能单向转换: scala.collection.Iterab ...

  7. Scala 中的函数式编程基础(一)

    主要来自 Scala 语言发明人 Martin Odersky 教授的 Coursera 课程 <Functional Programming Principles in Scala>. ...

  8. akka actor java_Akka:使用非默认构造函数在Scala中定义一个actor并从Java代码创建它 - java...

    Akka Scala演员必须扩展akka.actor.Actor Akka Java actor必须扩展akka.actor.UntypedActor 因此,在使用非默认构造函数定义Scala act ...

  9. 在scala中访问postgresql(使用sbt)

    默认已经安装了scala和SBT,并且对sbt有基本了解,知道怎样用sbt建立一个工程. 添加依赖 要在scala中使用postgresql数据库,需要导入postgresql驱动相关的库文件,pos ...

最新文章

  1. 单核工作法19:给创意充电(上)
  2. GDCM:尝试修复损坏的J2K / DICOM的测试程序
  3. BaseRecyclerViewAdapterHelper结合autolayout使用
  4. PHP多种序列化/反序列化的方法 (转载)
  5. Android媒体解码MediaCodec,MediaExtractor
  6. 案例:使用正则表达式的爬虫
  7. 红橙Darren视频笔记 自定义RatingBar touch事件学习 dp转px listener监听
  8. 苹果年底推出搭载M2芯片MacBook Air和MacBook Pro
  9. 斜杠的意思是或还是和_央视网评丨“斜杠老师”,可别顾了赚钱丢了主业
  10. Hive 大数据表性能调优
  11. html图像区域映射菜鸟,HTML area 标签 | w3cschool菜鸟教程
  12. 如果看到消息“此计算机无法读取您插入的磁盘”,该怎么办?
  13. hex2bin和bin2hex
  14. python下载bt文件_使用libtorrent-python下载Torrent
  15. UCOS 杂项 笔记
  16. Excel引用函数(1):FORMULATEXT,取得单元格公式内容
  17. 红米2 手机root
  18. 机器学习中SVM的损失函数,向量积
  19. 网红“骗粉”新套路:假装在底层
  20. 椭圆曲线密码算术(ECC)原理

热门文章

  1. RFID固定资产管理降低人工成本,实现智能化的管理-新导智能
  2. 简易计算器(C语言实现)
  3. 【JavaScript】时间日期,月日小于10的前面补0(新方法padStart)
  4. Matlab绘图常用设置及函数
  5. 洛谷B2058 奥运奖牌计数
  6. ROOT/RStringView.hxx:32:37: error: ‘experimental’ in namespace ‘std’ does not name a type
  7. asp.net mvc 5 identity 2.0 注册时密码强度验证
  8. 【编码译码】基于matlab QC-LDPC码编码和译码【含Matlab译码 2194期】
  9. 基于 Fortran QuickWin 的物性计算应用程序开发示例
  10. 通信原理 | 波段的划分