From 6f0f1c12e634c9f55302e6e74e05400d60b24c9b Mon Sep 17 00:00:00 2001 From: Zach Date: Wed, 17 Aug 2022 19:03:14 -0600 Subject: [PATCH] Forked array cleanup (#8) * Update ForkedArray * Add test for ForkedArray --- Sources/Fork/ForkedArray.swift | 45 ++++++++++++++++++--------------- Tests/ForkTests/ForkTests.swift | 11 ++++++++ 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/Sources/Fork/ForkedArray.swift b/Sources/Fork/ForkedArray.swift index 31a2f00..4f54493 100644 --- a/Sources/Fork/ForkedArray.swift +++ b/Sources/Fork/ForkedArray.swift @@ -27,11 +27,13 @@ public struct ForkedArray { 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, @@ -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, @@ -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( diff --git a/Tests/ForkTests/ForkTests.swift b/Tests/ForkTests/ForkTests.swift index 401984e..a8e7e36 100644 --- a/Tests/ForkTests/ForkTests.swift +++ b/Tests/ForkTests/ForkTests.swift @@ -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) + } }