zork
zork

Reputation: 2135

Scala: find diffs in lists with different structure

I need to find diffs and matching elements in two lists of different structure, such asmyBooks and top5books, where first number is a book ID in both lists:

case class Book(id: Int, score: Int)

  val myBooks = Array(
    Book(117, 10),
    Book(34, 5),
    Book(85, 7),
    Book(55, 8),
    Book(21, 3)
  )

  val top5Books = Array(117,356,55,85,11)

Result should be in two lists:

val matched = Array(
    Book(117, 10),     
    Book(85, 7),
    Book(55, 8)
  )

val missed = Array(
    Book(34, 5),
    Book(21, 3)
  )

Can't use diff here because input arrays have different structure. What would be a nice, clean code to do this?

Upvotes: 1

Views: 38

Answers (1)

grotrianster
grotrianster

Reputation: 2468

You can use partition method:

val (matched, missed) = myBooks.partition(book => top5Books.contains(book.id))

Upvotes: 3

Related Questions