Skip to content

Instantly share code, notes, and snippets.

@passingloop
Created September 27, 2011 13:41
Show Gist options
  • Save passingloop/1245062 to your computer and use it in GitHub Desktop.
Save passingloop/1245062 to your computer and use it in GitHub Desktop.
7l7w-scala-day2
trait Censor {
val document: List[String]
val dictionary = Map("Shoot" -> "Pucky", "Darn" -> "Beans")
def censor = document.map(word => dictionary.getOrElse(word, word))
}
object sample extends Censor {
val document = List("Gosh", "Darn", "It", "Shit", "Shoot")
}
println(sample.censor)
import scala.io.Source
trait Censor {
val document:List[String]
def dictionary:Map[String, String] = {
val dictionaryFile = Source.fromFile("dictionary.txt")
dictionaryFile.getLines.toList.map(line => line.split(' ')).map(pair => (pair(0), pair(1))).toMap
}
def censor = document.map(word => dictionary.getOrElse(word, word))
}
object sample extends Censor {
val document = List("Gosh", "Darn", "It", "Shit","Shoot")
}
println(sample.censor)
Shoot Pucky
Darn Beans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment