Skip to content

Commit 6f0f1c1

Browse files
authored
Forked array cleanup (#8)
* Update ForkedArray * Add test for ForkedArray
1 parent 2346454 commit 6f0f1c1

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

Sources/Fork/ForkedArray.swift

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ public struct ForkedArray<Value, Output> {
2727
self.filter = filter
2828
self.output = output
2929

30-
let forkType = ForkedArray.split(array: array)
31-
32-
switch forkType {
30+
switch ForkedArray.split(array: array) {
3331
case .none:
34-
self.fork = Fork(value: array, leftOutput: { _ in .none }, rightOutput: { _ in .none })
32+
self.fork = Fork(
33+
value: array,
34+
leftOutput: { _ in .none },
35+
rightOutput: { _ in .none }
36+
)
3537
case .single(let value):
3638
self.fork = Fork(
3739
value: value,
@@ -61,11 +63,12 @@ extension ForkedArray {
6163
array: [Value]
6264
) -> ForkType {
6365
let count = array.count
64-
65-
guard count > 0 else { return .none }
66-
guard count > 1 else { return .single(array[0]) }
67-
68-
if count == 2 {
66+
switch count {
67+
case 0:
68+
return .none
69+
case 1:
70+
return .single(array[0])
71+
case 2:
6972
return .fork(
7073
Fork(
7174
value: array,
@@ -75,19 +78,19 @@ extension ForkedArray {
7578
rightOutput: ForkType.single
7679
)
7780
)
78-
}
79-
80-
let midPoint = count / 2
81-
82-
return .fork(
83-
Fork(
84-
value: array,
85-
leftInputMap: { Array($0[0 ..< midPoint]) },
86-
rightInputMap: { Array($0[midPoint ... count - 1]) },
87-
leftOutput: split(array:),
88-
rightOutput: split(array:)
81+
default:
82+
let midPoint = count / 2
83+
84+
return .fork(
85+
Fork(
86+
value: array,
87+
leftInputMap: { Array($0[0 ..< midPoint]) },
88+
rightInputMap: { Array($0[midPoint ... count - 1]) },
89+
leftOutput: split(array:),
90+
rightOutput: split(array:)
91+
)
8992
)
90-
)
93+
}
9194
}
9295

9396
private static func output(

Tests/ForkTests/ForkTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,15 @@ final class ForkTests: XCTestCase {
138138

139139
XCTAssertEqual(photos, photoNames)
140140
}
141+
142+
func testExample_x() async throws {
143+
let photoNames = (0 ... Int.random(in: 1 ..< 100)).map(\.description)
144+
@Sendable func downloadPhoto(named: String) async -> String { named }
145+
func show(_ photos: [String]) { }
146+
147+
let forkedArray = ForkedArray(photoNames, output: downloadPhoto(named:))
148+
let photos = try await forkedArray.output()
149+
150+
XCTAssertEqual(photos, photoNames)
151+
}
141152
}

0 commit comments

Comments
 (0)