-
-
Save zacharycarter/fa91e0e448945b0c8a37b5502f18e9de to your computer and use it in GitHub Desktop.
ci
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
proc countTo(n: int): iterator(): int = | |
return iterator(): int = | |
var i = 0 | |
while i <= n: | |
yield i | |
inc i | |
let countTo20 = countTo(20) | |
echo countTo20() | |
var output = "" | |
# Raw iterator usage: | |
while true: | |
# 1. grab an element | |
let next = countTo20() | |
# 2. Is the element bogus? Its the end of the loop, discard it | |
if finished(countTo20): | |
break | |
# 3. Loop body goes here: | |
output.add($next & " ") | |
echo output | |
output = "" | |
let countTo9 = countTo(9) | |
for i in countTo9(): | |
output.add($i) | |
echo outputv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment