Created
September 27, 2011 13:41
-
-
Save passingloop/1245062 to your computer and use it in GitHub Desktop.
7l7w-scala-day2
This file contains 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 characters
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) |
This file contains 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 characters
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) |
This file contains 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 characters
Shoot Pucky | |
Darn Beans |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://d.hatena.ne.jp/passingloop/20110928/seven_languages_in_seven_weeks_scala_homework