Last active
December 5, 2018 09:59
-
-
Save guzhenhuaGitHub/32663f45f9ad434cdc813fed21d11998 to your computer and use it in GitHub Desktop.
Sequence with KeyPath
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Sequence { | |
func map<T>(_ keyPath: KeyPath<Element, T>) -> [T] { | |
return map { $0[keyPath: keyPath] } | |
} | |
} | |
extension Sequence { | |
func sorted<T: Comparable>(by keyPath: KeyPath<Element, T>) -> [Element] { | |
return sorted { a, b in | |
return a[keyPath: keyPath] < b[keyPath: keyPath] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment