I’ve been using Scala for a bit over a year now, and I often find myself writing:
seq.groupBy(x => x).view.mapValues(_.length)
Is this the best way to count the number of each item in a Seq? If not, please let me know the better way. However, if this is indeed the best, it would be a kindness to include a method on Seq that computes this for users in one method call, perhaps seq.itemCounts().
Thanks, I suppose I can do this. I run into this problem a lot on various research projects - suppose you are running a simulation, and the simulation returns an outcome, and you want to count how many times each outcome occurred, etc. It is probably less useful for enterprise development. I will make use of this implicit, thanks!