Skip to content

Instantly share code, notes, and snippets.

@zacharycarter
Created October 29, 2018 03:50
Show Gist options
  • Save zacharycarter/fa91e0e448945b0c8a37b5502f18e9de to your computer and use it in GitHub Desktop.
Save zacharycarter/fa91e0e448945b0c8a37b5502f18e9de to your computer and use it in GitHub Desktop.
ci
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