简体   繁体   中英

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:

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. NumericRange is a more generic version of Range that allows one to define a fractional (ie, of type Double ) step.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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