Skip to content

Instantly share code, notes, and snippets.

@WizardOfArc
Last active July 1, 2019 14:33
Show Gist options
  • Select an option

  • Save WizardOfArc/fd8c81c2b4ac35653cc251f19097f8a2 to your computer and use it in GitHub Desktop.

Select an option

Save WizardOfArc/fd8c81c2b4ac35653cc251f19097f8a2 to your computer and use it in GitHub Desktop.

Revisions

  1. WizardOfArc revised this gist Jul 1, 2019. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions wordCount.scala
    Original 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 = {
    val padding = (0 to (width - word.length)).foldLeft("")((a: String, _: Int) => a + " ")
    word + padding
    }
    def padOnRight(width: Int)(word: String): String = (0 to (width - word.length)).foldLeft(word)((a: String, _: Int) => a + " ")
    }
  2. WizardOfArc revised this gist May 15, 2019. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions wordCount.scala
    Original 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)
    }
    if(accum.contains(h)){ accum + (h -> (accum(h) + 1)) } else { accum + (h -> 1) }
    }.toMap

    def padOnRight(width: Int)(word: String): String = {
  3. WizardOfArc revised this gist May 15, 2019. 1 changed file with 9 additions and 11 deletions.
    20 changes: 9 additions & 11 deletions wordCount.scala
    Original 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] = {
    @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 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 + " ")
  4. WizardOfArc revised this gist May 15, 2019. No changes.
  5. WizardOfArc created this gist May 15, 2019.
    37 changes: 37 additions & 0 deletions wordCount.scala
    Original 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
    }
    }