Last active
February 5, 2017 05:33
-
-
Save yossan/06ab1534fbd1ce2b4b1ec25257ac740d to your computer and use it in GitHub Desktop.
CountDownStream
This file contains 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
/// 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