@@ -200,7 +200,7 @@ extension ChunkedOnCollection: LazyCollectionProtocol {}
200
200
201
201
/// A collection wrapper that evenly breaks a collection into a given number of
202
202
/// chunks.
203
- public struct EvenChunksCollection < Base: Collection > {
203
+ public struct EvenlyChunkedCollection < Base: Collection > {
204
204
/// The base collection.
205
205
@usableFromInline
206
206
internal let base : Base
@@ -230,7 +230,7 @@ public struct EvenChunksCollection<Base: Collection> {
230
230
}
231
231
}
232
232
233
- extension EvenChunksCollection {
233
+ extension EvenlyChunkedCollection {
234
234
/// Returns the number of chunks with size `smallChunkSize + 1` at the start
235
235
/// of this collection.
236
236
@inlinable
@@ -277,7 +277,7 @@ extension EvenChunksCollection {
277
277
}
278
278
}
279
279
280
- extension EvenChunksCollection : Collection {
280
+ extension EvenlyChunkedCollection : Collection {
281
281
public struct Index : Comparable {
282
282
/// The range corresponding to the chunk at this position.
283
283
@usableFromInline
@@ -381,9 +381,9 @@ extension EvenChunksCollection: Collection {
381
381
}
382
382
}
383
383
384
- extension EvenChunksCollection . Index : Hashable where Base. Index: Hashable { }
384
+ extension EvenlyChunkedCollection . Index : Hashable where Base. Index: Hashable { }
385
385
386
- extension EvenChunksCollection : BidirectionalCollection
386
+ extension EvenlyChunkedCollection : BidirectionalCollection
387
387
where Base: BidirectionalCollection
388
388
{
389
389
@inlinable
@@ -393,13 +393,13 @@ extension EvenChunksCollection: BidirectionalCollection
393
393
}
394
394
}
395
395
396
- extension EvenChunksCollection : RandomAccessCollection
396
+ extension EvenlyChunkedCollection : RandomAccessCollection
397
397
where Base: RandomAccessCollection { }
398
398
399
- extension EvenChunksCollection : LazySequenceProtocol
399
+ extension EvenlyChunkedCollection : LazySequenceProtocol
400
400
where Base: LazySequenceProtocol { }
401
401
402
- extension EvenChunksCollection : LazyCollectionProtocol
402
+ extension EvenlyChunkedCollection : LazyCollectionProtocol
403
403
where Base: LazyCollectionProtocol { }
404
404
405
405
//===----------------------------------------------------------------------===//
@@ -762,22 +762,37 @@ extension ChunksOfCountCollection {
762
762
}
763
763
764
764
extension Collection {
765
- /// Returns a `ChunksOfCountCollection<Self>` view presenting the elements in
766
- /// chunks with count of the given count parameter .
765
+ /// Returns a collection of subsequences of this collection, each with the
766
+ /// specified length .
767
767
///
768
- /// - Parameter count: The size of the chunks. If the `count` parameter is
769
- /// evenly divided by the count of the base `Collection` all the chunks will
770
- /// have the count equals to size. Otherwise, the last chunk will contain
771
- /// the remaining elements .
768
+ /// If the number of elements in the collection is evenly divided by `count`,
769
+ /// then every chunk will have a length equal to `count`. Otherwise, every
770
+ /// chunk but the last will have a length equal to `count`, with the
771
+ /// remaining elements in the last chunk .
772
772
///
773
- /// let c = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
774
- /// print(c.chunks(ofCount: 5).map(Array.init))
775
- /// // [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
773
+ /// let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
774
+ /// for chunk in numbers.chunks(ofCount: 5) {
775
+ /// print(chunk)
776
+ /// }
777
+ /// // [1, 2, 3, 4, 5]
778
+ /// // [6, 7, 8, 9, 10]
776
779
///
777
780
/// print(c.chunks(ofCount: 3).map(Array.init))
778
- /// // [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
781
+ /// for chunk in numbers.chunks(ofCount: 5) {
782
+ /// print(chunk)
783
+ /// }
784
+ /// // [1, 2, 3]
785
+ /// // [4, 5, 5]
786
+ /// // [7, 8, 9]
787
+ /// // [10]
779
788
///
780
- /// - Complexity: O(*n*), because the start index is pre-computed.
789
+ /// - Parameter count: The desired size of each chunk.
790
+ /// - Returns: A collection of consescutive, non-overlapping subseqeunces of
791
+ /// this collection, where each subsequence (except possibly the last) has
792
+ /// the length `count`.
793
+ ///
794
+ /// - Complexity: O(1) if the collection conforms to `RandomAccessCollection`;
795
+ /// otherwise, O(*k*), where *k* is equal to `count`.
781
796
@inlinable
782
797
public func chunks( ofCount count: Int ) -> ChunksOfCountCollection < Self > {
783
798
precondition ( count > 0 , " Cannot chunk with count <= 0! " )
@@ -798,10 +813,9 @@ extension ChunksOfCountCollection: LazyCollectionProtocol
798
813
//===----------------------------------------------------------------------===//
799
814
800
815
extension Collection {
801
- /// Returns a collection of `count` evenly divided subsequences of this
802
- /// collection.
816
+ /// Returns a collection of evenly divided subsequences of this collection.
803
817
///
804
- /// This method divides the collection into a given number of equally sized
818
+ /// This method divides the collection into a given number of evenly sized
805
819
/// chunks. If the length of the collection is not divisible by `count`, the
806
820
/// chunks at the start will be longer than the chunks at the end, like in
807
821
/// this example:
@@ -815,12 +829,30 @@ extension Collection {
815
829
/// // "rl"
816
830
/// // "d!"
817
831
///
818
- /// - Complexity: O(1) if the collection conforms to `RandomAccessCollection`,
819
- /// otherwise O(*n*), where *n* is the length of the collection.
832
+ /// If the number passed as `count` is greater than the number of elements in
833
+ /// the collection, the result will include one or more empty subsequences.
834
+ ///
835
+ /// for chunk in "Hi!".evenlyChunked(in: 5) {
836
+ /// print(chunk)
837
+ /// }
838
+ /// // "H"
839
+ /// // "i"
840
+ /// // "!"
841
+ /// // ""
842
+ /// // ""
843
+ ///
844
+ /// - Parameter count: The number of chunks to evenly divide this collection
845
+ /// into. If this collection is non-empty, `count` must be greater than
846
+ /// zero; otherwise, `count` may be zero or greater.
847
+ /// - Returns: A collection of `count` subsequences of this collection,
848
+ /// divided as evenly as possible.
849
+ ///
850
+ /// - Complexity: O(1) if the collection conforms to `RandomAccessCollection`;
851
+ /// otherwise, O(*n*), where *n* is the length of the collection.
820
852
@inlinable
821
- public func evenlyChunked( in count: Int ) -> EvenChunksCollection < Self > {
853
+ public func evenlyChunked( in count: Int ) -> EvenlyChunkedCollection < Self > {
822
854
precondition ( count >= 0 , " Can't divide into a negative number of chunks " )
823
855
precondition ( count > 0 || isEmpty, " Can't divide a non-empty collection into 0 chunks " )
824
- return EvenChunksCollection ( base: self , numberOfChunks: count)
856
+ return EvenlyChunkedCollection ( base: self , numberOfChunks: count)
825
857
}
826
858
}
0 commit comments