Skip to content

Instantly share code, notes, and snippets.

@BasThomas
Last active April 11, 2018 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BasThomas/d03e138a9bb098d347c52ca7d6870a45 to your computer and use it in GitHub Desktop.
Save BasThomas/d03e138a9bb098d347c52ca7d6870a45 to your computer and use it in GitHub Desktop.
Backward compatibility for `compactMap` in Swift 4.0.
#if swift(>=4.1)
#else
extension Collection {
func compactMap<ElementOfResult>(
_ transform: (Element) throws -> ElementOfResult?
) rethrows -> [ElementOfResult] {
return try flatMap(transform)
}
}
#endif
#if swift(>=4.1)
#elseif swift(>=4.2)
#warning("Remove this once we've updated.")
#else
extension Collection {
func compactMap<ElementOfResult>(
_ transform: (Element) throws -> ElementOfResult?
) rethrows -> [ElementOfResult] {
return try flatMap(transform)
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment