title: How do I return multiple key-value pairs in Scala using Spark's map transformation?
body: <p>I'm new to Scala and Spark. I'm trying to return multiple key-value pairs during the map transformation. My input data is a simple CSV file.</p>
  
  <pre>
  1, 2, 3
  4, 5, 6
  7, 8, 9
  </pre>
  
  <p>My Scala script looks like the following.</p>
  
  <pre><code>class Key(_i:Integer, _j:Integer) {
   def i = _i
   def j = _j
  }
  class Val(_x:Double, _y:Double) {
   def x = _x
   def y = _y
  }
  val arr = "1,2,3".split(",")
  for(i &lt;- 0 until arr.length) {
   val x = arr(i).toDouble
   for(j &lt;- 0 until arr.length) {
    val y = arr(j).toDouble
    val k = new Key(i, j)
    val v = new Val(x, y)
    //note that i want to return the tuples, (k, v)
   }
  }
  </code></pre>
  
  <p>I want to be able to use the for loop and data structures above to return multiple tuples (k, v). Something similar to the code below.</p>
  
  <pre><code>val file = sc.textFile("/path/to/test.csv")
  file.map(line =&gt; {
   val arr = line.split(",")
   for(i &lt;- 0 until arr.length) {
    val x = arr(i).toDouble
    for(j &lt;- (i+1) until arr.length) {
     val y = arr(j).toDouble
     val k = new Index(i,j)
     val v = new Val(x,y)
     (k,v)
    }
   }
  }).collect //reduceByKey is not there, reduce is there, but not what i want
  </code></pre>
  
  <p>When I copy/paste the code above into the lambda expression (and run on the Scala REPL shell) I get the following error: </p>
  
  <pre>
  error: illegal start of simple expression
  val arr = line.split(",")
  ^
  </pre>
  
  <p>I realize also that I am still stuck in imperative/procedural style programming thinking, so please bear with me (and a newbie at Scala/Spark). </p>