Last active
August 29, 2015 14:22
-
-
Save rbobillot/2ddbb16d14bb4d114ad1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Scala is kind of a coffee maker !