I have a vector of vectors that I refer to in order to apply a boolean function. i.e.
Vector[Vector[T]] , where I am going to do something according to f(myVector(i)(j)) where f is of type T => Boolean .
But this does not check the boundaries, and I cannot get something really elegant.
I can use applyOrElse : myVector.applyOrElse(i, (_:Int) => Vector.empty).applyOrElse (j, (_:Int) => defaultT)
where f(defaultT) will return false But I would just like to set the default value instead of a function.
I could use the elevator to give me Option , but it doesn’t work well for the second level: myVector.lift(i) map (_.lift(j) map f getOrElse false) getOrElse false
Which works, but still hard to read.
And then there are the standard if / else blocks:
if (myVector.size <= i) false else { val myVector2 = levelVector(i) if (myVector2.size <= j) false else f(myVector2(j)) }
It just looks like it should be easier to decompose than what I can achieve. And if I add a third layer, it will become even more ugly.
Are there any other options?
Disclaimer: this is adapted from coursera progfun course
scala
Stephen
source share