title: Navigating multidimensional arrays with nested for-loops in Kotlin
body: <p>Quite often I am navigating a two-dimensional image matrix of pixels or a three-dimensional volumetric data set.  Typically the underlying data structure is a one-dimensional array of some primitive type like <code>Double</code> or <code>Int</code>.  The multi-dimensional nature is navigated through by nested for-loops as illustrated below.  </p>
  
  <p>Given that Kotlin does not currently support the typically C and Java-like for-loop structure, there seems some lost flexibility.  I'm wondering if there is a clever iterator or Kotlin syntax to make this more elegant.</p>
  
  <p>In Java below, we have the ability to capture the whole iteration concisely by embedding the master index <code>i</code> into the for-loops and only involving implicit increment operations which are presumably computationally efficient (compared to division and modulo)...</p>
  
  <pre><code>public static void main(String[] args) {
      int nrow=1000, ncol=1000;
      int size=nrow*ncol;
      double[] data = new double[size];
  
      for (int i=0, r = 0; r &lt; nrow; r++) {
          for (int c = 0; c &lt; ncol; c++,i++) {
              data[i]= r*c;
          }
      }
  }
  </code></pre>
  
  <p>In Kotlin as follows, I find a solution where the index <code>i</code> is forced to have scope outside the loop block.  Also the <code>i++</code> line is somewhat buried and separated from the loop structure.  I'm missing in this situation the elegant Kotlin syntax such as 'builders', no semi-colons, etc. I humbly submit that this is because the for-loop flow-control structure is less expressive in Kotlin.  Admittedly, this is not critical, but more of a disappointment.</p>
  
  <pre><code>fun main(args : Array&lt;String&gt;) {
      val nrow = 1000
      val ncol = 1000
      val size = nrow * ncol
      val data = DoubleArray(size)
  
      var i = 0
      for(r in 0 until nrow) {
          for(c in 0 until ncol) {
              data[i] = (r * c).toDouble()
              i++
          }
      }
  }
  </code></pre>