Hi there,
forgive me if this question or a similar one has already been asked or if it's somewhat of a beginner question. I did some googling but it's a bit hard to find the correct search terms. What I'm trying to achieve is to add some generic mathematical functions like average(), median(), mode(), etc. to Collection types. I made some progress but it seems that I need to have two functions for each of these operations with the exact same implementation: one with a Element: BinaryInteger
constraint and one with a Element:BinaryFloatingPoint
constraint. For example for the average function, this would look like the following:
func sum() -> Element
{
return self.reduce(0, +)
}
func average() -> Double where Element: BinaryInteger
{
return Double(self.sum()) / Double(self.count)
}
func average() -> Double where Element: BinaryFloatingPoint
{
return Double(self.sum()) / Double(self.count)
}
Is there a way to reduce the code duplication?
kind regards,
Tim