Skip to content

Instantly share code, notes, and snippets.

View AndrewBarba's full-sized avatar

Andrew Barba AndrewBarba

View GitHub Profile
@AndrewBarba
AndrewBarba / AsyncMap.swift
Created June 20, 2021 03:18
Concurrent async version of map on any Sequence
extension Sequence {
func mapAsync<T>(_ handler: @escaping (Element) async -> T) async -> [T] {
return await withTaskGroup(of: (Int, T).self) { group in
var results: [Int: T] = [:]
for (index, item) in self.enumerated() {
group.async { (index, await handler(item)) }
}
for await (index, transformed) in group {
results[index] = transformed