virtualeyes
virtualeyes

Reputation: 11237

multi-assignment on a Seq[Tuple2]

Flailing around here, seems simple.

Have a Seq[Tuple2[A,B]], call it foo, and I'd like to extract the Tuple2 into a (Seq[A],Seq[B]) that I can do a one stop shop multi-assignment on.

val(a,b) = foo ??

Tried map, flatmap and other variations of fail.

Shed the light if you will ;-)

Upvotes: 0

Views: 116

Answers (1)

Dylan
Dylan

Reputation: 13922

Try unzip.

The docs specify it as

def unzip[A1, A2](implicit asPair: (A) ⇒ (A1, A2)): (Seq[A1], Seq[A2])

So you can just say val (a, b) = foo.unzip

To go the other way (from x: Seq[A] and y: Seq[B] to z: Seq[(A,B)]), you can use val z = x.zip(y).

Upvotes: 7

Related Questions