This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* non-performant solution to https://twitter.com/Al_Grigor/status/1357028887209902088 | |
* scala> partitionCount("aaaabbbcca") | |
* res0: List[(Char, Int)] = List((a,4), (b,3), (c,2), (a,1)) | |
* PS see "stringly" for a better solution in clojure. | |
* Is there a scala equivalent to this? Feel free to comment. | |
* (->> (partition-by identity s) (map frequencies) seq)) | |
*/ | |
def partitionCount(s: String): List[(Char, Int)] = { | |
s.toList.foldLeft(List.empty[(Char, Int)]) { (agg, c) => |
OlderNewer