Reading through this problem in a book "write an extension for all collections that returns the N smallest elements as an array, sorted smallest first, where N is an integer parameter," a solution is given as
extension Collection where Iterator.Element: Comparable {
func challenge38(count: Int) -> [Iterator.Element] {
let sorted = self.sorted()
return Array(sorted.prefix(count))
}
}
However, I find that removing Iterator
also works.
extension Collection where Element: Comparable {
func challenge38(count: Int) -> [Element] {
let sorted = self.sorted()
return Array(sorted.prefix(count))
}
}
What exactly is the difference, and the meaning of Iterator
in this context?