title: Kotlin combining 'N' lists together by summing each element
body: <p>I'm attempting to combine N lists together by summing each individual element together and outputting one final list with the result. Here's a visualization:</p>
  
  <pre><code>List 1: { 1, 2, 3 }
  List 2: { 4, 5, 6 }
  List 3: { 7, 8, 9 }
  ...
  List N: { X, X, X }
  </code></pre>
  
  <p>If we combined the first three, the output would be:</p>
  
  <pre><code>List Result: { 12, 15, 18 }
  </code></pre>
  
  <p><strong>I'd like to continue summing the elements at each index, cleanly, inline, with optimal performance.</strong></p>
  
  <p>I know the zip operator would help here (easy enough to do list1.zip(list2) and add pair.first and pair.second) but it doesn't handle more than one list.</p>
  
  <p>I educated myself on the Kotlin collection operations to try to find a solution - I looked into using fold, reduce, flatMap, zip, groupBy, and possibly transforming the List into a sequence to take advantage of the each-element aspect. But I can't seem to find a clean way to chain these operations together for a satisfying result.</p>
  
  <p>ADDITIONAL INFO:</p>
  
  <p>What I have now is an extension method called padStart (hidden for brevity) to help ensure all my nested lists are the same length, and then I'm clumsily creating a temporary list that I'm adding values to as I iterate:</p>
  
  <pre><code>myNestedLists
      .run {
          // Calculate largest list size of 'n' lists
          val largestSize = map { it.size }.max() ?: 0 
          // Pad lists with zeroes if size &lt; largestSize
          map { it.padStart(largestSize, 0.0) }
      }
      .run {
          // Create temporary list, add all of the first elements to it
          val combinedList: MutableList&lt;Double&gt; = mutableListOf()
  
          combinedList.addAll(this.first())
  
          // Now, for each nested list, add each individual element with the combined value at combinedList[index]
          drop(1).forEach {
              it.forEachIndexed { index, value -&gt;
                  combinedList[index] += value
              }
          }
  
          // Return the final result
          combinedList
      }
  </code></pre>
  
  <p>This solution works, but it's hard to read and not very clean. I'm looking for a better one!</p>