title: Iterating through files in scala to create values based on the file names
body: <p>I think there may be a simple solution to this, I was wondering if anybody knew how to iterate over a set of files and output a value based on the files name.</p>
  
  <p>My problem is, I want to read in a set of graph edges for each month, and then create a seperate monthly graphs.</p>
  
  <p>Currently I've done this the long way, which is fine for doing one years worth, but I'd like a way to automate it.</p>
  
  <p>You can see my code below which hopefully clearly shows what I am doing.</p>
  
  <pre><code>//Load vertex data
  val vertices= (sc.textFile("D:~vertices.csv")
    .map(line =&gt; line.split(",")).map(parts =&gt; (parts.head.toLong, parts.tail)))
  
  //Define function for creating edges from csv file
  def EdgeMaker(file: RDD[String]): RDD[Edge[String]] = {
    file.flatMap { line =&gt;
      if (!line.isEmpty &amp;&amp; line(0) != '#') {
        val lineArray = line.split(",")
        if (lineArray.length &lt; 0) {
          None
        } else {
          val srcId = lineArray(0).toInt
          val dstId = lineArray(1).toInt
          val ID = lineArray(2).toString
          (Array(Edge(srcId.toInt, dstId.toInt, ID)))
        }
      } else {
        None
      }
    }
  }
  
  //make graphs -This is where I want automation, so I can iterate through a 
  //folder of edge files and output corresponding monthly graphs. 
  val edgesJan = EdgeMaker(sc.textFile("D:~edges2011Jan.txt"))
  val graphJan = Graph(vertices, edgesJan)
  val edgesFeb = EdgeMaker(sc.textFile("D:~edges2011Feb.txt"))
  val graphFeb = Graph(vertices, edgesFeb)
  val edgesMar = EdgeMaker(sc.textFile("D:~edges2011Mar.txt"))
  val graphMar = Graph(vertices, edgesMar)
  val edgesApr = EdgeMaker(sc.textFile("D:~edges2011Apr.txt"))
  val graphApr = Graph(vertices, edgesApr)
  val edgesMay = EdgeMaker(sc.textFile("D:~edges2011May.txt"))
  val graphMay = Graph(vertices, edgesMay)
  val edgesJun = EdgeMaker(sc.textFile("D:~edges2011Jun.txt"))
  val graphJun = Graph(vertices, edgesJun)
  val edgesJul = EdgeMaker(sc.textFile("D:~edges2011Jul.txt"))
  val graphJul = Graph(vertices, edgesJul)
  val edgesAug = EdgeMaker(sc.textFile("D:~edges2011Aug.txt"))
  val graphAug = Graph(vertices, edgesAug)
  val edgesSep = EdgeMaker(sc.textFile("D:~edges2011Sep.txt"))
  val graphSep = Graph(vertices, edgesSep)
  val edgesOct = EdgeMaker(sc.textFile("D:~edges2011Oct.txt"))
  val graphOct = Graph(vertices, edgesOct)
  val edgesNov = EdgeMaker(sc.textFile("D:~edges2011Nov.txt"))
  val graphNov = Graph(vertices, edgesNov)
  val edgesDec = EdgeMaker(sc.textFile("D:~edges2011Dec.txt"))
  val graphDec = Graph(vertices, edgesDec)
  </code></pre>
  
  <p>Any help or pointers on this would be much appreciated.</p>