title: 4-Tuple Sequence To Map of Maps of Maps
body: <p>I need a more concise way to transform a sequence of tuples into a map of maps of maps... 
  As a signature I get in case of <code>Tuple4</code>:</p>
  
  <pre><code>def tuple4Seq2MapOfMaps[A,B,C,D](seq: Seq[(A,B,C,D)]): Map[A,Map[B,Map[C,D]]]
  </code></pre>
  
  <p>The following code shows my recent <em>ugly</em> code, I stucked with (type <code>A</code> to <code>D</code> arbitrary):</p>
  
  <pre><code>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) =&gt; (k,s.map{ x =&gt; (x._2,x._3,x._4) }) }
  val y = x.map{ case (k,s) =&gt; (k,s.groupBy{_._1}.map{ case (k,s) =&gt; (k,s.map{ x =&gt; (x._2,x._3) }) }) }
  val z = y.map{ case (k1,m) =&gt; (k1,m.map{ case (k2,s1) =&gt; (k2,s1.groupBy{_._1}.map{ case (k3,s2) =&gt; (k3,s2.map{ _._2 }.head) }) }) }
  
  val m = z(1)(1.0D)
  println(m("b"))
  </code></pre>
  
  <p>Note the use of <code>head</code> at <code>val z</code>.</p>
  
  <p>It would be nice to have a more concise way for only <code>Tuple4</code>, but furthermore interesting how to generalize this to <code>TupleN</code> (N >= 2).</p>
  
  <p>Is there a nice approach in someones mind out there?</p>
  
  <p>Thank you!</p>