>>102474746
Switft also has generics with restrictions. It's quite lovely.
// Function to find the maximum element in an array of Comparable elements
func findMax<T: Comparable>(array: [T]) -> T? {
guard !array.isEmpty else { return nil }
var maxElement = array[0]
for element in array {
if element > maxElement {
maxElement = element
}
}
return maxElement
}
Comment too long. Click here to view the full text.