Created
January 20, 2012 20:03
-
-
Save devnoo/1649297 to your computer and use it in GitHub Desktop.
seven languages in seven weeks scala day 2
This file contains hidden or 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 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")) |
This file contains hidden or 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 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")) |
This file contains hidden or 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
| 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