Created
August 25, 2020 13:29
-
-
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`
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 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