Created
March 22, 2015 13:06
-
-
Save grp/4d45868ac795d3af0bd7 to your computer and use it in GitHub Desktop.
Swift custom statements
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
// if | |
let a = 3 | |
let b = 3 | |
let c = 5 | |
// else if | |
// let a = 3 | |
// let b = 5 | |
// let c = 5 | |
// else | |
// let a = 3 | |
// let b = 4 | |
// let c = 5 | |
// MARK: Functions | |
var ran: Bool = false | |
func іf<T>(condition: Bool, x: Void -> T) -> Void { | |
ran = false | |
if condition { | |
x() | |
ran = true | |
} | |
} | |
func eⅼseіf<T>(condition: Bool, x: Void -> T) -> Void { | |
if !ran && condition { | |
x() | |
ran = true | |
} | |
} | |
func eⅼse<T>(x: Void -> T) -> Void { | |
if !ran { | |
x() | |
ran = true | |
} | |
} | |
іf (a == b) { | |
println("if") | |
}; eⅼseіf (b == c) { | |
println("else if") | |
}; eⅼse { | |
println("else") | |
} | |
// MARK: Methods | |
struct IfState { | |
let next: Bool | |
let ran: Bool | |
func іf<T>(x: Void -> T) -> IfState { | |
if next && !ran { | |
x() | |
return IfState(next: false, ran: true) | |
} else { | |
return IfState(next: false, ran: ran) | |
} | |
} | |
func eⅼse(x: Bool) -> IfState { | |
return IfState(next: x, ran: ran) | |
} | |
func eⅼse<T>(x: Void -> T) -> IfState { | |
if !ran { | |
x() | |
return IfState(next: false, ran: true) | |
} else { | |
return IfState(next: false, ran: ran) | |
} | |
} | |
} | |
extension Bool { | |
func іf<T>(x: Void -> T) -> IfState { | |
if self { | |
x() | |
return IfState(next: false, ran: true) | |
} else { | |
return IfState(next: false, ran: false) | |
} | |
} | |
} | |
(a == b).іf { | |
println("if") | |
}.eⅼse (b == c).іf { | |
println("else if") | |
}.eⅼse { | |
println("else") | |
} | |
// MARK: Operators | |
infix operator ¡⨍ { | |
associativity left | |
precedence 11 | |
} | |
func ¡⨍<T>(condition: Bool, block: Void -> T) -> Bool { | |
if condition { | |
block() | |
return true | |
} else { | |
return false | |
} | |
} | |
infix operator £¦§£ { | |
associativity left | |
precedence 10 | |
} | |
func £¦§£(ran1: Bool, ran2: Bool) -> Bool { | |
return ran1 || ran2 | |
} | |
func £¦§£<T>(ran: Bool, block: Void -> T) -> Bool { | |
if !ran { | |
block() | |
return true | |
} else { | |
return false | |
} | |
} | |
a == b ¡⨍ { | |
println("if") | |
} £¦§£ b == c ¡⨍ { | |
println("else if") | |
} £¦§£ { | |
println("else") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment