I have the following list of pairs (key,id):
val pairs = List(('a',1), ('a',2), ('b',1), ('b',2))
I need to generate all combinations of pairs when the keys are different so the expected output is:
List(
List(),
List(('a', 1)),
List(('a', 2)),
List(('b', 1)),
List(('a', 1), ('b', 1)),
List(('a', 2), ('b', 1)),
List(('b', 2)),
List(('a', 1), ('b', 2)),
List(('a', 2), ('b', 2))
)
Note (List(('a',1),('a',2))
should Not be part of the output so using Scala List.combinations
is not an option
I currently have the following code:
def subSeq (xs: List[(Char, Int)]): List[(Char,Int)] = {
xs match {
case Nil => List()
case y::ys => {
val eh = xs.filter (c => c._1 == y._1)
val et = xs.filter (c => c._1 != y._1)
for (z: (Char,Int) <- eh) yield z :: subSeq(et)
}
}
}
But I get an error saying List[List[(Char,Int)]] does not match List[(Char,Int)]