Skip to content

Commit

Permalink
Forked array cleanup (#8)
Browse files Browse the repository at this point in the history
* Update ForkedArray

* Add test for ForkedArray
  • Loading branch information
0xLeif authored Aug 18, 2022
1 parent 2346454 commit 6f0f1c1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
45 changes: 24 additions & 21 deletions Sources/Fork/ForkedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ public struct ForkedArray<Value, Output> {
self.filter = filter
self.output = output

let forkType = ForkedArray.split(array: array)

switch forkType {
switch ForkedArray.split(array: array) {
case .none:
self.fork = Fork(value: array, leftOutput: { _ in .none }, rightOutput: { _ in .none })
self.fork = Fork(
value: array,
leftOutput: { _ in .none },
rightOutput: { _ in .none }
)
case .single(let value):
self.fork = Fork(
value: value,
Expand Down Expand Up @@ -61,11 +63,12 @@ extension ForkedArray {
array: [Value]
) -> ForkType {
let count = array.count

guard count > 0 else { return .none }
guard count > 1 else { return .single(array[0]) }

if count == 2 {
switch count {
case 0:
return .none
case 1:
return .single(array[0])
case 2:
return .fork(
Fork(
value: array,
Expand All @@ -75,19 +78,19 @@ extension ForkedArray {
rightOutput: ForkType.single
)
)
}

let midPoint = count / 2

return .fork(
Fork(
value: array,
leftInputMap: { Array($0[0 ..< midPoint]) },
rightInputMap: { Array($0[midPoint ... count - 1]) },
leftOutput: split(array:),
rightOutput: split(array:)
default:
let midPoint = count / 2
return .fork(
Fork(
value: array,
leftInputMap: { Array($0[0 ..< midPoint]) },
rightInputMap: { Array($0[midPoint ... count - 1]) },
leftOutput: split(array:),
rightOutput: split(array:)
)
)
)
}
}

private static func output(
Expand Down
11 changes: 11 additions & 0 deletions Tests/ForkTests/ForkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,15 @@ final class ForkTests: XCTestCase {

XCTAssertEqual(photos, photoNames)
}

func testExample_x() async throws {
let photoNames = (0 ... Int.random(in: 1 ..< 100)).map(\.description)
@Sendable func downloadPhoto(named: String) async -> String { named }
func show(_ photos: [String]) { }

let forkedArray = ForkedArray(photoNames, output: downloadPhoto(named:))
let photos = try await forkedArray.output()

XCTAssertEqual(photos, photoNames)
}
}

0 comments on commit 6f0f1c1

Please sign in to comment.