Skip to content

Instantly share code, notes, and snippets.

@devnoo
Created January 20, 2012 20:03
Show Gist options
  • Select an option

  • Save devnoo/1649297 to your computer and use it in GitHub Desktop.

Select an option

Save devnoo/1649297 to your computer and use it in GitHub Desktop.
seven languages in seven weeks scala day 2
trait Censor {
val censoredWords = Map("shoot" -> "pucky", "darn" -> "beans")
def censor(text : String) : String = {
censoredWords.foldLeft(text)((sum, value) => sum.replaceAll(value._1, value._2))
}
}
class CensoredText extends Object with Censor {
}
val censor = new CensoredText();
print (censor.censor("shoot the darn thing, or I'll shoot you darn thing"))
import collection.mutable.HashMap
import collection.parallel.mutable
import io.Source
trait Censor {
val censoredWords = Source.fromFile("censored-words.txt").getLines()
.foldLeft(new HashMap[String, String])((map, line) => {
val pair = line.split(':')
map += pair(0) -> pair(1)
}).toMap
def censor(text : String) : String = {
censoredWords.foldLeft(text)((sum, value) => sum.replaceAll(value._1, value._2))
}
}
class CensoredText extends Object with Censor {
}
val censor = new CensoredText();
print (censor.censor("shoot the darn thing, or I'll shoot you darn thing"))
val list = List("1", "23", "45", "678", "90");
assert( 10 == list.foldLeft(0)((sum, value) => sum + value.length()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment