Skip to content

Commit 7682617

Browse files
authored
Promote Array extension to Sequence (#16)
* Promote Array extension to Sequence * Update test to show key, value access
1 parent 8ac6110 commit 7682617

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

Sources/Fork/Extensions/Array+ForkedArray.swift renamed to Sources/Fork/Extensions/Sequence+ForkedArray.swift

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
1-
extension Array {
2-
/// Create a ``ForkedArray`` from the current `Array`
1+
extension Sequence {
2+
/// Create a ``ForkedArray`` from the current `Sequence`
33
public func fork<Output>(
44
filter: @escaping (Element) async throws -> Bool,
55
map: @escaping (Element) async throws -> Output
66
) -> ForkedArray<Element, Output> {
77
ForkedArray(
8-
self,
8+
Array(self),
99
filter: filter,
1010
map: map
1111
)
1212
}
1313

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

21-
/// Create a ``ForkedArray`` from the current `Array` and get the Output Array
21+
/// Create a ``ForkedArray`` from the current `Sequence` and get the Output Array
2222
public func forked<Output>(
2323
filter: @escaping (Element) async throws -> Bool,
2424
map: @escaping (Element) async throws -> Output
2525
) async throws -> [Output] {
26-
try await ForkedArray(
27-
self,
28-
filter: filter,
29-
map: map
30-
)
31-
.output()
26+
try await fork(filter: filter, map: map).output()
3227
}
3328

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

62-
/// Calls the given closure for each of the elements in the Array. This function uses ``ForkedArray`` and will be parallelized when possible.
57+
/// Calls the given closure for each of the elements in the Sequence. This function uses ``ForkedArray`` and will be parallelized when possible.
6358
public func asyncForEach(
6459
_ transform: @escaping (Element) async throws -> Void
6560
) async throws {

Sources/Fork/Extensions/Task+Fork.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// Task+Fork.swift
3-
//
4-
//
5-
// Created by Leif on 9/21/22.
6-
//
7-
81
extension Task where Success == Never, Failure == Never {
92
/// Run the async throwing task while checking for cancellation before and after.
103
@discardableResult

Tests/ForkTests/ForkedArrayTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,25 @@ class ForkedArrayTests: XCTestCase {
9292

9393
XCTAssertEqual(photos, photoNames)
9494
}
95+
96+
func testForkedArraySet() async throws {
97+
let set = Set(0 ..< 9)
98+
99+
let outputArray = try await set.asyncMap(identity)
100+
101+
XCTAssertEqual(outputArray, Array(set))
102+
}
103+
104+
func testForkedArrayDictionary() async throws {
105+
let dictionary: [String: String] = [:]
106+
107+
let outputArray = try await dictionary.forked(
108+
filter: { (key: String, value: String) in
109+
return true
110+
},
111+
map: identity
112+
)
113+
114+
XCTAssert(type(of: outputArray) == [Dictionary<String, String>.Element].self)
115+
}
95116
}

0 commit comments

Comments
 (0)