Skip to content

Commit 19cdf53

Browse files
committed
Add function for returning a new orderedset with a member element removed
1 parent dd67859 commit 19cdf53

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

Sources/OrderedSet/OrderedSet.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,17 @@ public struct OrderedSet<E: Hashable> {
277277
return Self(arr)
278278
}
279279

280+
/// Returns a new ordered set with the member element removed.
281+
/// This function returns an equivalent ordered set if `element` is not a
282+
/// member.
283+
/// - parameter element: The member to remove.
284+
public func removing(element: Element) -> Self {
285+
guard let index = self.index(of: element) else { return self }
286+
var arr = _array
287+
arr.remove(at: index)
288+
return Self(arr)
289+
}
290+
280291
/// Returns a new ordered set with the elements filtered by the given predicate.
281292
/// - parameter shouldBeRemoved: A closure that takes an element of the
282293
/// sequence as its argument and returns a Boolean value indicating

Tests/OrderedSetTests/CreationFunctionTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ final class CreationFunctionTests: XCTestCase {
118118
XCTAssert(set.sanityCheck())
119119
}
120120

121+
func testRemovingElement() {
122+
let array = [1,2,3,4,5]
123+
let set = OrderedSet(array)
124+
let removing = set.removing(element: 3)
125+
XCTAssertEqual(removing, OrderedSet([1,2,4,5]))
126+
XCTAssert(set.sanityCheck())
127+
}
128+
121129
func testRemovingAll() {
122130
let array = [1,2,3,4,5]
123131
let set = OrderedSet(array)

0 commit comments

Comments
 (0)