functional programming - Scala zip with id on sorted collections -


i have 2 collections in scala objects containing id. , want zip them id. in such example:

case class a(id: long) case class b(id: long)  val col1 = a(1) :: a(2) :: a(5) :: nil val col2 = b(2) :: b(2) :: b(5) :: nil 

i expect result:

list(      (a(1), list()),      (a(2), list(b(2), b(2)),      (a(5), list(b(5))     ) 

how easy way? if know col1 , col2 sorted id somehow?

i can't figure out way without intermediate variable, how this:

val map = col2.groupby(_.id).withdefault(_ => list.empty) col1.map { => -> map(a.id) } 

for 3-element arrays doesn't matter, note main difference other answer linear time.


Comments