Skip to content

Commit ace62d3

Browse files
committed
Remove combinations() in favor of using 0...
1 parent 932f357 commit ace62d3

File tree

4 files changed

+28
-82
lines changed

4 files changed

+28
-82
lines changed

Guides/Combinations.md

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,6 @@ for combo in numbers(ofCounts: 2...3) {
5656
// [20, 30, 40]
5757
```
5858

59-
The `combinations()` method returns a sequence of *all* the different
60-
combinations of a collection’s elements in increasing order of size.
61-
62-
```swift
63-
let numbers = [10, 20, 30, 40]
64-
for combo in numbers.combinations() {
65-
print(combo)
66-
}
67-
// []
68-
// [10]
69-
// [20]
70-
// [30]
71-
// [40]
72-
// [10, 20]
73-
// [10, 30]
74-
// [10, 40]
75-
// [20, 30]
76-
// [20, 40]
77-
// [30, 40]
78-
// [10, 20, 30]
79-
// [10, 20, 40]
80-
// [10, 30, 40]
81-
// [20, 30, 40]
82-
// [10, 20, 30, 40]
83-
```
84-
8559
## Detailed Design
8660

8761
The `combinations(ofCount:)` method is declared as a `Collection` extension,

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Read more about the package, and the intent behind it, in the [announcement on s
88

99
#### Combinations / permutations
1010

11-
- [`combinations(ofCount:)`, `combinations(ofCounts:)`, `combinations()`](https://github.com/apple/swift-algorithms/blob/main/Guides/Combinations.md): Combinations of particular sizes of the elements in a collection.
11+
- [`combinations(ofCount:)`, `combinations(ofCounts:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Combinations.md): Combinations of particular sizes of the elements in a collection.
1212
- [`permutations(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Permutations.md): Permutations of a particular size of the elements in a collection, or of the full collection.
1313

1414
#### Mutating algorithms

Sources/Algorithms/Combinations.swift

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ public struct Combinations<Base: Collection> {
2020
@usableFromInline
2121
internal let kRange: Range<Int>?
2222

23-
/// Initializes a `Combinations` for all combinations of `base` of all sizes.
24-
/// - Parameter base: The collection to iterate over for combinations.
25-
@usableFromInline
26-
internal init(_ base: Base) {
27-
self.init(base, kRange: 0...)
28-
}
29-
3023
/// Initializes a `Combinations` for all combinations of `base` of size `k`.
3124
/// - Parameters:
3225
/// - base: The collection to iterate over for combinations.
@@ -183,51 +176,6 @@ extension Combinations: LazySequenceProtocol where Base: LazySequenceProtocol {}
183176
extension Combinations: Equatable where Base: Equatable {}
184177
extension Combinations: Hashable where Base: Hashable {}
185178

186-
//===----------------------------------------------------------------------===//
187-
// combinations()
188-
//===----------------------------------------------------------------------===//
189-
190-
extension Collection {
191-
/// Returns a collection of all the combinations of this collection's
192-
/// elements, including the full collection and an empty collection
193-
///
194-
/// This example prints the all the combinations from an array of letters:
195-
///
196-
/// let letters = ["A", "B", "C", "D"]
197-
/// for combo in letters.combinations() {
198-
/// print(combo.joined(separator: ", "))
199-
/// }
200-
/// //
201-
/// // A
202-
/// // B
203-
/// // C
204-
/// // D
205-
/// // A, B
206-
/// // A, C
207-
/// // A, D
208-
/// // B, C
209-
/// // B, D
210-
/// // C, D
211-
/// // A, B, C
212-
/// // A, B, D
213-
/// // A, C, D
214-
/// // B, C, D
215-
/// // A, B, C, D
216-
///
217-
/// The returned collection presents combinations in a consistent order, where
218-
/// the indices in each combination are in ascending lexicographical order,
219-
/// and the size of the combinations are in increasing order.
220-
/// That is, in the example above, the combinations in order are the elements
221-
/// at `[]`, `[0]`, `[1]`, `[2]`, `[3]`, `[0, 1]`, `[0, 2]`, `[0, 3]`,
222-
/// `[1, 2]`, `[1, 3]`, … `[0, 1, 2, 3]`.
223-
///
224-
/// - Complexity: O(1)
225-
@inlinable
226-
public func combinations() -> Combinations<Self> {
227-
return Combinations(self)
228-
}
229-
}
230-
231179
//===----------------------------------------------------------------------===//
232180
// combinations(ofCounts:)
233181
//===----------------------------------------------------------------------===//
@@ -260,6 +208,30 @@ extension Collection {
260208
/// at `[0]`, `[1]`, `[2]`, `[3]`, `[0, 1]`, `[0, 2]`, `[0, 3]`, `[1, 2]`,
261209
/// `[1, 3]`, and finally `[2, 3]`.
262210
///
211+
/// This example prints _all_ the combinations (including an empty array and
212+
/// the original collection) from an array of numbers:
213+
///
214+
/// let numbers = [10, 20, 30, 40]
215+
/// for combo in numbers.combinations() {
216+
/// print(combo)
217+
/// }
218+
/// // []
219+
/// // [10]
220+
/// // [20]
221+
/// // [30]
222+
/// // [40]
223+
/// // [10, 20]
224+
/// // [10, 30]
225+
/// // [10, 40]
226+
/// // [20, 30]
227+
/// // [20, 40]
228+
/// // [30, 40]
229+
/// // [10, 20, 30]
230+
/// // [10, 20, 40]
231+
/// // [10, 30, 40]
232+
/// // [20, 30, 40]
233+
/// // [10, 20, 30, 40]
234+
///
263235
/// If `kRange` is `0...0`, the resulting sequence has exactly one element, an
264236
/// empty array. If `k.upperBound` is greater than the number of elements in
265237
/// this sequence, the resulting sequence has no elements.

Tests/SwiftAlgorithmsTests/CombinationsTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ final class CombinationsTests: XCTestCase {
6464
let c14 = c.combinations(ofCounts: ...3).count
6565
XCTAssertEqual(c14, 15)
6666

67-
let c15 = c.combinations().count
67+
let c15 = c.combinations(ofCounts: 0...).count
6868
XCTAssertEqual(c15, 16)
6969
}
7070

@@ -89,7 +89,7 @@ final class CombinationsTests: XCTestCase {
8989
let c6 = c.combinations(ofCounts: 0...4)
9090
XCTAssertEqual(["", "A", "B", "C", "D", "AB", "AC", "AD", "BC", "BD", "CD", "ABC", "ABD", "ACD", "BCD", "ABCD"], c6.map { String($0) })
9191

92-
let c7 = c.combinations()
92+
let c7 = c.combinations(ofCounts: 0...)
9393
XCTAssertEqual(["", "A", "B", "C", "D", "AB", "AC", "AD", "BC", "BD", "CD", "ABC", "ABD", "ACD", "BCD", "ABCD"], c7.map { String($0) })
9494

9595
let c8 = c.combinations(ofCounts: ...4)
@@ -121,6 +121,6 @@ final class CombinationsTests: XCTestCase {
121121
XCTAssertLazySequence("ABC".lazy.combinations(ofCounts: 1...3))
122122
XCTAssertLazySequence("ABC".lazy.combinations(ofCounts: 1...))
123123
XCTAssertLazySequence("ABC".lazy.combinations(ofCounts: ...3))
124-
XCTAssertLazySequence("ABC".lazy.combinations())
124+
XCTAssertLazySequence("ABC".lazy.combinations(ofCounts: 0...))
125125
}
126126
}

0 commit comments

Comments
 (0)