Scala groupBy of a tuple to calculate stock basis
<p>I am working on an exercise to calculate stock basis given a list of stock purchases in the form of thruples (ticker, qty, stock_price).  I've got it working, but would like to do the calculation part in more of a functional way.  Anyone have an answer for this?</p>

<pre><code>// input: 
// List(("TSLA", 20, 200),
//      ("TSLA", 20, 100),
//      ("FB", 10, 100)
// output:
// List(("FB", (10, 100)), 
//      ("TSLA", (40, 150))))

def generateBasis(trades: Iterable[(String, Int, Int)]) = { 
  val basises = trades groupBy(_._1) map {
    case (key, pairs) =&gt;
      val quantity = pairs.map(_._2).toList
      val price = pairs.map(_._3).toList
      var totalPrice: Int = 0
      for (i &lt;- quantity.indices) {
        totalPrice += quantity(i) * price(i)
      }
      key -&gt; (quantity.sum, totalPrice / quantity.sum)
    }
  basises
}
</code></pre>