Question

Stuck here, trying to convert a List of case class tuples to a tuple of sequences and multi-assign the result.

val items = repo.foo.list // gives me a List[(A,B)]

I can pull off multi-assignment like so:

val(a,b) = (items.map(_._1).toSeq, items.map(_._2).toSeq)

but it would be nicer to do in 1 step, along the lines of:

val(a,b) = repo.foo.list.map{case(a,b) => (a,b)}
Was it helpful?

Solution

I am not sure if I understood the question correctly. Maybe unzip works for what you want?

Here is a link with some examples: http://daily-scala.blogspot.de/2010/03/unzip.html

OTHER TIPS

For a more general case you can look at product-collections. A CollSeqN is both a Seq[TupleN[A1..An]] and a TupleN[Seq[A1..An]]

In your example you could extract the Seqs like so:

items._1
items._2
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top