Guest User

Untitled

a guest
May 16th, 2012
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.68 KB | None | 0 0
  1.  
  2. def groupFreq[A,B](xs: Iterator[A], f: A => B): HashMap[B, Int] = {
  3.     var freqMap = new HashMap[B, Int]
  4.     for (x <- xs) freqMap = freqMap + (f(x)->(freqMap.getOrElse(f(x),0)+1))  
  5.     freqMap
  6.   }
  7.  
  8. def sizeFreq(file: String): HashMap[Int, Int] = {
  9.     def f(x: String) = x.length
  10.     groupFreq[String,Int](Lines.iterator(file),f)
  11.   }
  12.  
  13. //helper
  14. object Lines {
  15.  
  16.   def iterator(file: String) : Iterator[String] = {
  17.     val f = new File(file)
  18.     scala.io.Source.fromFile(f).getLines()
  19.   }
  20.  
  21.   def list(file: String) : List[String] = {
  22.     val buf   = scala.io.Source.fromFile(new File(file))
  23.     val lines = buf.getLines().toList
  24.     buf.close()
  25.     lines
  26.   }
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment