Is there any way to add a method to multiple classes, or reduce the amount of duplicated code needed to achieve the same.
At the moment I use the following (in this example to add a c method which count repetitions of items in the iterable):
/** Counts each of the distinct items. */
fun <T> Iterable<T>.c() = groupingBy { it }.eachCount()
/** Counts each of the distinct items. */
fun <T> Sequence<T>.c() = groupingBy { it }.eachCount()
/** Counts each of the distinct items. */
fun <T> Array<T>.c() = groupingBy { it }.eachCount()
I am wondering if there is a way to reduce the amount of code, at the moment I repeat that for all of the methods I want to be usable on any of the 3 types.
The only way I can find is to define an interface with the common methods, and then use a class to wrap each of the types with implements the common interface, but this seems like a lot of code for something that might be possible a simpler way.