-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add extension for addition and subtraction
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
extension OrderedSet { | ||
|
||
/// Appends the contents of a sequencet to the ordered set. | ||
/// | ||
/// If it already contains the element, an element is not inserted. | ||
/// | ||
/// - returns: True if any item was inserted. | ||
@discardableResult | ||
public mutating func append<S : Sequence>(_ sequence: S) -> Bool | ||
where S.Iterator.Element == Element { | ||
return sequence.map { append($0) }.contains(true) | ||
} | ||
|
||
/// Return a new ordered set with the elements of a finite sequence | ||
/// appended. | ||
public func appending<S : Sequence>(_ sequence: S) | ||
-> OrderedSet where S.Iterator.Element == Element { | ||
var copy = self | ||
copy.append(sequence) | ||
return copy | ||
} | ||
|
||
/// Remove all members in the ordered set that occur in a finite sequence. | ||
public mutating func subtract<S : Sequence>(_ sequence: S) | ||
where S.Iterator.Element == Element { | ||
set.subtract(sequence) | ||
array = array.filter { set.contains($0) } | ||
} | ||
|
||
/// Return a new ordered set with elements in this set that do not occur | ||
/// in a finite sequence. | ||
public func subtracting<S : Sequence>(_ sequence: S) | ||
-> OrderedSet where S.Iterator.Element == Element { | ||
var copy = self | ||
copy.subtract(sequence) | ||
return copy | ||
} | ||
} |
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