-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArray+Extensions.swift
More file actions
58 lines (50 loc) · 1.7 KB
/
Array+Extensions.swift
File metadata and controls
58 lines (50 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// Array+Extensions.swift
// NextcloudFileProviderKit
//
// Created by Claudio Cambra on 2024-12-18.
//
fileprivate let defaultChunkSize = 64
extension Array {
func chunked(into size: Int = defaultChunkSize) -> [ArraySlice<Element>] {
guard size > 0 else { return [] } // Avoid invalid chunk sizes
return stride(from: 0, to: count, by: size).map { startIndex in
self[startIndex..<Swift.min(startIndex + size, count)]
}
}
func chunkedMap<T>(into size: Int = defaultChunkSize, transform: (Element) -> T) -> [[T]] {
return self.chunked(into: size).map { chunk in
chunk.map(transform)
}
}
func concurrentChunkedForEach(
into size: Int = defaultChunkSize, operation: @escaping (Element) async -> Void
) async {
await withTaskGroup(of: Void.self) { group in
for chunk in chunked(into: size) {
group.addTask {
for element in chunk {
await operation(element)
}
}
}
}
}
func concurrentChunkedCompactMap<T>(
into size: Int = defaultChunkSize, transform: @escaping (Element) throws -> T?
) async throws -> [T] {
try await withThrowingTaskGroup(of: [T].self) { group in
var results = [T]()
results.reserveCapacity(self.count)
for chunk in chunked(into: size) {
group.addTask {
return try chunk.compactMap { try transform($0) }
}
}
for try await chunkResult in group {
results += chunkResult
}
return results
}
}
}