2011-08-23 62 views
0

我正在做一个代码审查电话簿助记符马丁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。

谢谢你的时间。

编辑:

我敢肯定,我已经产生的VAL和REPL 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", "[email protected]") 
words4: List[java.lang.String] = List(hi, Hello world, [email protected]) 

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 = [email protected] 

这里是整个代码我使用:

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不会采用任何不在地图中的字符。如果是这种情况,这是否意味着withDefaultValue是无用的?如果是这样,那么我认为提出的代码有一点缺陷?

+1

当我用':paste'命令将你的“整个代码”粘贴到REPL中时,它编译时没有错误。我正在使用2.9.1.RC3。你可以试着想出一个行为意外的小例子吗? –

+0

添加了一个较小的片段,谢谢。 – mythicalprogrammer

+1

该代码有错误。他在OSCON 2011上展示的代码已更正。 –

回答

3

你最后的通知是正确的:只要一个字符不在地图中,函数wordCodemap charCode部分将失败,并抛出异常。因此你不会到达wihDefaultPart

如果存在缺陷,则取决于您期望角色不在地图中时的行为。