Skip to content

Instantly share code, notes, and snippets.

@Varriount
Last active December 28, 2015 08:19
Show Gist options
  • Save Varriount/7470706 to your computer and use it in GitHub Desktop.
Save Varriount/7470706 to your computer and use it in GitHub Desktop.
c:\64\nimrod\lib\system.nim(390, 13) Error: cannot instantiate: 'T'
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