我是新来的斯卡拉,我想弄清楚一些scala语法。斯卡拉地图和/或groupby函数
所以我有一个字符串列表。
wordList: List[String] = List("this", "is", "a", "test")
我有一个返回一个包含每个单词的辅音和元音数对的列表功能:
def countFunction(words: List[String]): List[(String, Int)]
因此,举例来说:
countFunction(List("test")) => List(('Consonants', 3), ('Vowels', 1))
我现在想取单词清单并按计数签名分组:
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")
我想我需要使用的GroupBy做到这一点:
def mapFunction(words: List[String]): Map[List[(String, Int)], List[String]] = {
words.groupBy(F: (A) => K)
}
我读过的Map.GroupBy斯卡拉API和看到,F表示鉴别功能和K是你想要返回键的类型。所以我试过这个:
words.groupBy(countFunction => List[(String, Int)]
但是,scala不喜欢这种语法。我试着查找一些groupBy的例子,似乎没有任何东西可以帮助我处理用例。有任何想法吗?
这很好。我创建了第二个函数,只需要一个单词。我现在的其他功能只是调用这个单个字符串函数。谢谢您的帮助。 – user1772790