Created
April 25, 2025 20:40
-
-
Save iharkatkavets/6c502b34559e586ddedc6d596cbda40e to your computer and use it in GitHub Desktop.
Task related to understanding Swift structured concurrency
This file contains hidden or 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
import Foundation | |
// TASK | |
// Complete function `process(...)` | |
func processData(_ v: Int) async throws -> Int { | |
print("<-start \"\(v)\"...") | |
let s: Int = 5 // .random(in: 0...10) | |
try await Task.sleep(for: .seconds(s)) | |
print("->finish \"\(v)\" (took \(s) sec).") | |
return v | |
} | |
@main | |
struct MyApp { | |
static func main() async throws { | |
let inStream = AsyncStream(Int.self) { | |
for i in 1...10 { | |
$0.yield(i) | |
} | |
$0.finish() | |
} | |
let start = Date() | |
let outStream = process(inStream, 3) | |
for try await v in outStream { | |
print("get result from \"\(v)\"") | |
} | |
print("app duration: \(Int(Date().timeIntervalSince(start))) sec") | |
} | |
} | |
func process( | |
_ inStream: AsyncStream<Int>, | |
_ concurrentOperationsLimit: Int | |
) -> AsyncThrowingStream<Int, any Error> { | |
// TODO | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment