Skip to content

Commit

Permalink
Promote Array extension to Sequence (#16)
Browse files Browse the repository at this point in the history
* Promote Array extension to Sequence

* Update test to show key, value access
  • Loading branch information
0xLeif authored Sep 22, 2022
1 parent 8ac6110 commit 7682617
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
extension Array {
/// Create a ``ForkedArray`` from the current `Array`
extension Sequence {
/// Create a ``ForkedArray`` from the current `Sequence`
public func fork<Output>(
filter: @escaping (Element) async throws -> Bool,
map: @escaping (Element) async throws -> Output
) -> ForkedArray<Element, Output> {
ForkedArray(
self,
Array(self),
filter: filter,
map: map
)
}

/// Create a ``ForkedArray`` from the current `Array`
/// Create a ``ForkedArray`` from the current `Sequence`
public func fork<Output>(
map: @escaping (Element) async throws -> Output
) -> ForkedArray<Element, Output> {
fork(filter: { _ in true }, map: map)
}

/// Create a ``ForkedArray`` from the current `Array` and get the Output Array
/// Create a ``ForkedArray`` from the current `Sequence` and get the Output Array
public func forked<Output>(
filter: @escaping (Element) async throws -> Bool,
map: @escaping (Element) async throws -> Output
) async throws -> [Output] {
try await ForkedArray(
self,
filter: filter,
map: map
)
.output()
try await fork(filter: filter, map: map).output()
}

/// Create a ``ForkedArray`` from the current `Array` and get the Output Array
/// Create a ``ForkedArray`` from the current `Sequence` and get the Output Array
public func forked<Output>(
map: @escaping (Element) async throws -> Output
) async throws -> [Output] {
Expand Down Expand Up @@ -59,7 +54,7 @@ extension Array {
try await fork(filter: isIncluded, map: identity).output()
}

/// Calls the given closure for each of the elements in the Array. This function uses ``ForkedArray`` and will be parallelized when possible.
/// Calls the given closure for each of the elements in the Sequence. This function uses ``ForkedArray`` and will be parallelized when possible.
public func asyncForEach(
_ transform: @escaping (Element) async throws -> Void
) async throws {
Expand Down
7 changes: 0 additions & 7 deletions Sources/Fork/Extensions/Task+Fork.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// Task+Fork.swift
//
//
// Created by Leif on 9/21/22.
//

extension Task where Success == Never, Failure == Never {
/// Run the async throwing task while checking for cancellation before and after.
@discardableResult
Expand Down
21 changes: 21 additions & 0 deletions Tests/ForkTests/ForkedArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,25 @@ class ForkedArrayTests: XCTestCase {

XCTAssertEqual(photos, photoNames)
}

func testForkedArraySet() async throws {
let set = Set(0 ..< 9)

let outputArray = try await set.asyncMap(identity)

XCTAssertEqual(outputArray, Array(set))
}

func testForkedArrayDictionary() async throws {
let dictionary: [String: String] = [:]

let outputArray = try await dictionary.forked(
filter: { (key: String, value: String) in
return true
},
map: identity
)

XCTAssert(type(of: outputArray) == [Dictionary<String, String>.Element].self)
}
}

0 comments on commit 7682617

Please sign in to comment.