Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.collection.mutable
- import scala.collection.mutable
- def wordCounter(text: String): scala.collection.mutable.Map[String,Int] = {
- new mutable.HashMap[String,Int] ++ text.trim.split("\\s+").groupBy(x => x).mapValues(_.length)
- }
- def wCount(text: String): scala.collection.mutable.HashMap[String, Int] = {
- mutable.HashMap() ++= text.trim.split("\\s+").map(txt => (txt, txt.length)).toMap
- }
- val s: String = "dupa guwno cipa huj"
- wordCounter(s)
- wCount(s)
- //-------------
- wordCounter: wordCounter[](val text: String) => scala.collection.mutable.Map[String,Int]
- wCount: wCount[](val text: String) => scala.collection.mutable.HashMap[String,Int]
- s: String = dupa guwno cipa huj
- res0: scala.collection.mutable.Map[String,Int] = Map(dupa -> 1, guwno -> 1, huj -> 1, cipa -> 1)
- res1: scala.collection.mutable.HashMap[String,Int] = Map(guwno -> 5, dupa -> 4, huj -> 3, cipa -> 4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement