Created
February 11, 2013 23:41
-
-
Save andrewconner/4758686 to your computer and use it in GitHub Desktop.
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 Translator { | |
def translate(input: String): String | |
} | |
class FrenchTranslatorImpl extends Translator { | |
val wordReplacements = Map( | |
"hello" -> "bonjour", | |
"hi" -> "salut", | |
"greetings" -> "salutations" | |
) | |
def translate(input: String): String = { | |
input.split("""\s+""").map(word => wordReplacements.get(word.toLowerCase).map(newWord => word match { | |
case _ if word(0).isUpper => newWord.capitalize | |
case _ => newWord | |
}).getOrElse(word)).mkString(" ") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment