From 11d7cfe1c9b668484cc1898fd9bad3a33f2b6d77 Mon Sep 17 00:00:00 2001 From: Philippe Hausler Date: Fri, 18 Mar 2022 14:03:46 -0700 Subject: [PATCH] First pass at some documentation for chunked/chunks (#92) * First pass at some documentation for chunked/chunks * Manually line break docs --- .../AsyncChunkedByGroupSequence.swift | 25 +++++++++++++++++++ .../AsyncChunkedOnProjectionSequence.swift | 4 +++ .../AsyncChunksOfCountOrSignalSequence.swift | 10 ++++++++ .../AsyncChunksOfCountSequence.swift | 4 +++ 4 files changed, 43 insertions(+) diff --git a/Sources/AsyncAlgorithms/AsyncChunkedByGroupSequence.swift b/Sources/AsyncAlgorithms/AsyncChunkedByGroupSequence.swift index 54f9637..7a9e9a3 100644 --- a/Sources/AsyncAlgorithms/AsyncChunkedByGroupSequence.swift +++ b/Sources/AsyncAlgorithms/AsyncChunkedByGroupSequence.swift @@ -10,20 +10,45 @@ //===----------------------------------------------------------------------===// extension AsyncSequence { + /// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` + /// type by testing if elements belong in the same group. @inlinable public func chunked(into: Collected.Type, by belongInSameGroup: @escaping @Sendable (Element, Element) -> Bool) -> AsyncChunkedByGroupSequence where Collected.Element == Element { AsyncChunkedByGroupSequence(self, grouping: belongInSameGroup) } + /// Creates an asynchronous sequence that creates chunks by testing if elements belong in the same group. @inlinable public func chunked(by belongInSameGroup: @escaping @Sendable (Element, Element) -> Bool) -> AsyncChunkedByGroupSequence { chunked(into: [Element].self, by: belongInSameGroup) } } +/// An `AsyncSequence` that chunks by testing if two elements belong to the same group. +/// +/// Group chunks are determined by passing two consecutive elements to a closure which tests +/// whether they are in the same group. When the `AsyncChunkedByGroupSequence` iterator +/// receives the first element from the base sequence, it will immediately be added to a group. When +/// it receives the second item, it tests whether the previous item and the current item belong to the +/// same group. If they are not in the same group, then the iterator emits the first item's group and a +/// new group is created containing the second item. Items declared to be in the same group +/// accumulate until a new group is declared, or the iterator finds the end of the base sequence. +/// When the base sequence terminates, the final group is emitted. If the base sequence throws an +/// error, `AsyncChunkedByGroupSequence` will rethrow that error immediately and discard +/// any current group. +/// +/// let chunks = numbers.chunked { $0 <= $1 } +/// for await numberChunk in chunks { +/// print(numberChunk) +/// } +/// // prints +/// // [10, 20, 30] +/// // [10, 40, 40] +/// // [10, 20] public struct AsyncChunkedByGroupSequence: AsyncSequence where Collected.Element == Base.Element { public typealias Element = Collected + /// The iterator for a `AsyncChunkedByGroupSequence` instance. @frozen public struct Iterator: AsyncIteratorProtocol { diff --git a/Sources/AsyncAlgorithms/AsyncChunkedOnProjectionSequence.swift b/Sources/AsyncAlgorithms/AsyncChunkedOnProjectionSequence.swift index 44fce0b..ccd1518 100644 --- a/Sources/AsyncAlgorithms/AsyncChunkedOnProjectionSequence.swift +++ b/Sources/AsyncAlgorithms/AsyncChunkedOnProjectionSequence.swift @@ -10,20 +10,24 @@ //===----------------------------------------------------------------------===// extension AsyncSequence { + /// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type on the uniqueness of a given subject. @inlinable public func chunked(into: Collected.Type, on projection: @escaping @Sendable (Element) -> Subject) -> AsyncChunkedOnProjectionSequence { AsyncChunkedOnProjectionSequence(self, projection: projection) } + /// Creates an asynchronous sequence that creates chunks on the uniqueness of a given subject. @inlinable public func chunked(on projection: @escaping @Sendable (Element) -> Subject) -> AsyncChunkedOnProjectionSequence { chunked(into: [Element].self, on: projection) } } +/// An `AsyncSequence` that chunks on a subject when it differs from the last element. public struct AsyncChunkedOnProjectionSequence: AsyncSequence where Collected.Element == Base.Element { public typealias Element = (Subject, Collected) + /// The iterator for a `AsyncChunkedOnProjectionSequence` instance. @frozen public struct Iterator: AsyncIteratorProtocol { diff --git a/Sources/AsyncAlgorithms/AsyncChunksOfCountOrSignalSequence.swift b/Sources/AsyncAlgorithms/AsyncChunksOfCountOrSignalSequence.swift index f5bd53f..70ea2d2 100644 --- a/Sources/AsyncAlgorithms/AsyncChunksOfCountOrSignalSequence.swift +++ b/Sources/AsyncAlgorithms/AsyncChunksOfCountOrSignalSequence.swift @@ -10,47 +10,57 @@ //===----------------------------------------------------------------------===// extension AsyncSequence { + /// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type of a given count or when a signal `AsyncSequence` produces an element. public func chunks(ofCount count: Int, or signal: Signal, into: Collected.Type) -> AsyncChunksOfCountOrSignalSequence where Collected.Element == Element { AsyncChunksOfCountOrSignalSequence(self, count: count, signal: signal) } + /// Creates an asynchronous sequence that creates chunks of a given count or when a signal `AsyncSequence` produces an element. public func chunks(ofCount count: Int, or signal: Signal) -> AsyncChunksOfCountOrSignalSequence { chunks(ofCount: count, or: signal, into: [Element].self) } + /// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type when a signal `AsyncSequence` produces an element. public func chunked(by signal: Signal, into: Collected.Type) -> AsyncChunksOfCountOrSignalSequence where Collected.Element == Element { AsyncChunksOfCountOrSignalSequence(self, count: nil, signal: signal) } + /// Creates an asynchronous sequence that creates chunks when a signal `AsyncSequence` produces an element. public func chunked(by signal: Signal) -> AsyncChunksOfCountOrSignalSequence { chunked(by: signal, into: [Element].self) } + /// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type of a given count or when an `AsyncTimerSequence` fires. @available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) public func chunks(ofCount count: Int, or timer: AsyncTimerSequence, into: Collected.Type) -> AsyncChunksOfCountOrSignalSequence> where Collected.Element == Element { AsyncChunksOfCountOrSignalSequence(self, count: count, signal: timer) } + /// Creates an asynchronous sequence that creates chunks of a given count or when an `AsyncTimerSequence` fires. @available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) public func chunks(ofCount count: Int, or timer: AsyncTimerSequence) -> AsyncChunksOfCountOrSignalSequence> { chunks(ofCount: count, or: timer, into: [Element].self) } + /// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type when an `AsyncTimerSequence` fires. @available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) public func chunked(by timer: AsyncTimerSequence, into: Collected.Type) -> AsyncChunksOfCountOrSignalSequence> where Collected.Element == Element { AsyncChunksOfCountOrSignalSequence(self, count: nil, signal: timer) } + /// Creates an asynchronous sequence that creates chunks when an `AsyncTimerSequence` fires. @available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) public func chunked(by timer: AsyncTimerSequence) -> AsyncChunksOfCountOrSignalSequence> { chunked(by: timer, into: [Element].self) } } +/// An `AsyncSequence` that chunks elements into collected `RangeReplaceableCollection` instances by either count or a signal from another `AsyncSequence`. public struct AsyncChunksOfCountOrSignalSequence: AsyncSequence, Sendable where Collected.Element == Base.Element, Base: Sendable, Signal: Sendable, Base.AsyncIterator: Sendable, Signal.AsyncIterator: Sendable, Base.Element: Sendable, Signal.Element: Sendable { public typealias Element = Collected + /// The iterator for a `AsyncChunksOfCountOrSignalSequence` instance. public struct Iterator: AsyncIteratorProtocol, Sendable { let count: Int? var state: Merge2StateMachine diff --git a/Sources/AsyncAlgorithms/AsyncChunksOfCountSequence.swift b/Sources/AsyncAlgorithms/AsyncChunksOfCountSequence.swift index 10edb0b..504f131 100644 --- a/Sources/AsyncAlgorithms/AsyncChunksOfCountSequence.swift +++ b/Sources/AsyncAlgorithms/AsyncChunksOfCountSequence.swift @@ -10,20 +10,24 @@ //===----------------------------------------------------------------------===// extension AsyncSequence { + /// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` of a given count. @inlinable public func chunks(ofCount count: Int, into: Collected.Type) -> AsyncChunksOfCountSequence where Collected.Element == Element { AsyncChunksOfCountSequence(self, count: count) } + /// Creates an asynchronous sequence that creates chunks of a given count. @inlinable public func chunks(ofCount count: Int) -> AsyncChunksOfCountSequence { chunks(ofCount: count, into: [Element].self) } } +/// An `AsyncSequence` that chunks elements into `RangeReplaceableCollection` instances of at least a given count. public struct AsyncChunksOfCountSequence: AsyncSequence where Collected.Element == Base.Element { public typealias Element = Collected + /// The iterator for a `AsyncChunksOfCountSequence` instance. @frozen public struct Iterator: AsyncIteratorProtocol {