Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adam-fowler/async-collections
Swift concurrency collection support
https://github.com/adam-fowler/async-collections
async-await collections concurrency swift
Last synced: about 2 months ago
JSON representation
Swift concurrency collection support
- Host: GitHub
- URL: https://github.com/adam-fowler/async-collections
- Owner: adam-fowler
- License: apache-2.0
- Created: 2021-11-14T21:33:05.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-29T14:53:25.000Z (8 months ago)
- Last Synced: 2024-10-27T09:48:43.800Z (about 2 months ago)
- Topics: async-await, collections, concurrency, swift
- Language: Swift
- Homepage:
- Size: 32.2 KB
- Stars: 30
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AsyncCollections
Functions for running async processes on Swift Collections
## ForEach
Run an async function on every element of a Sequence.
```swift
await array.asyncForEach {
await asyncProcess($0)
}
```
The async closures are run serially ie the closure run on the current element of a sequence has to finish before we run the closure on the next element.To run the closures concurrently use
```swift
try await array.concurrentForEach {
try await asyncProcess($0)
}
```
You can manage the number of tasks running at any one time with the `maxConcurrentTasks` parameter
```swift
try await array.concurrentForEach(maxConcurrentTasks: 4) {
try await asyncProcess($0)
}
```## Map
Return an array transformed by an async function.
```swift
let result = await array.asyncMap {
return await asyncTransform($0)
}
```Similar to `asyncForEach` there are versions of `asyncMap` that runs the transforms concurrently.
```swift
let result = await array.concurrentMap(maxConcurrentTasks: 8) {
return await asyncTransform($0)
}
```## Compact Map
Return a non-optional array transformed by an async function returning optional results.
```swift
let result: [MyType] = await array.asyncCompactMap { value -> MyType? in
return await asyncTransform(value)
}
```Similar to `asyncForEach` there are versions of `asyncCompactMap` that runs the transforms concurrently.
```swift
let result: [MyType] = await array.concurrentCompactMap(maxConcurrentTasks: 8) { value -> MyType? in
return await asyncTransform(value)
```## FlatMap
Return a concatenated array transformed by an async function that returns a sequence.
```swift
let result: [MySequence.Element] = await array.asyncMap { value -> MySequence in
return await asyncTransform($0)
}
```Similar to `asyncForEach` there are versions of `asyncFlatMap` that runs the transforms concurrently.
```swift
let result: [MySequence.Element] = await array.concurrentMap(maxConcurrentTasks: 8) { value -> MySequence in
return await asyncTransform($0)
}
```## Filter
Return a filtered array transformed by an async function.
```swift
let result = await array.asyncFilter {
return await asyncTransform($0)
}
```Similar to `asyncForEach` there are versions of `asyncFilter` that runs the transforms concurrently.
```swift
let result = await array.concurrentFilter(maxConcurrentTasks: 8) {
return await asyncTransform($0)
}
```