Can someone help me write the for comprehension to collect the values rather then printing them.
I have a data structure which is a Map of Map of Map. When I try to use a for
comprehension, I don’t understand the compiler errors. I can use the for
comprehension to print (with println
) the values I want to collect, but I can’t collect them with yield
I have prepared a ScalaFiddle exhibiting the problem. But here is an explanation for posterity.
val h:HashMap[Int,HashMap[Int,HashMap[List[Int],Set[List[Int]]]]] = someaccessor()
I understand that h(3)
gives me a HashMap (or exception), and h.get(3)
gives me an Option
.
Rather than using nested pattern matches, I would like to use a for
comprehension, but I get a bit lost.
When I use the following for
comprehension to print, it works fine.
val vec = QmVec(List(List(1,2,3),List(-1,2,3),List(1,-2,3),List(1,2),List(-1,2)))
vec.hash.get(2)
vec.hash.get(2).get(3)
vec.hash.get(2).get(3).get(List(1,2,3)) // Set of Lists of length 3 whose absval are (1,2,3) and exactly 2 of which are positive
for{
x <- vec.hash.get(2)
y <- x.get(3)
(k,v) <- y
i <- v
} println(s"k=$k v=$v i=$i")
This prints the following, which is what I expect. The values of i
are printed, correctly.
import dimacs.QmVec
import dimacs.dimacsSimplify._
vec: dimacs.QmVec = dimacs.QmVec@5e7422ef
res0: Option[scala.collection.mutable.HashMap[Int,scala.collection.mutable.HashMap[dimacs.dimacsSimplify.Clause,Set[dimacs.dimacsSimplify.Clause]]]] = Some(Map(2 -> Map(List(1, 2) -> Set(List(1, 2))), 3 -> Map(List(1, 2, 3) -> Set(List(-1, 2, 3), List(1, -2, 3)))))
res1: scala.collection.mutable.HashMap[dimacs.dimacsSimplify.Clause,Set[dimacs.dimacsSimplify.Clause]] = Map(List(1, 2, 3) -> Set(List(-1, 2, 3), List(1, -2, 3)))
res2: Option[Set[dimacs.dimacsSimplify.Clause]] = Some(Set(List(-1, 2, 3), List(1, -2, 3)))
k=List(1, 2, 3) v=Set(List(-1, 2, 3), List(1, -2, 3)) i=List(-1, 2, 3)
k=List(1, 2, 3) v=Set(List(-1, 2, 3), List(1, -2, 3)) i=List(1, -2, 3)
Now I want to modify the for
comprehension so that it collects the values of i
into a Set
rather than printing them, so I modify the code as follows.
for{
x <- vec.hash.get(2)
y <- x.get(3) /// flagged in red
(k,v) <- y /// flagged in red
i <- v
} yield i
But in this case I get compiler errors.
found : scala.collection.mutable.Iterable[dimacs.dimacsSimplify.Clause]
(which expands to) scala.collection.mutable.Iterable[List[Int]]
required: Option[?]
(k,v) <- y