|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Async Algorithms open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +/// An `AsyncSequence` that iterates over the adjacent pairs of the original |
| 13 | +/// `AsyncSequence`. |
| 14 | +@frozen |
| 15 | +public struct AdjacentPairsSequence<Base: AsyncSequence>: AsyncSequence { |
| 16 | + public typealias Element = (Base.Element, Base.Element) |
| 17 | + |
| 18 | + @usableFromInline |
| 19 | + let base: Base |
| 20 | + |
| 21 | + @inlinable |
| 22 | + init(_ base: Base) { |
| 23 | + self.base = base |
| 24 | + } |
| 25 | + |
| 26 | + /// The iterator for an `AdjacentPairsSequence` instance. |
| 27 | + @frozen |
| 28 | + public struct Iterator: AsyncIteratorProtocol { |
| 29 | + public typealias Element = (Base.Element, Base.Element) |
| 30 | + |
| 31 | + @usableFromInline |
| 32 | + var base: Base.AsyncIterator |
| 33 | + |
| 34 | + @usableFromInline |
| 35 | + internal var previousElement: Base.Element? |
| 36 | + |
| 37 | + @inlinable |
| 38 | + init(_ base: Base.AsyncIterator) { |
| 39 | + self.base = base |
| 40 | + } |
| 41 | + |
| 42 | + @inlinable |
| 43 | + public mutating func next() async rethrows -> (Base.Element, Base.Element)? { |
| 44 | + if previousElement == nil { |
| 45 | + previousElement = try await base.next() |
| 46 | + } |
| 47 | + |
| 48 | + guard let previous = previousElement, let next = try await base.next() else { |
| 49 | + return nil |
| 50 | + } |
| 51 | + |
| 52 | + previousElement = next |
| 53 | + return (previous, next) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @inlinable |
| 58 | + public func makeAsyncIterator() -> Iterator { |
| 59 | + Iterator(base.makeAsyncIterator()) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +extension AsyncSequence { |
| 64 | + /// An `AsyncSequence` that iterates over the adjacent pairs of the original |
| 65 | + /// original `AsyncSequence`. |
| 66 | + /// |
| 67 | + /// ``` |
| 68 | + /// for await (first, second) in (1...5).async.adjacentPairs() { |
| 69 | + /// print("First: \(first), Second: \(second)") |
| 70 | + /// } |
| 71 | + /// |
| 72 | + /// // First: 1, Second: 2 |
| 73 | + /// // First: 2, Second: 3 |
| 74 | + /// // First: 3, Second: 4 |
| 75 | + /// // First: 4, Second: 5 |
| 76 | + /// ``` |
| 77 | + /// |
| 78 | + /// - Returns: An `AsyncSequence` where the element is a tuple of two adjacent elements |
| 79 | + /// or the original `AsyncSequence`. |
| 80 | + @inlinable |
| 81 | + public func adjacentPairs() -> AdjacentPairsSequence<Self> { |
| 82 | + AdjacentPairsSequence(self) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +extension AdjacentPairsSequence: Sendable where Base: Sendable, Base.Element: Sendable, Base.AsyncIterator: Sendable { } |
| 87 | +extension AdjacentPairsSequence.Iterator: Sendable where Base: Sendable, Base.Element: Sendable, Base.AsyncIterator: Sendable { } |
0 commit comments