Arli  Chokoev
Arli Chokoev

Reputation: 549

Scala for-yield with multiple conditions

I have a bitmap object that is a 3-dimensional array with third dimension equal to 3. I want to split it into 64x64x3 blocks. For this I have the following code snippet:

val tiles: someType = for {

  x <- bitmap.indices by 64
  y <- bitmap(0).indices by 64


  data = for {
    //For all X and Y within one future tile coordinates
    tx <- x until x + 64
    ty <- y until y + 64
  } yield bitmap(tx)(ty)

...
}

In the data for loop yield will cause an ArrayIndexOutOfBoundsException at the last chunk. How can I check, whether x and y don't exceed array borders in this loop? Is it possible to have multiple until conditions for the same variable in the same loop?

Upvotes: 0

Views: 181

Answers (1)

What about this?

val tiles: someType = for {
  x <- bitmap.indices by 64
  y <- bitmap(0).indices by 64

  data = for {
    //For all X and Y within one future tile coordinates
    tx <- x until math.min(x + 64, bitmap.length)
    ty <- y until math.min(y + 64, bitmap(0).length)
  } yield bitmap(tx)(ty)
}

Upvotes: 2

Related Questions