Created
May 4, 2018 06:03
-
-
Save iamchiwon/187dd79ddfe939bf9175f800db7ad663 to your computer and use it in GitHub Desktop.
This file contains 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 Array where Element: Equatable { | |
func exists(_ item: Element) -> Bool { | |
if let _ = self.indexOf(item) { | |
return true | |
} | |
return false | |
} | |
func indexOf(_ item: Element) -> Int? { | |
return self.enumerated().filter({ $0.element == item }).map({ $0.offset }).first | |
} | |
mutating func remove(item: Element) -> Element? { | |
if let index = indexOf(item) { | |
return self.remove(at: index) | |
} | |
return nil | |
} | |
func removed(_ item: Element) -> [Element] { | |
return self.filter({ $0 != item }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment