Skip to content

Instantly share code, notes, and snippets.

@riteshhgupta
Last active December 22, 2022 09:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riteshhgupta/f3655c81bde86a869a1dc41f8bed9881 to your computer and use it in GitHub Desktop.
Save riteshhgupta/f3655c81bde86a869a1dc41f8bed9881 to your computer and use it in GitHub Desktop.
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
}
}
@clayellis
Copy link

Cool extension! 👍 Though, removed should be called removing to match Foundations APIs and to follow Swift's API Design Guidelines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment