Skip to content

Instantly share code, notes, and snippets.

@brikis98
Created March 19, 2012 07:01
Show Gist options
  • Select an option

  • Save brikis98/2099982 to your computer and use it in GitHub Desktop.

Select an option

Save brikis98/2099982 to your computer and use it in GitHub Desktop.
Seven Languages in Seven Weeks: Scala, Day 2
import collection.mutable.HashMap
trait Censor {
val curseWords = new HashMap[String, String]()
io.Source.fromFile("censor.txt").getLines().foreach { (line) =>
val parts = line.split(": ")
curseWords += parts(0) -> parts(1)
}
def censor(s: String) = curseWords.foldLeft(s)((prev, curr) => prev.replaceAll(curr._1, curr._2))
}
class Text(s: String) extends Censor {
def value = s
def censoredValue = censor(s)
}
val text = new Text("Shoot, I forgot my Darn traits again")
println("Original String: " + text.value)
println("Censored String: " + text.censoredValue)
Shoot: Pucky
Darn: Beans
val list = List("foo", "bar", "blah")
val totalLength = list.foldLeft(0)(_ + _.length)
println("The total length of " + list + " is " + totalLength)
@brikis98
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment