Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Last active August 29, 2015 14:25
Show Gist options
  • Save jweinst1/d25336947712b4f1e930 to your computer and use it in GitHub Desktop.
Save jweinst1/d25336947712b4f1e930 to your computer and use it in GitHub Desktop.
Threading While Loops into Closures in Swift
// multiplies by adding using a while loop within a closure
var MulbyAdding = {(x:Int, y:Int) -> Int in var add = 0; var count = 0; while count < y {
add += x
count += 1
}; return add
}
// A Power closure that uses MulbyAdding and a while loop
var PowerbyMul = {(x:Int, y:Int) -> Int in var Pow = x; var count = 0; while count < y {
Pow *= x; count += 1
}; return Pow
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment