Int 的 Scala 序列

标签 scala int range sequence

我有这样的东西:

    case class FunctionsTest(lowerBound: Int = 1,
                                upperBound: Int = 1000,
                                factor: Int = 2) {
  require(lowerBound < upperBound)

  /**
    * implement a sequence of ints, which start with lowerBound and end with
    * upperbound.
    *
    * for all elements following should be true:
    *
    * xs(i) < xs(i+1)
    * xs(i) + factor == xs(i + 1) (for i > 0 and i <= 1000)
    *
    */
  val xs: Seq[Int] = Seq.range(lowerBound,upperBound +1)

所以我需要这个类的序列来构成这些标准..我用

试过了

Seq.range()

但它为我创建了适合第一个标准的序列,但我不知道现在如何应用评论中提到的第二个标准?

最佳答案

Seq.range[T](start: T, end: T, step)step 参数允许您按因子增加。

scala> Seq.range(1,10,2)
res0: Seq[Int] = List(1, 3, 5, 7, 9)

这两个条件都满足。

scala> res0.zip(res0.tail).forall(t => t._1 < t._2)
res4 Boolean = true

scala> res0(0) + 2 == res0(0 + 1)
res5: Boolean = true

关于Int 的 Scala 序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33426488/

相关文章:

scala - 无法使用 Anorm 读取整数列

c++ - 如何将 24 位(整数值)转换为字符串,反之亦然?

C#从excel文件中读取数据结果###

excel - 如果该行包含基于数字范围的某些值,则将一行从一个工作表复制到另一个工作表

scala - 如何在 Scala 中捕获进程的输出

scala - 在 akka 中测试定时器事件

java - LayoutInflater 类型中的方法 inflate(int, ViewGroup, boolean) 不适用于参数 (int, int, boolean)

使用有符号字符指针更改 int 数组的值

excel - 如何在excel VBA中为具有命名范围的图表设置源数据

java - Java/Scala 中无效的 ZoneDateTime 解析