Skip to content

Instantly share code, notes, and snippets.

@rbobillot
Last active August 29, 2015 14:22
Show Gist options
  • Save rbobillot/2ddbb16d14bb4d114ad1 to your computer and use it in GitHub Desktop.
Save rbobillot/2ddbb16d14bb4d114ad1 to your computer and use it in GitHub Desktop.
import scala.io.Source._
try
{
val datas = fromFile( args(0) ).mkString // file's text to a BIG string
.groupBy( x => x ) // group occurrences into strings in a map (Char -> String)
.mapValues( _.size ) // converts map (Char -> Int) => where Int is: String.size
.toList // map to list (preparing it for sorting)
.sortWith( _._2 > _._2 ) // reverse sorting with the 2nd value of each element
datas.foreach {
v => println(
"'" + v._1 + "' -> " + v._2 // char -> occurrences number
+ " (" + v._2.toFloat * datas.size / 100 + ")" // (ratio)
)
}
}
catch
{
case a:java.lang.ArrayIndexOutOfBoundsException => println( "No arg given" )
case f:java.io.FileNotFoundException => println( "File not found" )
}
@rbobillot
Copy link
Author

Scala is kind of a coffee maker !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment