I’ll start the ball rolling.
Given a sequence of elements, some of which can be contained multiple times, one typical requirement is to count them – in the form of a tally or histogram.
The oft-quoted solution is:
ss.groupBy(identity).mapValues(_.size)
So what other solutions exist for similar commonly encountered problems in Scala?
Using
Monoids orNumerics to define sensible operations for rich classes, using implicits if necessary.So then let’s say I have a collection of
Money‘s and I want to sum them:But in order to do this I need to have an implicit
Monoid. But of course, a zero value for aMoneyonly makes sense if I have some currency already!So now Scala will use both of these implicits: