jdevelop
jdevelop

Reputation: 12296

Scala: join iterators with values in a wrapper

I have the following types: A, B and C, so B <: A and C <: A

I need to get the Iterator[A] that works like this:

What is the best way to do so in Scala?

Upvotes: 0

Views: 77

Answers (1)

Jean Logeart
Jean Logeart

Reputation: 53839

Use pattern matching and flatMap:

val itA: Iterator[A] = // ...
val itB: Iterator[B] = itA.flatMap(a => a match {
  case b: B => Iterator(b)
  case c: C => parse(c)
})

Upvotes: 4

Related Questions