Last active
July 1, 2019 14:33
-
-
Save WizardOfArc/fd8c81c2b4ac35653cc251f19097f8a2 to your computer and use it in GitHub Desktop.
Revisions
-
WizardOfArc revised this gist
Jul 1, 2019 . 1 changed file with 1 addition and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -24,8 +24,5 @@ object WordCounter extends App { if(accum.contains(h)){ accum + (h -> (accum(h) + 1)) } else { accum + (h -> 1) } }.toMap def padOnRight(width: Int)(word: String): String = (0 to (width - word.length)).foldLeft(word)((a: String, _: Int) => a + " ") } -
WizardOfArc revised this gist
May 15, 2019 . 1 changed file with 1 addition and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -21,11 +21,7 @@ object WordCounter extends App { def collectWords(words: List[String]): Map[String, Int] = words.foldLeft(MMap[String, Int]()){ case (accum, h) => if(accum.contains(h)){ accum + (h -> (accum(h) + 1)) } else { accum + (h -> 1) } }.toMap def padOnRight(width: Int)(word: String): String = { -
WizardOfArc revised this gist
May 15, 2019 . 1 changed file with 9 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,6 @@ import scala.io.Source import scala.annotation.tailrec import scala.collection.mutable.{Map -> MMap} object WordCounter extends App { if(args.length != 1){ @@ -18,17 +19,14 @@ object WordCounter extends App { wordCount.toList.sortWith(_._2 > _._2).map{case (k,v) => s"${padFunc(k)} | $v"} foreach println } def collectWords(words: List[String]): Map[String, Int] = words.foldLeft(MMap[String, Int]()){ case (accum, h) => if(accum.contains(h)){ accum + (h -> (accum(h) + 1)) } else { accum + (h -> 1) } }.toMap def padOnRight(width: Int)(word: String): String = { val padding = (0 to (width - word.length)).foldLeft("")((a: String, _: Int) => a + " ") -
WizardOfArc revised this gist
May 15, 2019 . No changes.There are no files selected for viewing
-
WizardOfArc created this gist
May 15, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ import scala.io.Source import scala.annotation.tailrec object WordCounter extends App { if(args.length != 1){ println("Give me a file to investigate") } else { val wordList = Source.fromFile(args(0)).getLines .flatMap(_.split("[()<>''+-=!|/ .,:{}\"]")) .flatMap(_.split(raw"\\")) .flatMap(_.split(raw"\[")) .flatMap(_.split(raw"\]")) .flatMap(_.split(raw"\s")) .filter(_ != "") .toList val padFunc = padOnRight(wordList.map(_.length).max)(_) val wordCount = collectWords(wordList) wordCount.toList.sortWith(_._2 > _._2).map{case (k,v) => s"${padFunc(k)} | $v"} foreach println } def collectWords(words: List[String]): Map[String, Int] = { @tailrec def countThem(wordList: List[String], accum: scala.collection.mutable.Map[String, Int]):Map[String,Int] = wordList match { case Nil => accum.toMap case h::t => if(accum.contains(h)){ accum += (h -> (accum(h) + 1)) } else { accum += (h -> 1) } countThem(t, accum) } countThem(words, scala.collection.mutable.Map[String, Int]()) } def padOnRight(width: Int)(word: String): String = { val padding = (0 to (width - word.length)).foldLeft("")((a: String, _: Int) => a + " ") word + padding } }