Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def groupFreq[A,B](xs: Iterator[A], f: A => B): HashMap[B, Int] = {
- var freqMap = new HashMap[B, Int]
- for (x <- xs) freqMap = freqMap + (f(x)->(freqMap.getOrElse(f(x),0)+1))
- freqMap
- }
- def sizeFreq(file: String): HashMap[Int, Int] = {
- def f(x: String) = x.length
- groupFreq[String,Int](Lines.iterator(file),f)
- }
- //helper
- object Lines {
- def iterator(file: String) : Iterator[String] = {
- val f = new File(file)
- scala.io.Source.fromFile(f).getLines()
- }
- def list(file: String) : List[String] = {
- val buf = scala.io.Source.fromFile(new File(file))
- val lines = buf.getLines().toList
- buf.close()
- lines
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment