mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-24 04:17:52 +00:00
* Rebase guides WIP to avoid source changes * Add prose for debounce and throttle * Add an alterantive for debounce * Starts to prose for reductions * Add effect listings for all types * Add some examples for reductions * Update Guides/Channel.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Guides/Channel.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Guides/Channel.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Guides/Channel.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Guides/Collections.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Guides/Throttle.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Guides/Throttle.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Guides/Throttle.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Sources/AsyncAlgorithms/AsyncInclusiveReductionsSequence.swift Co-authored-by: Chris Adamson <invalidname@gmail.com> * Adjust events to elements for channels * Add a bit more detail to the README and a start to the guide for Task.select * Chunked updates * Add a getting started section to the README * Add some more prose for Channel * Update Guides/RemoveDuplicates.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Apply suggestions from code review Co-authored-by: Chris Adamson <invalidname@gmail.com> * Add some brief prose for Compacted * reductions could be named scan * spelling and correctness fixes * Apply @kperryua's suggestion for changing the intro of Compacted * Add some brief prose for Timer * Apply suggestions from code review for README Co-authored-by: Tony Parker <anthony.parker@apple.com> * Add some brief prose for RemoveDuplicates * Fleshing out of `Joined` Co-authored-by: Chris Adamson <invalidname@gmail.com> Co-authored-by: Kevin Perry <kperry@apple.com> Co-authored-by: Tony Parker <anthony.parker@apple.com>
86 lines
2.7 KiB
Swift
86 lines
2.7 KiB
Swift
extension AsyncSequence {
|
|
/// Returns an asynchronous sequence containing the accumulated results of combining the
|
|
/// elements of the asynchronous sequence using the given closure.
|
|
///
|
|
/// This can be seen as applying the reduce function to each element and
|
|
/// producing an asynchronous sequence consisting of the initial value followed
|
|
/// by these results.
|
|
///
|
|
/// ```
|
|
/// let runningTotal = [1, 2, 3, 4].async.reductions(+)
|
|
/// print(await Array(runningTotal))
|
|
///
|
|
/// // prints [1, 3, 6, 10]
|
|
/// ```
|
|
///
|
|
/// - Parameter transform: A closure that combines the previously reduced
|
|
/// result and the next element in the receiving sequence, and returns
|
|
/// the result.
|
|
/// - Returns: An asynchronous sequence of the reduced elements.
|
|
@inlinable
|
|
public func reductions(
|
|
_ transform: @Sendable @escaping (Element, Element) async -> Element
|
|
) -> AsyncInclusiveReductionsSequence<Self> {
|
|
AsyncInclusiveReductionsSequence(self, transform: transform)
|
|
}
|
|
}
|
|
|
|
@frozen
|
|
public struct AsyncInclusiveReductionsSequence<Base: AsyncSequence> {
|
|
@usableFromInline
|
|
let base: Base
|
|
|
|
@usableFromInline
|
|
let transform: @Sendable (Base.Element, Base.Element) async -> Base.Element
|
|
|
|
@inlinable
|
|
init(_ base: Base, transform: @Sendable @escaping (Base.Element, Base.Element) async -> Base.Element) {
|
|
self.base = base
|
|
self.transform = transform
|
|
}
|
|
}
|
|
|
|
extension AsyncInclusiveReductionsSequence: AsyncSequence {
|
|
public typealias Element = Base.Element
|
|
|
|
@frozen
|
|
public struct Iterator: AsyncIteratorProtocol {
|
|
@usableFromInline
|
|
internal var iterator: Base.AsyncIterator
|
|
|
|
@usableFromInline
|
|
internal var element: Base.Element?
|
|
|
|
@usableFromInline
|
|
internal let transform: @Sendable (Base.Element, Base.Element) async -> Base.Element
|
|
|
|
@inlinable
|
|
internal init(
|
|
_ iterator: Base.AsyncIterator,
|
|
transform: @Sendable @escaping (Base.Element, Base.Element) async -> Base.Element
|
|
) {
|
|
self.iterator = iterator
|
|
self.transform = transform
|
|
}
|
|
|
|
@inlinable
|
|
public mutating func next() async rethrows -> Base.Element? {
|
|
guard let previous = element else {
|
|
element = try await iterator.next()
|
|
return element
|
|
}
|
|
guard let next = try await iterator.next() else { return nil }
|
|
element = await transform(previous, next)
|
|
return element
|
|
}
|
|
}
|
|
|
|
@inlinable
|
|
public func makeAsyncIterator() -> Iterator {
|
|
Iterator(base.makeAsyncIterator(), transform: transform)
|
|
}
|
|
}
|
|
|
|
extension AsyncInclusiveReductionsSequence: Sendable where Base: Sendable { }
|
|
extension AsyncInclusiveReductionsSequence.Iterator: Sendable where Base.AsyncIterator: Sendable, Base.Element: Sendable { }
|