File tree Expand file tree Collapse file tree 2 files changed +8
-11
lines changed Expand file tree Collapse file tree 2 files changed +8
-11
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ Break a collection into subsequences where consecutive elements pass a binary
7
7
predicate, or where all elements in each chunk project to the same value.
8
8
9
9
Also, includes a ` chunks(ofCount:) ` that breaks a collection into subsequences
10
- of a given size .
10
+ of a given ` count ` .
11
11
12
12
There are two variations of the ` chunked ` method: ` chunked(by:) ` and
13
13
` chunked(on:) ` . ` chunked(by:) ` uses a binary predicate to test consecutive
@@ -29,7 +29,7 @@ let chunks = names.chunked(on: \.first!)
29
29
// [["David"], ["Kyle", "Karoy"], ["Nate"]]
30
30
```
31
31
32
- The ` chunks(ofCount:) ` takes a ` size ` parameter (required to be > 0) and separates
32
+ The ` chunks(ofCount:) ` takes a ` count ` parameter (required to be > 0) and separates
33
33
the collection into ` n ` chunks of this given size. If the size parameter is
34
34
evenly divided by the count of the base ` Collection ` all the chunks will have
35
35
the count equals to size. Otherwise, the last chunk will contain the remaining elements.
Original file line number Diff line number Diff line change @@ -331,7 +331,11 @@ where Base: RandomAccessCollection {
331
331
let distance = base. distance ( from: start. base, to: end. base)
332
332
let ( quotient, remainder) =
333
333
distance. quotientAndRemainder ( dividingBy: _chunkCount)
334
- return quotient + remainder
334
+ // Increment should account for negative distances.
335
+ if remainder < 0 {
336
+ return quotient - 1
337
+ }
338
+ return quotient + ( remainder == 0 ? 0 : 1 )
335
339
}
336
340
337
341
@inlinable
@@ -343,13 +347,6 @@ where Base: RandomAccessCollection {
343
347
base. index ( i. base, offsetBy: n * _chunkCount, limitedBy: bound) ?? bound
344
348
)
345
349
}
346
-
347
- @inlinable
348
- public var count : Int {
349
- return base. count
350
- . isMultiple ( of: _chunkCount) ? base. count/ _chunkCount
351
- : base. count/ _chunkCount + 1
352
- }
353
350
}
354
351
355
352
extension Collection {
@@ -371,7 +368,7 @@ extension Collection {
371
368
/// - Complexity: O(1)
372
369
@inlinable
373
370
public func chunks( ofCount count: Int ) -> ChunkedCollection < Self > {
374
- assert ( count > 0 , " Cannot chunk with size <= 0! " )
371
+ precondition ( count > 0 , " Cannot chunk with count <= 0! " )
375
372
return ChunkedCollection ( _base: self , _chunkCount: count)
376
373
}
377
374
}
You can’t perform that action at this time.
0 commit comments