30 lines
797 B
Swift
30 lines
797 B
Swift
import Foundation
|
|
|
|
extension Collection {
|
|
|
|
func sorted<T>(ascending: Bool = true, using conversion: (Element) -> T) -> [Element] where T: Comparable {
|
|
guard ascending else {
|
|
return sorted { conversion($0) > conversion($1) }
|
|
}
|
|
return sorted { conversion($0) < conversion($1) }
|
|
}
|
|
|
|
func count(where predicate: (Element) throws -> Bool) rethrows -> Int {
|
|
try reduce(0) { count, element in
|
|
try predicate(element) ? count + 1 : count
|
|
}
|
|
}
|
|
|
|
func countThrows(where predicate: (Element) throws -> Void) -> Int {
|
|
reduce(0) { count, element in
|
|
do {
|
|
try predicate(element)
|
|
return count
|
|
} catch {
|
|
return count + 1
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|