简体   繁体   English

是否存在一个带有小数步长的Scala“范围”方法(类似于Python)?

[英]Is there a Scala “range” method with fractional step size (similar to Python)?

This works: 这有效:

scala> 0 to 24 by 5
res16: scala.collection.immutable.Range = Range(0, 5, 10, 15, 20)

Negative integer works, too: 负整数也适用:

scala> 0 to 24 by -1
res17: scala.collection.immutable.Range = Range()

Demands an integer: 需要一个整数:

scala> 0 to 24 by 0.5

<console>:40: error: type mismatch;
 found   : Double(0.5)
 required: Int
              0 to 24 by 0.5
                         ^

Explicitly define a range of Double s: 明确定义Double的范围:

scala> 0.0 to 24.0 by 0.5
// res0: scala.collection.immutable.NumericRange[Double] = NumericRange(0.0, 0.5, 1.0, ...)

A Range works with Int only. Range仅适用于Int NumericRange is a more generic version of Range that allows one to define a fractional (ie, of type Double ) step. NumericRangeRange一种更通用的版本,它允许您定义分数(即Double类型)步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM