Guest User

Untitled

a guest
Dec 15th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. extension Array where Element: OptionalType {
  2. // Short hand for the original compactMap: we do not need to
  3. // pass in a selector function - instead just unwrap each
  4. // element in the Array to its base type.
  5. func compactMap() -> [Element.Wrapped] {
  6. return self.compactMap({$0.value})
  7. }
  8. }
  9.  
  10. let a: [Int] = [0, 1, 2]
  11. // a.compactMap() - compile error.
  12. let b: [Int?] = [0, nil, 1, 2, nil]
  13. let c: [Int] = b.compactMap() // Returns [1, 2, 3]
Add Comment
Please, Sign In to add comment