I was browsing around and found a question about grouping a String
by it's characters, such as this:
The input:
"aaabbbccccdd"
Would produce the following output:
"aaa"
"bbb"
"cccc"
"ddd"
and I found this suggestion:
val str = "aaabbbccccdd"[
val list = str.groupBy(identity).toList.sortBy(_._1).map(_._2)
And this identity
fellow got me curious. I found out it is defined in PreDef
like this:
identity[A](x: A): A
So basically it returns whatever it is given, right? but how does that apply in the call to groupBy
?
I'm sorry if this is a basic question, is just that functional programming is still tangling my brains a little. Please let me know if there's any information I can give to make this question clearer