Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tonyarnold/0a39d2939cd2fa4f3581ee9444e0a52d to your computer and use it in GitHub Desktop.
Save tonyarnold/0a39d2939cd2fa4f3581ee9444e0a52d to your computer and use it in GitHub Desktop.
Use this to add an operator to append items to sequences, ie: `collection += item`
extension RangeReplaceableCollection {
static func += (collection: inout Self, element: Element) {
collection.append(element)
}
static func += (collection: inout Self, element: Element?) {
if let element = element {
collection.append(element)
}
}
}
extension RangeReplaceableCollection where Element: Equatable {
static func -= (collection: inout Self, element: Element) {
if let index = collection.firstIndex(of: element) {
collection.remove(at: index)
}
}
static func -= (collection: inout Self, element: Element?) {
if let element = element, let index = collection.firstIndex(of: element) {
collection.remove(at: index)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment