convert List[Tuple2[A,B]] to Tuple2[Seq[A],Seq[B]]
Pergunta
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)}
Solução
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
Outras dicas
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
...
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow