Skip to content

Instantly share code, notes, and snippets.

@guoye-zhang
Last active January 17, 2021 03:40
Show Gist options
  • Save guoye-zhang/114248e384e577813924 to your computer and use it in GitHub Desktop.
Save guoye-zhang/114248e384e577813924 to your computer and use it in GitHub Desktop.
Implementing if without using if in Swift
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