Merging list of uneven length with default value for missing matches

Viewed 36

Im trying to pair up two lists in Scala where non matching pairs should be replaced by a default value, this is what I have so far but thy all fall short in some way.

How do I create List((a,a),(b,empty),(c,c))???

case class Test(id: Option[Int] = None)
val empty = Test()
val a = Test(Some(1))
val b = Test(Some(2))
val c = Test(Some(3))

val cache = List(a,b,c)
val delta = List(a,c)

//Trial 1
val newCache1 = cache.zipAll(delta,empty,empty)

//Tial 2
val newCache2 = for {
  c <- cache
  d <- delta
  if c.id == d.id
} yield (c,d)

//Tial 3
val newCache3 = for {
  c <- cache
  d <- delta
} yield if (c.id == d.id) (c,d) else (c,empty)
1 Answers
Related