Reputation: 12296
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:
If the current value is of type B
, then return B
If the current value is of type C
, then invoke function parse(c: C) : Iterator[B]
and then use this iterator until it exhausts. Then continue with the value from the "parent" iterator.
What is the best way to do so in Scala?
Upvotes: 0
Views: 77
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