a.moussa a.moussa - 8 days ago 5
Scala Question

Enqueue to a Scala Queue another Scala Queue

I want to enqueue to a Scala Queue another Scala Queue. Something like:

val q1 = Queue(1,2,3,4)
val q2 = Queue(5,6,7,8)


I want to get a result like:

val q3 = Queue(1,2,3,4,5,6,7,8)


I already try this:

val q3 = (0 until q2.length).map(ind => q1.enqueue(q2(ind)))


But it return an IndexedSeq[Unit] like below:

Vector((), (), (), ())


Do you have any idea?

Answer

What about:

import scala.collection.immutable.Queue
val q1 = Queue(1,2,3,4)
val q2 = Queue(5,6,7,8)
val q3 = q1 ++ q2
q3: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3, 4, 5, 6, 7, 8)

Or in mutable way:

import scala.collection.mutable.Queue
val q1 = Queue(1,2,3,4)
val q2 = Queue(5,6,7,8)
val q3 = q1 ++= q2
q3: q1.type = Queue(1, 2, 3, 4, 5, 6, 7, 8, 5, 6, 7, 8)
Comments