【发布时间】:2021-05-08 14:27:15
【问题描述】:
我刚开始接触 Scala。我发现自己经常使用元组变量。
例如,这是我写的一些代码:
/* Count each letter of a string and return in a list sorted by character
* countLetter("test") = List(('e',1),('s',1),('t',2))
*/
def countLetters(s: String): List[(Char, Int)] = {
val charsListMap = s.toList.groupBy((c:Char) => c)
charsListMap.map(x => (x._1, x._2.length)).toList.sortBy(_._1)
}
Scala 开发人员是否不赞成这种元组语法(x._1、x._2 等)?
【问题讨论】:
-
我认为这是 Scala 开发人员所希望的。 :)