Created
August 20, 2014 07:13
-
-
Save k0nserv/38a91a37eb0962f05be2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// The DoUntil struct holds the action that is | |
// the loop body | |
struct DoUntil { | |
let action: () -> () | |
init(action: () -> ()) { | |
self.action = action | |
} | |
// Using a method like this allows replacing what would have | |
// been a space in | |
// do { | |
// println("i = \(i)") | |
// i++ | |
// } until (i == 10) | |
// with a dot instead | |
func until(condition: @autoclosure () -> BooleanType) { | |
while condition().boolValue == false { | |
action() | |
} | |
} | |
} | |
func Do(block: () -> ()) -> DoUntil { | |
return DoUntil(action: block) | |
} | |
i = 0 | |
Do { | |
println("i = \(i)") | |
i++ | |
}.until(i == 10) | |
i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment