mitrakov

Merging maps with Semigroups

Jan 3rd, 2021
912
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import scala.io.Source
  2. import cats.implicits._
  3.  
  4. object Temp extends App {
  5.   val ru = load("/home/mitrakov/buffer/ru.txt")
  6.   val en = load("/home/mitrakov/buffer/en.txt")
  7.   val pt = load("/home/mitrakov/buffer/pt.txt")
  8.  
  9.   val all = ru.mapValues(List(_)) |+| en.mapValues(List(_)) |+| pt.mapValues(List(_))
  10.   val result = all.mapValues(_.size).toList.sortBy(_._2)
  11.  
  12.   result foreach println
  13.  
  14.   def load(filename: String): Map[String, String] = {
  15.     Source.fromFile(filename).getLines().map { line =>
  16.       line.trim.split(":").toList match {
  17.         case head :: tail => head -> tail.mkString(":")
  18.       }
  19.     }.toMap
  20.   }
  21. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×