Skip to content

Instantly share code, notes, and snippets.

@guzhenhuaGitHub
Last active December 5, 2018 09:59
Show Gist options
  • Save guzhenhuaGitHub/32663f45f9ad434cdc813fed21d11998 to your computer and use it in GitHub Desktop.
Save guzhenhuaGitHub/32663f45f9ad434cdc813fed21d11998 to your computer and use it in GitHub Desktop.
Sequence with KeyPath
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