Skip to content

Instantly share code, notes, and snippets.

@SlaunchaMan
Created June 29, 2015 18:08
Show Gist options
  • Select an option

  • Save SlaunchaMan/567f0e8ea60fe8f4bc48 to your computer and use it in GitHub Desktop.

Select an option

Save SlaunchaMan/567f0e8ea60fe8f4bc48 to your computer and use it in GitHub Desktop.
Returns an array containing the given array’s items of type U.
extension Array {
func optionalMap<U>(transform: (T) -> U?) -> [U] {
return map(transform).filter { $0 != nil }.map { $0! }
}
func itemsOfKind<U>(kind: U.Type) -> [U] {
return optionalMap { $0 as? U }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment