I need a more concise way to convert a sequence of tuples into a map of map mappings ... As a signature, I get in the case of Tuple4 :
def tuple4Seq2MapOfMaps[A,B,C,D](seq: Seq[(A,B,C,D)]): Map[A,Map[B,Map[C,D]]]
The following code shows my last ugly code that I used (type A to D arbitrary):
type A = Int type B = Double type C = String type D = Boolean val tupleSeq = Seq[(A,B,C,D)]( (1,1.0D,"a",true), (1,1.0D,"b",true), (1,1.0D,"c",false) ) val x = tupleSeq.groupBy{ _._1 }.map{ case (k,s) => (k,s.map{ x => (x._2,x._3,x._4) }) } val y = x.map{ case (k,s) => (k,s.groupBy{_._1}.map{ case (k,s) => (k,s.map{ x => (x._2,x._3) }) }) } val z = y.map{ case (k1,m) => (k1,m.map{ case (k2,s1) => (k2,s1.groupBy{_._1}.map{ case (k3,s2) => (k3,s2.map{ _._2 }.head) }) }) } val m = z(1)(1.0D) println(m("b"))
Note the use of head in val z .
It would be nice to have a more concise way only for Tuple4 , but also wondering how to generalize this to TupleN (N> = 2).
Is there a good approach in someone's head?
Thanks!