Skip to content

Commit 2938a79

Browse files
Change Sequence.contains(_:) where Element: Hashable to receive any Sequence (#1169)
1 parent 076ad52 commit 2938a79

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ The changelog for **SwifterSwift**. Also see the [releases](https://github.com/S
1212

1313
### Changed
1414
- **Sequence**
15-
- `sorted(by:)`, `sorted(by:with:)`, `sorted(by:and:)`, `sorted(by:and:and:)`, `sum(for:)`, `first(where:equals:)` now have alternatives that receive functions as parameters. This change maintains compatibility with KeyPath while making the methods more flexible. [#1170](https://github.com/SwifterSwift/SwifterSwift/pull/1170) by [MartonioJunior](https://github.com/MartonioJunior)
15+
- `sorted(by:)`, `sorted(by:with:)`, `sorted(by:and:)`, `sorted(by:and:and:)`, `sum(for:)`, `first(where:equals:)` now have alternatives that receive functions as parameters. This change maintains compatibility with KeyPath while making the methods more flexible. [#1170](https://github.com/SwifterSwift/SwifterSwift/pull/1170) by [MartonioJunior](https://github.com/MartonioJunior)
16+
- `contains(_:)` for `Element: Hashable` now can receive any type that conforms to `Sequence`, not just an `Array`. [#1169](https://github.com/SwifterSwift/SwifterSwift/pull/1169) by [MartonioJunior](https://github.com/MartonioJunior)
1617

1718
### Fixed
1819
- **PrivacyInfo.xcprivacy**

Sources/SwifterSwift/SwiftStdlib/SequenceExtensions.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,24 +341,24 @@ public extension Sequence where Element: Equatable {
341341
/// ["h", "e", "l", "l", "o"].contains(["l", "o"]) -> true
342342
///
343343
/// - Parameter elements: array of elements to check.
344-
/// - Returns: true if array contains all given items.
344+
/// - Returns: true if sequence contains all given items.
345345
/// - Complexity: _O(m·n)_, where _m_ is the length of `elements` and _n_ is the length of this sequence.
346-
func contains(_ elements: [Element]) -> Bool {
346+
func contains<S>(_ elements: S) -> Bool where S: Sequence, Element == S.Element {
347347
return elements.allSatisfy { contains($0) }
348348
}
349349
}
350350

351351
public extension Sequence where Element: Hashable {
352-
/// SwifterSwift: Check if array contains an array of elements.
352+
/// SwifterSwift: Check if sequence contains elements of another sequence.
353353
///
354354
/// [1, 2, 3, 4, 5].contains([1, 2]) -> true
355355
/// [1.2, 2.3, 4.5, 3.4, 4.5].contains([2, 6]) -> false
356356
/// ["h", "e", "l", "l", "o"].contains(["l", "o"]) -> true
357357
///
358-
/// - Parameter elements: array of elements to check.
359-
/// - Returns: true if array contains all given items.
358+
/// - Parameter elements: sequence of elements to check.
359+
/// - Returns: true if sequence contains all given items.
360360
/// - Complexity: _O(m + n)_, where _m_ is the length of `elements` and _n_ is the length of this sequence.
361-
func contains(_ elements: [Element]) -> Bool {
361+
func contains<S: Sequence>(_ elements: S) -> Bool where Element == S.Element {
362362
let set = Set(self)
363363
return elements.allSatisfy { set.contains($0) }
364364
}

Tests/SwiftStdlibTests/SequenceExtensionsTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ final class SequenceExtensionsTests: XCTestCase {
112112
XCTAssert([1, 2, 3].contains([2, 3]))
113113
XCTAssert([1, 2, 3].contains([1, 3]))
114114
XCTAssertFalse([1, 2, 3].contains([4, 5]))
115+
116+
XCTAssert([Int]().contains(AnyIterator({ nil })))
117+
XCTAssertFalse([Int]().contains(AnyIterator([1, 2].makeIterator())))
118+
XCTAssert([1, 2, 3].contains(AnyIterator([1, 2].makeIterator())))
119+
XCTAssert([1, 2, 3].contains(AnyIterator([2, 3].makeIterator())))
120+
XCTAssert([1, 2, 3].contains(AnyIterator([1, 3].makeIterator())))
121+
XCTAssertFalse([1, 2, 3].contains(AnyIterator([4, 5].makeIterator())))
122+
123+
XCTAssert([Int]().contains(Set<Int>()))
124+
XCTAssertFalse([Int]().contains(Set([1, 2])))
125+
XCTAssert([1, 2, 3].contains(Set([1, 2])))
126+
XCTAssert([1, 2, 3].contains(Set([2, 3])))
127+
XCTAssert([1, 2, 3].contains(Set([1, 3])))
128+
XCTAssertFalse([1, 2, 3].contains(Set([4, 5])))
115129
}
116130

117131
func testContainsDuplicates() {

0 commit comments

Comments
 (0)