-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1943d5d
commit 703e0d2
Showing
9 changed files
with
122 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
Sources/BidirectionalStream/BidirectionalStream.docc/LandingPage.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# ``BidirectionalStream`` | ||
|
||
`BidirectionalStream` aims to bring features similar to Python's Generator to Swift. Users can generate values using `yield`, send values back with `send`, and close the stream by throwing a `StopIteration` error. | ||
|
||
## Overview | ||
|
||
Inspired by Python generators, which not only can use `yield` to produce values, but also can use `send` to receive values, and `return` to raise a `StopIteration` error and halt the stream, the ``BidirectionalSyncStream`` and ``BidirectionalAsyncStream`` in Swift offer similar features for synchronous and asynchronous operations respectively. | ||
|
||
For more information about the generator in python, See: [PEP 255](https://peps.python.org/pep-0255/), [PEP 342](https://peps.python.org/pep-0342/#new-generator-method-send-value), [Doc](https://docs.python.org/3/reference/expressions.html#generator-iterator-methods) | ||
|
||
## Getting Started | ||
|
||
In the following example, the stream uses the `send(_:)` method to send a value back to the stream, which is received by the `yield(_:)` return value. | ||
|
||
```swift | ||
let stream = BidirectionalSyncStream<Int, Int, NoneType> { continuation in | ||
var value = 0 | ||
while true { | ||
value = continuation.yield(value) | ||
value += 1 | ||
} | ||
} | ||
|
||
try stream.next() // 0 start the stream | ||
try stream.send(5) // 6 send value back to the stream, and get the next value | ||
``` | ||
|
||
In the following example, when the stream's closure uses `return(_:)` to stop the streaming process, a `StopIteration` error containing the return value will be caught outside the closure. | ||
|
||
```swift | ||
let stream = BidirectionalSyncStream<Int, Int, String> { continuation in | ||
var value = 0 | ||
while true { | ||
value = continuation.yield(value) | ||
value += 1 | ||
if value == 5 { | ||
continuation.return("Stop") | ||
} | ||
} | ||
} | ||
|
||
try stream.next() // 0 start the stream | ||
do { | ||
try stream.send(4) // Throw StopIteration error | ||
} catch { | ||
// get return value | ||
print((error as! StopIteration).value) // "Stop" | ||
} | ||
``` | ||
|
||
## Topics | ||
|
||
### Supporting Types | ||
|
||
+ ``NoneType`` | ||
+ ``StopIteration`` | ||
+ ``Terminated`` | ||
+ ``WrongStreamUse`` | ||
+ ``AsyncSemaphore`` | ||
|
||
### BidirectionalStream | ||
|
||
+ ``BidirectionalSyncStream`` | ||
+ ``BidirectionalAsyncStream`` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 0 additions & 67 deletions
67
Sources/SyncStream/SyncStream.docc/BidirectionalSyncStream.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# ``SyncStream`` | ||
|
||
`SyncStream` is a class that generates a sequence of values, inspired by `AsyncStream` from the swift standard library. | ||
|
||
## Overview | ||
|
||
[`AsyncStream`](https://developer.apple.com/documentation/swift/asyncstream) offers a convenient method to create a sequence from a closure that invokes a continuation to generate elements. However, in certain cases, you may need to produce a sequence synchronously using a closure. To address this need, we introduce [`SyncStream`](syncstream/syncstream), which shares the same API as `AsyncStream` but operates synchronously. | ||
|
||
Here is a simple example of how to use `SyncStream`: | ||
|
||
```swift | ||
let stream = SyncStream<Int> { continuation in | ||
for i in 0 ..< 10 { | ||
continuation.yield(i) | ||
} | ||
continuation.finish() | ||
} | ||
|
||
for value in stream { | ||
print(value, terminator: " ") | ||
} | ||
// 0 1 2 3 4 5 6 7 8 9 | ||
``` | ||
|
||
## Topics | ||
|
||
+ ``SyncStream-class`` | ||
+ ``SyncStream/Continuation`` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters