Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Sequence of Int

I have something like this:

    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)

So I need a Sequence for this class which is making up these criteria.. I tried it with the

Seq.range()

but it creates me the Sequence which is right for the first criteria but I don't know how to now apply the second criteria mentioned in the comment?

like image 260
bajro Avatar asked Jan 28 '26 14:01

bajro


1 Answers

The step parameter of Seq.range[T](start: T, end: T, step) allows you to increase by factor.

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

This satisfies both criteria.

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

and

scala> res0(0) + 2 == res0(0 + 1)
res5: Boolean = true
like image 81
Brian Avatar answered Jan 31 '26 06:01

Brian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!