Skip to content

Commit 041a250

Browse files
committed
Provide a specialized filter method
Overrides filter to return a SortedArray, instead of a Swift.Array.
1 parent 44fe998 commit 041a250

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

Sources/SortedArray.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ extension SortedArray: RandomAccessCollection {
9797
public subscript(position: Index) -> Element {
9898
return _elements[position]
9999
}
100+
101+
public func filter(_ isIncluded: (Element) throws -> Bool) rethrows -> SortedArray<Element> {
102+
return SortedArray(sorted: try filter(isIncluded), areInIncreasingOrder: areInIncreasingOrder)
103+
}
100104
}
101105

102106
extension SortedArray: CustomStringConvertible, CustomDebugStringConvertible {

Tests/SortedArrayTests/SortedArrayTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ class SortedArrayTests: XCTestCase {
151151
let description = String(reflecting: sut)
152152
XCTAssertEqual(description, "<SortedArray> [\"a\", \"b\", \"c\"]")
153153
}
154+
155+
func testFilter() {
156+
let sut = SortedArray(unsorted: ["a", "b", "c"])
157+
assertElementsEqual(sut.filter { $0 != "a" }, ["b", "c"])
158+
}
154159
}
155160

156161
extension SortedArrayTests {

0 commit comments

Comments
 (0)