Last active
November 7, 2015 06:53
-
-
Save rjchatfield/df781f490286053cf2d7 to your computer and use it in GitHub Desktop.
Do, If/ElseIf/If, While, Repeat/While, For in Swift (only using switch cases and recursion)
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
typealias VoidFuncType = () -> Void | |
typealias BoolFuncType = () -> Bool | |
/** | |
IF/ELSE STATEMENT | |
*/ | |
typealias ElseIfType = (Bool, VoidFuncType) -> IfElse | |
typealias ElseType = (VoidFuncType) -> Void | |
struct IfElse { | |
let else_if_: ElseIfType | |
let else_ : ElseType | |
} | |
func else_(f: VoidFuncType) { f() } | |
func noElse_(_: VoidFuncType) -> Void { return } | |
func noElseIf_(_: Bool, _: VoidFuncType) -> IfElse { | |
return IfElse(else_if_: noElseIf_, else_: noElse_) | |
} | |
func if_(cond: Bool, f:VoidFuncType) -> IfElse { | |
switch cond { | |
case true: | |
f() | |
return IfElse(else_if_: noElseIf_, else_: noElse_) | |
case false: | |
return IfElse(else_if_: if_, else_: else_) | |
} | |
} | |
//-- IF | |
if_(true) { | |
print("Hi IF 1") | |
} | |
.else_if_(false) { | |
print("Hi") | |
} | |
.else_if_(false) { | |
print("Hi ELSE IF 1") | |
} | |
.else_{ | |
print("Hi ELSE 1") | |
} | |
//-> "Hi IF 1" | |
//-- ELSE IF | |
if_(false) { | |
print("Hi IF 2") | |
} | |
.else_if_(false) { | |
print("Hi") | |
} | |
.else_if_(true) { | |
print("Hi ELSE IF 2") | |
} | |
.else_{ | |
print("Hi ELSE 2") | |
} | |
//-> "Hi ELSE IF 2" | |
//-- ELSE | |
if_(false) { | |
print("Hi IF 3") | |
} | |
.else_if_(false) { | |
print("Hi") | |
} | |
.else_if_(false) { | |
print("Hi ELSE IF 3") | |
} | |
.else_{ | |
print("Hi ELSE 3") | |
} | |
//-> "Hi ELSE 3" | |
print("") | |
/** | |
WHILE STATEMENT | |
*/ | |
private func whileLogic(@autoclosure cond: BoolFuncType, @noescape f:VoidFuncType) { | |
switch cond() { | |
case true: | |
f() | |
while_(cond, f: f) | |
case false: | |
return | |
} | |
} | |
func while_(@autoclosure cond: BoolFuncType, @noescape f:VoidFuncType) { | |
whileLogic(cond, f: f) | |
} | |
var i = 0 | |
while_(i < 10) { | |
print("\(i) ") | |
i++ | |
} | |
//-> "0 1 2 3 4 5 6 7 8 9" | |
print("") | |
/** | |
REPEAT WHILE STATEMENT | |
*/ | |
struct DoWhile { | |
let f: VoidFuncType | |
func while_(@autoclosure cond: BoolFuncType) { | |
whileLogic(cond, f: f) | |
} | |
} | |
func do_(f:VoidFuncType) -> DoWhile { | |
f() | |
return DoWhile(f: f) | |
} | |
i = 0 | |
do_{ | |
print("\(i) ") | |
i++ | |
}.while_(i < 10) | |
//-> "0 1 2 3 4 5 6 7 8 9" | |
print("") | |
/** | |
FOR STATEMENT | |
*/ | |
func for_( | |
@autoclosure initialiser: VoidFuncType, | |
@autoclosure _ cond: BoolFuncType, | |
@autoclosure _ incrementer: ()->Any?, | |
f:VoidFuncType | |
) { | |
initialiser() | |
whileLogic(cond) { | |
f() | |
incrementer() | |
} | |
} | |
for_(i = 0, i < 10, i++) { | |
print("\(i) ") | |
} | |
//-> "0 1 2 3 4 5 6 7 8 9" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment