title: how to iterate over flatmap in spark
body: <p>I want to run a flatMap inside a for loop in spark like that:</p>
  
  <pre><code>def clusterVar(points: DStream[ArrayBuffer[Double]]): DStream[ArrayBuffer[Double]]={
  var best = new ArrayBuffer[Double]()
  var bestVarianceSum = Double.PositiveInfinity
  for (i &lt;- 0 until numTrials) { 
      val clusters = cluster(points)
      // compute the variance of the current list
      clusters.flatMap(cl =&gt; {
        val varianceSum = score(cl)
        if (varianceSum &lt; bestVarianceSum) { 
          best.appendAll(cl)
          bestVarianceSum = varianceSum
        }
        best
      })
    }
  //return value: clusters:DStream[ArrayBuffer[Double]]
  }
  </code></pre>
  
  <p>but I don't know how to return the cluster dstream since it's created inside the for loop. Any thoughts how to do this?</p>