Martin Odersky在Scala Days 2011上发表了主题演讲.在其中,他为Lutz Prechelt在IEEE计算机33中的"七种编程语言的经验比较"一文中使用的着名电话号码短语编码器问题提出了令人印象深刻的解决方案.我试图获取代码从PDF,但结果充满了不间断的空间,很难摆脱.
此外,在给出的解决方案中有一些奇怪的东西,比如可以推断它们时明确提到的类型,以及带有List [String]值的Map被赋予默认值0.它只是一个类; 它不可执行.
有没有人有该示例代码的现成清理版本?
主题演讲视频和幻灯片可在此处获得:
这是 Coder 类的清理版本,以及您像这样执行的 CoderTest 类,第一个参数是要编码的电话号码,其余是字典中的单词:
$ scala CoderTest 7225276257 let us see if scala rocks is in the output
Set(scala rocks)
Run Code Online (Sandbox Code Playgroud)
此输出表示,对于给定的电话号码和字典,找到的唯一编码是单词“scala rock”对。
这是测试驱动程序:
object CoderTest extends App {
val digits::words = args.toList
println( new Coder(words) translate digits )
}
Run Code Online (Sandbox Code Playgroud)
这是 Coder 类本身。我更改了一些标识符和注释,以使它们对我自己更清晰。
class Coder( words: List[String] ) {
// In this code "num" means a string of digits, e.g. "834921".
val digitToLetters = Map(
'2' -> "ABC", '3' -> "DEF" , '4' -> "GHI", '5' -> "JKL",
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ"
)
// Invert digitToLetters to give a map from chars 'A'..'Z' to '2'..'9'.
val letterToDigit =
for ( (digit,itsLetters) <- digitToLetters; letter <- itsLetters )
yield ( letter -> digit )
// Maps a word to the digit string it can represent.
def wordToNum( word: String ) = word.toUpperCase map letterToDigit
// Map from digit string to words in our dictionary that represent it.
// e.g. 5282 -> List( Java, Kata, Lava, ... )
val numToWords = ( words groupBy wordToNum ) withDefaultValue List()
// Maps a digit string to all phrases (lists of dictionary words)
// that represent it.
def numToPhrases( num: String ): Set[ List[String] ] =
if ( num.isEmpty )
Set( List() )
else (
for { splitPoint <- 1 to num.length
word <- numToWords ( num take splitPoint )
phrase <- numToPhrases( num drop splitPoint )
} yield word::phrase
).toSet
// Maps a number to the set of all word phrases that can represent it.
def translate( num: String ) = numToPhrases(num) map ( _ mkString " " )
}
Run Code Online (Sandbox Code Playgroud)
但请记住,指定构成公共 API 一部分的方法的返回类型实际上是一个好主意。现在您可以使用 splitAt 一次性完成取放。