Skip to content

Commit 11d7cfe

Browse files
authored
First pass at some documentation for chunked/chunks (apple#92)
* First pass at some documentation for chunked/chunks * Manually line break docs
1 parent b11a32f commit 11d7cfe

4 files changed

+43
-0
lines changed

Sources/AsyncAlgorithms/AsyncChunkedByGroupSequence.swift

+25
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,45 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
extension AsyncSequence {
13+
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection`
14+
/// type by testing if elements belong in the same group.
1315
@inlinable
1416
public func chunked<Collected: RangeReplaceableCollection>(into: Collected.Type, by belongInSameGroup: @escaping @Sendable (Element, Element) -> Bool) -> AsyncChunkedByGroupSequence<Self, Collected> where Collected.Element == Element {
1517
AsyncChunkedByGroupSequence(self, grouping: belongInSameGroup)
1618
}
1719

20+
/// Creates an asynchronous sequence that creates chunks by testing if elements belong in the same group.
1821
@inlinable
1922
public func chunked(by belongInSameGroup: @escaping @Sendable (Element, Element) -> Bool) -> AsyncChunkedByGroupSequence<Self, [Element]> {
2023
chunked(into: [Element].self, by: belongInSameGroup)
2124
}
2225
}
2326

27+
/// An `AsyncSequence` that chunks by testing if two elements belong to the same group.
28+
///
29+
/// Group chunks are determined by passing two consecutive elements to a closure which tests
30+
/// whether they are in the same group. When the `AsyncChunkedByGroupSequence` iterator
31+
/// receives the first element from the base sequence, it will immediately be added to a group. When
32+
/// it receives the second item, it tests whether the previous item and the current item belong to the
33+
/// same group. If they are not in the same group, then the iterator emits the first item's group and a
34+
/// new group is created containing the second item. Items declared to be in the same group
35+
/// accumulate until a new group is declared, or the iterator finds the end of the base sequence.
36+
/// When the base sequence terminates, the final group is emitted. If the base sequence throws an
37+
/// error, `AsyncChunkedByGroupSequence` will rethrow that error immediately and discard
38+
/// any current group.
39+
///
40+
/// let chunks = numbers.chunked { $0 <= $1 }
41+
/// for await numberChunk in chunks {
42+
/// print(numberChunk)
43+
/// }
44+
/// // prints
45+
/// // [10, 20, 30]
46+
/// // [10, 40, 40]
47+
/// // [10, 20]
2448
public struct AsyncChunkedByGroupSequence<Base: AsyncSequence, Collected: RangeReplaceableCollection>: AsyncSequence where Collected.Element == Base.Element {
2549
public typealias Element = Collected
2650

51+
/// The iterator for a `AsyncChunkedByGroupSequence` instance.
2752
@frozen
2853
public struct Iterator: AsyncIteratorProtocol {
2954

Sources/AsyncAlgorithms/AsyncChunkedOnProjectionSequence.swift

+4
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
extension AsyncSequence {
13+
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type on the uniqueness of a given subject.
1314
@inlinable
1415
public func chunked<Subject : Equatable, Collected: RangeReplaceableCollection>(into: Collected.Type, on projection: @escaping @Sendable (Element) -> Subject) -> AsyncChunkedOnProjectionSequence<Self, Subject, Collected> {
1516
AsyncChunkedOnProjectionSequence(self, projection: projection)
1617
}
1718

19+
/// Creates an asynchronous sequence that creates chunks on the uniqueness of a given subject.
1820
@inlinable
1921
public func chunked<Subject : Equatable>(on projection: @escaping @Sendable (Element) -> Subject) -> AsyncChunkedOnProjectionSequence<Self, Subject, [Element]> {
2022
chunked(into: [Element].self, on: projection)
2123
}
2224
}
2325

26+
/// An `AsyncSequence` that chunks on a subject when it differs from the last element.
2427
public struct AsyncChunkedOnProjectionSequence<Base: AsyncSequence, Subject: Equatable, Collected: RangeReplaceableCollection>: AsyncSequence where Collected.Element == Base.Element {
2528
public typealias Element = (Subject, Collected)
2629

30+
/// The iterator for a `AsyncChunkedOnProjectionSequence` instance.
2731
@frozen
2832
public struct Iterator: AsyncIteratorProtocol {
2933

Sources/AsyncAlgorithms/AsyncChunksOfCountOrSignalSequence.swift

+10
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,57 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
extension AsyncSequence {
13+
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type of a given count or when a signal `AsyncSequence` produces an element.
1314
public func chunks<Signal, Collected: RangeReplaceableCollection>(ofCount count: Int, or signal: Signal, into: Collected.Type) -> AsyncChunksOfCountOrSignalSequence<Self, Collected, Signal> where Collected.Element == Element {
1415
AsyncChunksOfCountOrSignalSequence(self, count: count, signal: signal)
1516
}
1617

18+
/// Creates an asynchronous sequence that creates chunks of a given count or when a signal `AsyncSequence` produces an element.
1719
public func chunks<Signal>(ofCount count: Int, or signal: Signal) -> AsyncChunksOfCountOrSignalSequence<Self, [Element], Signal> {
1820
chunks(ofCount: count, or: signal, into: [Element].self)
1921
}
2022

23+
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type when a signal `AsyncSequence` produces an element.
2124
public func chunked<Signal, Collected: RangeReplaceableCollection>(by signal: Signal, into: Collected.Type) -> AsyncChunksOfCountOrSignalSequence<Self, Collected, Signal> where Collected.Element == Element {
2225
AsyncChunksOfCountOrSignalSequence(self, count: nil, signal: signal)
2326
}
2427

28+
/// Creates an asynchronous sequence that creates chunks when a signal `AsyncSequence` produces an element.
2529
public func chunked<Signal>(by signal: Signal) -> AsyncChunksOfCountOrSignalSequence<Self, [Element], Signal> {
2630
chunked(by: signal, into: [Element].self)
2731
}
2832

33+
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type of a given count or when an `AsyncTimerSequence` fires.
2934
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
3035
public func chunks<C: Clock, Collected: RangeReplaceableCollection>(ofCount count: Int, or timer: AsyncTimerSequence<C>, into: Collected.Type) -> AsyncChunksOfCountOrSignalSequence<Self, Collected, AsyncTimerSequence<C>> where Collected.Element == Element {
3136
AsyncChunksOfCountOrSignalSequence(self, count: count, signal: timer)
3237
}
3338

39+
/// Creates an asynchronous sequence that creates chunks of a given count or when an `AsyncTimerSequence` fires.
3440
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
3541
public func chunks<C: Clock>(ofCount count: Int, or timer: AsyncTimerSequence<C>) -> AsyncChunksOfCountOrSignalSequence<Self, [Element], AsyncTimerSequence<C>> {
3642
chunks(ofCount: count, or: timer, into: [Element].self)
3743
}
3844

45+
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type when an `AsyncTimerSequence` fires.
3946
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
4047
public func chunked<C: Clock, Collected: RangeReplaceableCollection>(by timer: AsyncTimerSequence<C>, into: Collected.Type) -> AsyncChunksOfCountOrSignalSequence<Self, Collected, AsyncTimerSequence<C>> where Collected.Element == Element {
4148
AsyncChunksOfCountOrSignalSequence(self, count: nil, signal: timer)
4249
}
4350

51+
/// Creates an asynchronous sequence that creates chunks when an `AsyncTimerSequence` fires.
4452
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
4553
public func chunked<C: Clock>(by timer: AsyncTimerSequence<C>) -> AsyncChunksOfCountOrSignalSequence<Self, [Element], AsyncTimerSequence<C>> {
4654
chunked(by: timer, into: [Element].self)
4755
}
4856
}
4957

58+
/// An `AsyncSequence` that chunks elements into collected `RangeReplaceableCollection` instances by either count or a signal from another `AsyncSequence`.
5059
public struct AsyncChunksOfCountOrSignalSequence<Base: AsyncSequence, Collected: RangeReplaceableCollection, Signal: AsyncSequence>: AsyncSequence, Sendable where Collected.Element == Base.Element, Base: Sendable, Signal: Sendable, Base.AsyncIterator: Sendable, Signal.AsyncIterator: Sendable, Base.Element: Sendable, Signal.Element: Sendable {
5160

5261
public typealias Element = Collected
5362

63+
/// The iterator for a `AsyncChunksOfCountOrSignalSequence` instance.
5464
public struct Iterator: AsyncIteratorProtocol, Sendable {
5565
let count: Int?
5666
var state: Merge2StateMachine<Base, Signal>

Sources/AsyncAlgorithms/AsyncChunksOfCountSequence.swift

+4
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
extension AsyncSequence {
13+
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` of a given count.
1314
@inlinable
1415
public func chunks<Collected: RangeReplaceableCollection>(ofCount count: Int, into: Collected.Type) -> AsyncChunksOfCountSequence<Self, Collected> where Collected.Element == Element {
1516
AsyncChunksOfCountSequence(self, count: count)
1617
}
1718

19+
/// Creates an asynchronous sequence that creates chunks of a given count.
1820
@inlinable
1921
public func chunks(ofCount count: Int) -> AsyncChunksOfCountSequence<Self, [Element]> {
2022
chunks(ofCount: count, into: [Element].self)
2123
}
2224
}
2325

26+
/// An `AsyncSequence` that chunks elements into `RangeReplaceableCollection` instances of at least a given count.
2427
public struct AsyncChunksOfCountSequence<Base: AsyncSequence, Collected: RangeReplaceableCollection>: AsyncSequence where Collected.Element == Base.Element {
2528
public typealias Element = Collected
2629

30+
/// The iterator for a `AsyncChunksOfCountSequence` instance.
2731
@frozen
2832
public struct Iterator: AsyncIteratorProtocol {
2933

0 commit comments

Comments
 (0)