Scala 映射和/或 groupby 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13060090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala map and/or groupby functions
提问by user1772790
I am new to Scala and I am trying to figure out some scala syntax.
我是 Scala 的新手,我正在尝试找出一些 Scala 语法。
So I have a list of strings.
所以我有一个字符串列表。
wordList: List[String] = List("this", "is", "a", "test")
I have a function that returns a list of pairs that contains consonants and vowels counts per word:
我有一个函数返回包含每个单词的辅音和元音计数的对列表:
def countFunction(words: List[String]): List[(String, Int)]
So, for example:
因此,例如:
countFunction(List("test")) => List(('Consonants', 3), ('Vowels', 1))
I now want to take a list of words and group them by count signatures:
我现在想获取一个单词列表并按计数签名将它们分组:
def mapFunction(words: List[String]): Map[List[(String, Int)], List[String]]
//using wordList from above
mapFunction(wordList) => List(('Consonants', 3), ('Vowels', 1)) -> Seq("this", "test")
List(('Consonants', 1), ('Vowels', 1)) -> Seq("is")
List(('Consonants', 0), ('Vowels', 1)) -> Seq("a")
I'm thinking I need to use GroupBy to do this:
我想我需要使用 GroupBy 来做到这一点:
def mapFunction(words: List[String]): Map[List[(String, Int)], List[String]] = {
words.groupBy(F: (A) => K)
}
I've read the scala api for Map.GroupBy and see that F represents discriminator function and K is the type of keys you want returned. So I tried this:
我已经阅读了 Map.GroupBy 的 scala api,看到 F 表示鉴别器函数,而 K 是您想要返回的键类型。所以我试过这个:
words.groupBy(countFunction => List[(String, Int)]
However, scala doesn't like this syntax. I tried looking up some examples for groupBy and nothing seems to help me with my use case. Any ideas?
然而,scala 不喜欢这种语法。我尝试查找 groupBy 的一些示例,但似乎没有什么对我的用例有帮助。有任何想法吗?
回答by huynhjl
Based on your description, your count function should take a word instead of a list of words. I would have defined it like this:
根据您的描述,您的计数函数应该采用一个单词而不是单词列表。我会这样定义它:
def countFunction(words: String): List[(String, Int)]
If you do that you should be able to call words.groupBy(countFunction), which is the same as:
如果你这样做,你应该能够调用words.groupBy(countFunction),这与:
words.groupBy(word => countFunction(word))
If you cannot change the signature of countFunction, then you should be able to call group by like this:
如果您不能更改 的签名countFunction,那么您应该可以像这样调用 group:
words.groupBy(word => countFunction(List(word)))
回答by DaoWen
You shouldn't put the return type of the function in the call. The compiler can figure this out itself. You should just call it like this:
您不应将函数的返回类型放入调用中。编译器可以自己解决这个问题。你应该这样称呼它:
words.groupBy(countFunction)
If that doesn't work, please post your countFunctionimplementation.
如果这不起作用,请发布您的countFunction实现。
Update:
更新:
I tested it in the REPL and this works (note that my countFunctionhas a slightly different signature from yours):
我在 REPL 中对其进行了测试,并且可以正常工作(请注意,我的countFunction签名与您的签名略有不同):
scala> def isVowel(c: Char) = "aeiou".contains(c)
isVowel: (c: Char)Boolean
scala> def isConsonant(c: Char) = ! isVowel(c)
isConsonant: (c: Char)Boolean
scala> def countFunction(s: String) = (('Consonants, s count isConsonant), ('Vowels, s count isVowel))
countFunction: (s: String)((Symbol, Int), (Symbol, Int))
scala> List("this", "is", "a", "test").groupBy(countFunction)
res1: scala.collection.immutable.Map[((Symbol, Int), (Symbol, Int)),List[java.lang.String]] = Map((('Consonants,0),('Vowels,1)) -> List(a), (('Consonants,1),('Vowels,1)) -> List(is), (('Consonants,3),('Vowels,1)) -> List(this, test))
You caninclude the type of the function passed to groupBy, but like I said you don't need it. If you want to pass it in you do it like this:
您可以包含传递给 的函数类型groupBy,但就像我说的那样,您不需要它。如果你想传递它,你可以这样做:
words.groupBy(countFunction: String => ((Symbol, Int), (Symbol, Int)))

