Convenience functions for sum and product across a Collection type

I regularly find myself wanting to sum all the elements in a Set, Array or other Collections. This made me think whether adding a simple wrapper in the standard library for the sum and product operations, something along the lines of:

collection.reduce(0,+), collection.reduce(1,*)

Can somebody suggest whether this would be a good idea, and if so any advice for creating such an issue? I have assumed here that ordering isn't invariant, which is why I thought of collection. For Collection I was also thinking of creating a function called pairwiseDifferences() -> [Element] for Collection where Element: Numeric. This would iterate through the collection and calculate the pairwise differences for every pair that can be selected. This would make use of the existing combinations(ofCount with an argument of 2.

Do I need to do some more research? If so, what would be good to consider?

2 Likes