Last active
December 28, 2015 08:19
-
-
Save Varriount/7470706 to your computer and use it in GitHub Desktop.
c:\64\nimrod\lib\system.nim(390, 13) Error: cannot instantiate: 'T'
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
iterator cycle* [iterType, returnType](iter:iterType): returnType = | |
## An iterator returning elements from the iterable and saving a | |
## copy of each. When the iterable is exhausted, return elements | |
## from the saved copy. Repeats indefinitely. | |
## | |
## Parameters: | |
## iter: The iterator to cycle through | |
var cont:seq[returnType] | |
cont = newSeq[returnType]() | |
for element in iter: | |
yield element | |
cont.add(element) | |
while True: | |
for element in cont: | |
yield element | |
when defined(isMainModule): | |
var cycleSeq = @[1,2] | |
var cycleIterator = cycle[seq[int], int] | |
for i in 1..3: | |
cycleSeq.add(cycleIterator(cycleSeq)) | |
assert(cycleSeq == @[1,2,1,2,1,2]) | |
echo(repr(cycleSeq)) | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment