0
votes

I am trying understand some Scala code I was given to debug, and why it does not work as expected. (p.s. newbe alert wrt Scala).

case class ColNmbr(colNmbr: Int)

def getValidColumns(m: Matrix): List[ColNmbr] = {
    var l1 = matrix.zipWithIndex
    var l2 = l1.filter(tp => !checkCol(tp._1)).map(_._2)
    println("result:" + l2)

    l2
}   

Matrix is just a List of Lists.

The code is supposed to return a list of column indexes of all matrix columns that pass a validity check which returns a boolean. The data is correct as at the println, but there is a type error because l2 is a List[Int] instead of a List[ColNmbr]. I cannot change the case class, so how do I get the types to match?

1
Possible duplicate of Return a list of user defined class type Scala. Is it from some currently running Scala online course or something? Or is it just a strange coincidence?Andrey Tyukin
If you want to make minimal adjustments to you code, just replace your map by map(t => ColNmbr(t._2)).Andrey Tyukin
Yeah, can see the similarity, but just coincidence. Prefer my solution anyway. But many thanks - that did the trick.stretcherCase
Where is checkCol and Matrix.zipWithIndex ?sarveshseri
They are all in the same object. "checkCol" is merely a sanity check with a Boolean return.stretcherCase

1 Answers

1
votes
case class ColNmbr(colNmbr: Int)

def getValidColumns(m: Matrix): List[ColNmbr] = {
    var l1 = matrix.zipWithIndex
    var l2 = l1.filter(tp => !checkCol(tp._1)).map(_._2)
    println("result:" + l2)

    l2.map(ColNmbr)
}