Skip to content

Instantly share code, notes, and snippets.

@simonbromberg
Created May 17, 2022 20:47
Show Gist options
  • Save simonbromberg/f8fbd80fb089f1bfad0e3c722808839e to your computer and use it in GitHub Desktop.
Save simonbromberg/f8fbd80fb089f1bfad0e3c722808839e to your computer and use it in GitHub Desktop.
BoolMap
public extension Bool {
func map<T>(
_ closure: () -> T?,
else: (() -> T?)? = nil
) -> T? {
self ? closure() : `else`?()
}
func map<T>(
_ closure: () -> T,
else: () -> T
) -> T {
self ? closure() : `else`()
}
func map<T>(
_ closure: () async throws -> T,
else: (() async throws -> T)? = nil
) async throws -> T? {
try self ? await closure() : await `else`?()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment