2
\$\begingroup\$

I created a program to get frequency of word in all files in a directory using Future API

import java.io.File
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global


class GetWordCount{

  def inFileCount(source: File):Future[Map[String,Int]]= {

    val futureMap:Future[Map[String,Int]]=Future {
      val wordList = scala.io.Source.fromFile(source.toString).getLines.flatMap(_.split("\\W+")).toList
      val wordMap = wordList.groupBy(a => a)
      wordMap.map { case (k, v) => (k, v.length) }
    }
    futureMap

  }

  def getWordCount(dir: String): Future[Map[String,Int]]= {

    val file = new File(dir)
    require(file.exists() && file.isDirectory)
    Future.sequence(file.listFiles.filter(_.isFile).map { a => inFileCount(a) }.toList) map{
      list => list.fold(Map())((a,b) =>a ++ b.map{ case (k,v) => k-> (v+a.getOrElse(k,0)) })
    }

  }

}

Please review this code and make suggestions.

\$\endgroup\$
  • 1
    \$\begingroup\$ Its really cool, good work \$\endgroup\$ – Aamir Nov 3 '16 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.