import java.io.File
import scala.collection.mutable.HashMap

object Freq {

  def fileContents(file: String): List[String] = {
    val f = new java.io.File(file)
    try {
      scala.io.Source.fromFile(f).getLines().toList
    } catch {
      case e: java.io.FileNotFoundException => {
        println("WTF! No such file exists")
        List()
      }
    }
  }

  def fileContentz(file: String): Iterator[String] = {
    val f = new java.io.File(file)
    try {
      scala.io.Source.fromFile(f).getLines()
    } catch {
      case e: java.io.FileNotFoundException => {
        println("WTF! No such file exists")
        Iterator()
      }
    }
  }

  def fileCharz(file: String): Iterator[Char] = {
    for (line <- fileContentz(file); char <- line) yield char
  }

  def fileChars(file: String): List[Char] = {
    for (line <- fileContents(file); char <- line) yield char
  }

  def freq[A](xs: Iterable[A]): HashMap[A, Int] = {
    val freqMap = new HashMap[A, Int]
    for (x <- xs) {
      freqMap(x) = 1 + freqMap.getOrElse(x, 0)
    }
    freqMap
  }

  def apply[A](xs: Iterable[A]) = freq(xs)

  def show[A](m: HashMap[A, Int], size: Int) = {
    for ((k, v) <- m) { println(k + " : " + "#" * (v / size)) }
  }

  def main(args: Array[String]) {
    val str   = fileChars(args(0))
    val freqs = freq(str)
    show(freqs, 1)
  }

}
