Skip to content

Instantly share code, notes, and snippets.

@k0nserv
Created August 20, 2014 07:13
Show Gist options
  • Save k0nserv/38a91a37eb0962f05be2 to your computer and use it in GitHub Desktop.
Save k0nserv/38a91a37eb0962f05be2 to your computer and use it in GitHub Desktop.
// 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