Last active
January 17, 2021 03:40
-
-
Save guoye-zhang/114248e384e577813924 to your computer and use it in GitHub Desktop.
Implementing if without using if in Swift
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
func i(_ condition: Bool, then: () -> ()) -> (() -> ()) -> () { | |
func run(_ condition: Bool) -> (() -> ()) -> () { | |
return { _ = condition && $0() as Any is () } | |
} | |
run(condition)(then) | |
return run(!condition) | |
} | |
// Usage: | |
let els = i(true) { | |
print("a") | |
} | |
els { | |
print("b") | |
} | |
i(true) { | |
// true | |
} ( { | |
// false | |
} ) | |
(i(false) { | |
// true | |
} ) { | |
// false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment