0

I need check sequence contains another sequence, but based on one field only:

case class Test(f1: Int, f2: String)

val seq = Seq(Test(1, "a"), Test(2, "b"), Test(3, "a"), Test(4, "c"))

Now i want to have something like(theoretical not working code):

seq.containsSlice(Seq(Test(2, _), Test(3, _))) shouldBe true

Where second field can be any.

My idea - create another class, based on Test, where equals/hashcode uses only one field, and convert my Seq[Test] to Seq[TestWithOneField]. May be there is more elegant and universal solution?

zella
  • 4,645
  • 6
  • 35
  • 60
  • Maybe [this](http://stackoverflow.com/questions/12739432/how-to-implement-a-set-with-a-user-defined-equality) can hint you towards a more "universal" solution to custom equality. – Yuval Itzchakov Jan 25 '17 at 08:21

1 Answers1

0

You could map the sequence to obtain a sequence only of the fields you are checking (in this case a sequence of the integers in the f1 field):

seq.map(_.f1).containsSlice(Seq(2,3))
lex82
  • 11,173
  • 2
  • 44
  • 69