Last active
January 3, 2016 15:49
-
-
Save Varriount/8485141 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Normally, iterators must be passed all their arguments each time they are iterated over | |
iterator counter(start, stop:int): int {.closure.} = | |
var x = start | |
while x < stop: | |
yield x | |
inc(x) | |
var counterInstance = counter | |
echo(counterInstance(0,5)) | |
echo(counterInstance(0,5)) | |
echo(counterInstance(0,5)) | |
echo(counterInstance(0,5)) | |
echo(counterInstance(0,10)) | |
echo(counterInstance(0,10)) | |
echo(counterInstance(0,10)) | |
echo(counterInstance(0,10)) | |
# Heres a python equivalent (note the lack of explicit args each iteration) | |
def counter(start, stop): | |
x = start | |
while x < stop: | |
yield x | |
x += 1 | |
counterInstance = counter(0, 5) | |
print(counterInstance.next()) | |
print(counterInstance.next()) | |
print(counterInstance.next()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment