Sha256: 50d8eddb0b39f4be97179ca5a59c19016876f0d129af48d5726625e39e0f5afc
Contents?: true
Size: 1.46 KB
Versions: 396
Compression:
Stored size: 1.46 KB
Contents
import scala.concurrent._ import scala.concurrent.duration.Duration import ExecutionContext.Implicits.global object Frequency { // get the char counts for one String private def countChars(text: String): Map[Char, Int] = text.filter(_.isLetter).groupBy(_.toLower).map{case (c, s) => (c, s.length)} // get the char counts for a Seq of String private def countChars(texts: Seq[String]): Map[Char, Int] = texts.map(s => countChars(s)).foldLeft(Map[Char, Int]())((textMap, acc) => merge(acc, textMap)) // Merges two char count maps into a single map private def merge(map1: Map[Char, Int], map2: Map[Char, Int]) = map1 ++ map2.map{ case (k,v) => k -> (v + map1.getOrElse(k,0)) } def frequency(numWorkers: Int, texts: Seq[String]): Map[Char, Int] = { require(numWorkers > 0) val chunkSize = Math.ceil(texts.size / (numWorkers * 1.0)).asInstanceOf[Int] if (chunkSize == 0) return Map() // Create a list of Futures. Each Future is to process a sequence of chunkSize // texts. val futures: Seq[Future[Map[Char, Int]]] = texts.grouped(chunkSize).map(textsChunk => future {countChars(textsChunk)}).toSeq val sequence: Future[Seq[Map[Char, Int]]] = Future.sequence(futures) // Wait for the results val futuresResult: Seq[Map[Char, Int]] = Await.result(Future.sequence(futures), Duration.Inf) // Aggregate the results from each Future futuresResult.foldLeft(Map[Char,Int]())((textMap, acc) => merge(acc, textMap)) } }
Version data entries
396 entries across 396 versions & 1 rubygems