我正在为Martin Odersky博士mentioned over at Skillmatters进行电话簿助记符的代码审查。
以下是他所拥有的片段:
class Coder(words: List[String]) {
private val mnemonics = Map(
'2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ")
/** Invert the mnemonics map to give a map from chars 'A' ... 'Z' to '2' ... '9'*/
private val charCode: Map[Char, Char] =
for ((digit, str) <- mnemonics; letter <- str) yield (letter -> digit)
/** Maps a word to the digit string it can represent */
private def wordCode(word: String): String = word.toUpperCase map charCode
/** A map from digit strings to the words that represent them,
* e,g. 5282 -> Set(Java, Kata, Lava, ...) */
private val wordsForNum: Map[String, List[String]] =
(words groupBy wordCode) withDefaultValue List()
我试图将所有内容声明为函数和变量,以了解withDefaultValue的行为方式,这就是我所得到的:
scala> val words3 = List("moo", "1111")
words3: List[java.lang.String] = List(moo, 1111)
scala> (words3 groupBy wordCode) withDefaultValue List()
java.util.NoSuchElementException: key not found: $
at scala.collection.MapLike$class.default(MapLike.scala:224)
在视频中,他正在谈论如果这个词没有被映射,那么我们应该得到一个项目列表(这从视频中的17分钟开始)。我收到错误了?我正在使用Scala的REPL 2.9.0.1。
感谢您的时间。
编辑:
我很确定我已经在REPL中正确生成了val和函数def。
scala> :paste
// Entering paste mode (ctrl-D to finish)
val mnemonics = Map(
'2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ")
// Exiting paste mode, now interpreting.
mnemonics: scala.collection.immutable.Map[Char,java.lang.String] = Map(8 -> TUV, 4 -> GHI, 9 -> WXYZ, 5 -> JKL, 6 -> MNO, 2 -> ABC, 7 -> PQRS, 3 -> DEF)
scala> :paste
// Entering paste mode (ctrl-D to finish)
val charCode: Map[Char, Char] =
for ((digit, str) <- mnemonics; letter <- str) yield (letter -> digit)
// Exiting paste mode, now interpreting.
charCode: Map[Char,Char] = Map(E -> 3, X -> 9, N -> 6, T -> 8, Y -> 9, J -> 5, U -> 8, F -> 3, A -> 2, M -> 6, I -> 4, G -> 4, V -> 8, Q -> 7, L -> 5, B -> 2, P -> 7, C -> 2, H -> 4, W -> 9, K -> 5, R -> 7, O -> 6, D -> 3, Z -> 9, S -> 7)
def wordCode(word: String): String = word.toUpperCase map charCode
wordCode: (word: String)String
我也尝试在REPL中定义整个类。
defined class Coder
scala> val words4 = List("hi", "Hello world", "$t@r")
words4: List[java.lang.String] = List(hi, Hello world, $t@r)
scala> var listPhoneNumber = new Coder(words4)
java.util.NoSuchElementException: key not found:
at scala.collection.MapLike$class.default(MapLike.scala:224)
scala> val words5 = List("hi","hello","ciao")
words5: List[java.lang.String] = List(hi, hello, ciao)
scala> var listPhoneNumber = new Coder(words5)
listPhoneNumber: Coder = Coder@119c2af
这是我正在使用的整个代码:
class Coder(words: List[String]) {
private val mnemonics = Map(
'2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ")
/** Invert the mnemonics map to give a map from chars 'A' ... 'Z' to '2' ... '9'*/
private val charCode: Map[Char, Char] =
for ((digit, str) <- mnemonics; letter <- str) yield (letter -> digit)
/** Maps a word to the digit string it can represent */
private def wordCode(word: String): String = word.toUpperCase map charCode
/** A map from digit strings to the words that represent them,
* e,g. 5282 -> Set(Java, Kata, Lava, ...) */
private val wordsForNum: Map[String, List[String]] =
(words groupBy wordCode) withDefaultValue List()
/** Return all ways to encode a number as a list of words */
def encode(number: String): Set[List[String]] =
if (number.isEmpty)
Set(List())
else {
for {
splitPoint <- 1 to number.length
word <- wordsForNum(number take splitPoint)
rest <- encode(number drop splitPoint)
} yield word :: rest
}.toSet
/** Maps a number to a list of all word phrases that can represent it */
def translate(number: String): Set[String] = encode(number) map (_ mkString " ")
}
Edit2 - 较小的代码段
val mnemonics = Map(
'2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ")
val charCode: Map[Char, Char] =
for ((digit, str) <- mnemonics; letter <- str) yield (letter -> digit)
def wordCode(word: String): String = word.toUpperCase map charCode
val words = List("Hello", "1111") // doesn't work
(words groupBy wordCode) withDefaultValue List()
val words2 = List("Hello", "Odersky")
(words2 groupBy wordCode) withDefaultValue List() //works
我刚刚注意到,似乎方法/函数wordCode不会接受任何不在Map中的字符。如果是这种情况,那意味着withDefaultValue是没用的吗?如果是这样,那么我猜所呈现的代码有一点缺陷?
答案 0 :(得分:3)
您的最后通知是正确的:只要某个字符不在地图中,函数map charCode
的{{1}}部分就会失败,从而引发异常。因此,您无法访问wordCode
。
如果存在缺陷,则取决于角色不在地图中时的行为。