Skip to content

Instantly share code, notes, and snippets.

@harlanhaskins
Created January 8, 2016 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harlanhaskins/bafab4a56c445d078ed9 to your computer and use it in GitHub Desktop.
Save harlanhaskins/bafab4a56c445d078ed9 to your computer and use it in GitHub Desktop.
Loops implemented terribly, in Swift
func while_(@autoclosure(escaping) cond: () -> Bool, body: () -> ()) {
func goto(label: String) {
switch label {
case "cond": goto(cond() ? "body" : "end")
case "body":
body()
goto("cond")
case "end":
fallthrough
default: break
}
}
goto("cond")
}
func for_(value: Int, _ cond: Int -> Bool, _ post: (inout Int) -> (), _ body: Int -> ()) {
var value = value
while_(cond(value)) {
body(value)
post(&value)
}
}
var j = 100
while_ (j > 50) {
print(j)
j -= 1
}
for_(0, { $0 < 50 }, { $0 += 1 }) {
print($0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment