Skip to content

Commit 8fff92e

Browse files
authored
Add async and throwing capability to value closure for Fork (#12)
* Add async and throwing capability to value closure for Fork * Remove await and prefer parallel await using async let
1 parent 6aec4eb commit 8fff92e

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

Sources/Fork/Fork.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public struct Fork<LeftOutput, RightOutput> {
3939
/// - leftOutput: An `async` closure that uses `LeftInput` to return `LeftOutput`
4040
/// - rightOutput: An `async` closure that uses `RightInput` to return `RightOutput`
4141
public init<Value, LeftInput, RightInput>(
42-
value: @escaping () -> Value,
42+
value: @escaping () async throws -> Value,
4343
leftInputMap: @escaping (Value) throws -> LeftInput,
4444
rightInputMap: @escaping (Value) throws -> RightInput,
4545
leftOutput: @escaping (LeftInput) async throws -> LeftOutput,
@@ -91,7 +91,7 @@ public struct Fork<LeftOutput, RightOutput> {
9191
/// - leftOutput: An `async` closure that uses `LeftInput` to return `LeftOutput`
9292
/// - rightOutput: An `async` closure that uses `RightInput` to return `RightOutput`
9393
public init<Value>(
94-
value: @escaping () -> Value,
94+
value: @escaping () async throws -> Value,
9595
leftOutput: @escaping (Value) async throws -> LeftOutput,
9696
rightOutput: @escaping (Value) async throws -> RightOutput
9797
) {

Sources/Fork/ForkedArray.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,12 @@ public struct ForkedArray<Value, Output> {
5858

5959
/// Asynchronously resolve the forked array
6060
public func output() async throws -> [Output] {
61-
try await fork.merged(
62-
using: { leftForkType, rightForkType in
63-
async let leftOutput = try await ForkedArray.output(for: leftForkType, isIncluded: filter, transform: map)
64-
async let rightOutput = try await ForkedArray.output(for: rightForkType, isIncluded: filter, transform: map)
65-
66-
return try await leftOutput + rightOutput
67-
}
68-
)
61+
try await fork.merged { leftForkType, rightForkType in
62+
async let leftOutput = try ForkedArray.output(for: leftForkType, isIncluded: filter, transform: map)
63+
async let rightOutput = try ForkedArray.output(for: rightForkType, isIncluded: filter, transform: map)
64+
65+
return try await leftOutput + rightOutput
66+
}
6967
}
7068
}
7169

0 commit comments

Comments
 (0)