-
-
Save riteshhgupta/f3655c81bde86a869a1dc41f8bed9881 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 { | |
// it removes the first element that matches the handler condition on the array itself | |
public mutating func remove(_ handler: (Element) -> Bool) -> Element? { | |
guard let idx = index(where: handler) else { return nil } | |
let item = self[idx] | |
remove(at: idx) | |
return item | |
} | |
// it removes the first element that matches the handler condition & returns a new array | |
public func removed(_ handler: (Element) -> Bool) -> Array { | |
var items = self | |
items.remove(handler) | |
return items | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool extension! 👍 Though,
removed
should be calledremoving
to matchFoundation
s APIs and to follow Swift's API Design Guidelines.