Last active
March 14, 2024 00:41
-
-
Save tkersey/52ecd34ace5b7226ddd6adf03cfbefa2 to your computer and use it in GitHub Desktop.
Missing async functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Optional { | |
func map<T>( | |
transform: (Wrapped) async throws -> T | |
) async rethrows -> T? { | |
guard case let .some(value) = self else { return nil } | |
return try await transform(value) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Sequence { | |
func map<T>( | |
_ transform: (Element) async throws -> T | |
) async rethrows -> [T] { | |
var values = [T]() | |
for element in self { | |
try await values.append(transform(element)) | |
} | |
return values | |
} | |
func compactMap<T>( | |
_ transform: (Element) async throws -> T? | |
) async rethrows -> [T] { | |
var values = [T]() | |
for element in self { | |
if let newElement = try await transform(element) { | |
values.append(newElement) | |
} | |
} | |
return values | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment