If you want to alternate an arbitrary number of sequences in order, you can use something like
implicit class Interleave[T](input: Seq[Seq[T]]) { def interleave: Seq[T] = { input.foldLeft(Seq[Seq[T]]()) { (acc, cur) => if (acc.isEmpty) cur.map { m => Seq(m) } else (acc zip cur).map { case (sequence, m) => sequence :+ m } }.flatten.toVector } }
It may be possible to improve performance on this, especially since toVector exists primarily to convert the stream to something impatient.
Usage looks something like this
Seq(Seq(1,2), Seq(2,3), Seq(3,4)).interleave should be(Seq(1,2,3,2,3,4))
source share