I study scala and iterate over some excrement.
So I need to write a function that returns a list of pairs (Char, Int) given by a string as input. I made it work just by experimenting a bit on the scala worksheet.
My solution gives the correct result, I just wonder if it’s good to do it, or maybe there is a better way.
def countChars(s: String): List[(Char, Int)] = {
s.groupBy(c => c.toLower).flatMap(e => List((e._1, e._2.length))).toList
}
countChars("Green Grass")
This leads to the same result as on the sheet: res3: List [(Char, Int)] = List ((e, 2), (s, 2), (n, 1), (a, 1), (, 1 ), (g, 2), (g, 2))
source
share