object MorseCode extends App { private val code = Map( ('A', ".- "), ('B', "-... "), ('C', "-.-. "), ('D', "-.. "), ('E', ". "), ('F', "..-. "), ('G', "--. "), ('H', ".... "), ('I', ".. "), ('J', ".--- "), ('K', "-.- "), ('L', ".-.. "), ('M', "-- "), ('N', "-. "), ('O', "--- "), ('P', ".--. "), ('Q', "--.- "), ('R', ".-. "), ('S', "... "), ('T', "- "), ('U', "..- "), ('V', "...- "), ('W', ".- - "), ('X', "-..- "), ('Y', "-.-- "), ('Z', "--.. "), ('0', "----- "), ('1', ".---- "), ('2', "..--- "), ('3', "...-- "), ('4', "....- "), ('5', "..... "), ('6', "-.... "), ('7', "--... "), ('8', "---.. "), ('9', "----. "), ('\'', ".----."), (':', "---... "), (',', "--..-- "), ('-', "-....- "), ('(', "-.--.- "), ('.', ".-.-.- "), ('?', "..--.. "), (';', "-.-.-. "), ('/', "-..-. "), ('-', "..--.- "), (')', "---.. "), ('=', "-...- "), ('@', ".--.-. "), ('"', ".-..-. "), ('+', ".-.-. "), (' ', "/")) // cheat a little private def printMorse(input: String): Unit = { println(input) println(input.trim.replaceAll("[ ]+", " ").toUpperCase .map(code.getOrElse(_, "").trim).mkString(" ")) } printMorse("sos") printMorse(" Hello World!") printMorse("Rosetta Code") }