Skip to content

Instantly share code, notes, and snippets.

@yossan
Last active February 5, 2017 05:33
Show Gist options
  • Save yossan/06ab1534fbd1ce2b4b1ec25257ac740d to your computer and use it in GitHub Desktop.
Save yossan/06ab1534fbd1ce2b4b1ec25257ac740d to your computer and use it in GitHub Desktop.
CountDownStream
/// Creating Stream for CountDown
func createCountDown(_ n: Int) -> Array<Any> {
guard n > 0 else {
return [0]
}
return [n, {
return createCountDown(n-1)
}]
}
func startCountDown(_ countDown: Array<Any>) {
//start counting down
for i in countDown {
switch i {
case is Int:
print(i, terminator: " ")
case is (Void) -> Array<Any>:
let next = i as! (Void) -> Array<Any>
startCountDown(next())
default:
print("ERROR")
}
}
}
let countDown = createCountDown(5)
startCountDown(countDown)
// 5 4 3 2 1 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment