I want to add NumPy like functionality to a Swift array where you can use the + or – operators to add/subtract a Double to every element of a numeric Array and return a new Array with the new elements as [Double]. Here is how I have implemented it in 2 extensions for Int and Floating point types:
extension Array where Element: BinaryInteger {
static func + (array: Self, num: Double) -> [Double]
{
return array.map{Double($0) + num}
}
static func - (array: Self, num: Double) -> [Double]
{
return array.map{Double($0) - num}
}
}
extension Array where Element: BinaryFloatingPoint {
static func + (array: Self, num: Double) -> [Double]
{
return array.map{Double($0) + num}
}
static func - (array: Self, num: Double) -> [Double]
{
return array.map{Double($0) - num}
}
}
As you can see there is a lot of duplicate code here. Is there a better way to implement this?
I tried this but it doesn’t work:
extension Array where Element: Numeric {
static func + (array: Self, num: Double) -> [Double]
{
return array.map{Double($0) + num}
}
static func - (array: Self, num: Double) -> [Double]
{
return array.map{Double($0) - num}
}
}
The compiler complains at line Double($0)
since Double doesn’t have initializer for Numeric.
2
Answers
I decided to check type of Element and force typecast instead:
Why use
Double
at all? You’re just introducing potential precision issues for integer types that have perfect precision over integers