Created
May 17, 2022 20:47
-
-
Save simonbromberg/f8fbd80fb089f1bfad0e3c722808839e to your computer and use it in GitHub Desktop.
BoolMap
This file contains 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
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