Advertisement
Guest User

Untitled

a guest
Jan 5th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.90 KB | None | 0 0
  1. import scala.collection.mutable
  2.  
  3. import scala.collection.mutable
  4.  
  5. def wordCounter(text: String): scala.collection.mutable.Map[String,Int] = {
  6.   new mutable.HashMap[String,Int] ++ text.trim.split("\\s+").groupBy(x => x).mapValues(_.length)
  7. }
  8.  
  9. def wCount(text: String): scala.collection.mutable.HashMap[String, Int] = {
  10.   mutable.HashMap() ++= text.trim.split("\\s+").map(txt => (txt, txt.length)).toMap
  11. }
  12.  
  13. val s: String = "dupa guwno cipa huj"
  14. wordCounter(s)
  15. wCount(s)
  16.  
  17.  
  18. //-------------
  19. wordCounter: wordCounter[](val text: String) => scala.collection.mutable.Map[String,Int]
  20.  
  21.  
  22.  
  23. wCount: wCount[](val text: String) => scala.collection.mutable.HashMap[String,Int]
  24.  
  25.  
  26.  
  27. s: String = dupa guwno cipa huj
  28. res0: scala.collection.mutable.Map[String,Int] = Map(dupa -> 1, guwno -> 1, huj -> 1, cipa -> 1)
  29. 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