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
struct TabBar: View {
@State private var selectedTab: Tab = .overview
@Namespace private var ns
enum Tab {
case overview, data, graphs, settings
}
var body: some View {