Hi everyone!
I’m experimenting with this functional selection sort code and I wonder if
anyone could help me simplify the portion indicated below.
// Swift 3
func selectionSort(_ array: [Int]) -> [Int] {
guard array.count > 1, let minElement = array.min() else {
return array
}
let indexOfMinElement = array.index(of: minElement)!
// All of this just to filter out the first smallest element and return the
rest
// Also tried ‘suffix(from:)' here, but couldn’t make it work properly
let rest = array.enumerated()
.filter({ index, _ in index != indexOfMinElement })
.map({ _, element in element })
return [minElement] + selectionSort(rest)
}
By the way, it feels really weird to chain method calls like this in Swift 3,
particularly due to the mixing of terms of art (e.g. “filter” and “map”) with
other methods that follow the -ed/-ing rules from the API guidelines (e.g.
enumerated).
Best,
— A_______________________________________________
swift-users mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-users