Last active
December 19, 2015 15:49
-
-
Save sam/5979128 to your computer and use it in GitHub Desktop.
Top Ten Words in a String.
This file contains hidden or 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
object Main extends App { | |
val wordCount = "(\\w+)".r | |
.findAllIn(_:String) | |
.toSeq | |
.groupBy(_.toLowerCase) | |
.mapValues(_.size) | |
.toSeq | |
.sortBy(_._2) | |
.reverse | |
val topTen = wordCount andThen(_.take(10).map(_._1)) | |
println(topTen("My name is Sam, sam is a super great name yo! SAM! Sam. sam")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a tweaked version optimizing out the reverse: