Skip to content

AsyncLazySequence proposal #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Evolution/NNNN-lazy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# AsyncLazySequence

* Proposal: [NNNN](NNNN-lazy.md)
* Authors: [Philippe Hausler](https://github.com/phausler)
* Status: **Implemented**

* Implementation:
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncLazySequence.swift) |
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestLazy.swift)

## Introduction

`AsyncLazySequence` converts a non-asynchronous sequence into an asynchronous one.

This operation is available for all `Sequence` types.

```swift
let numbers = [1, 2, 3, 4].async
let characters = "abcde".async
```

This transformation can be useful to test operations specifically available on `AsyncSequence` but also is useful
to combine with other `AsyncSequence` types to provide well known sources of data.

The `.async` property returns an `AsyncLazySequence` that is generic upon the base `Sequence` it was constructed from.

```swift
extension Sequence {
public var async: AsyncLazySequence<Self> { get }
}

public struct AsyncLazySequence<Base: Sequence>: AsyncSequence {
...
}

extension AsyncLazySequence: Sendable where Base: Sendable { }
extension AsyncLazySequence.Iterator: Sendable where Base.Iterator: Sendable { }
```

### Naming

This property's and type's name match the naming approaches in the Swift standard library. The property is named with a
succinct name in inspiration from `.lazy`, and the type is named in reference to the lazy behavior of the constructed
`AsyncSequence`.

## Effect on API resilience

`AsyncLazySequence` has a trivial implementation and is marked as `@frozen` and `@inlinable`. This removes the ability of this type and functions to be ABI resilient boundaries at the benefit of being highly optimizable.