我有这样的东西:
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/