Skip to content

Instantly share code, notes, and snippets.

@0xLeif
Created March 16, 2019 02:45
Show Gist options
  • Save 0xLeif/a4ef8768cd79fef67e70831eb0af509a to your computer and use it in GitHub Desktop.
Save 0xLeif/a4ef8768cd79fef67e70831eb0af509a to your computer and use it in GitHub Desktop.
Conditional
extension Bool {
func `if`(_ block: () -> Bool) -> Bool {
return self ? block() : self
}
func `true`(_ block: () -> Void) -> Bool {
if self {
block()
}
return self
}
func `false`(_ block: () -> Void) {
if !self {
block()
}
}
}
(6 == 5)
.if { return 6 == 6 }
.true { print(true) }
.false { print(false) }
let boolValue = "1234" == "1234"
var count = 0
boolValue.if {
count += 1
return count == 4
}.true {
count += 2
}.false {
count += 3
}
print(count)
let isUserAuthenticated = false
let isProcessing = false
isUserAuthenticated
.if { !isProcessing }
.true { print("Login") }
.false { print("Blocked") }
func test() {
print("Loaded")
}
isProcessing
.true { print("Loading...") }
.false(test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment