fix bug and explain closure iterators better
This commit is contained in:
parent
728f51a2ee
commit
a868603482
1 changed files with 11 additions and 3 deletions
|
|
@ -84,7 +84,7 @@ $ nimrod c -r ./inline_iter.nim
|
||||||
|
|
||||||
|
|
||||||
## Closure Iterators
|
## Closure Iterators
|
||||||
Closure iterators hold on to their state and can be resumed at any time. The `finished()` function can be used to check if there are any more elements available in the iterator.
|
Closure iterators hold on to their state and can be resumed at any time. The `finished()` function can be used to check if there are any more elements available in the iterator, however raw iterator usage is unintuitive and difficult to get right.
|
||||||
|
|
||||||
``` nimrod
|
``` nimrod
|
||||||
proc countTo(n: int): iterator(): int =
|
proc countTo(n: int): iterator(): int =
|
||||||
|
|
@ -99,8 +99,16 @@ let countTo20 = countTo(20)
|
||||||
echo countTo20()
|
echo countTo20()
|
||||||
|
|
||||||
var output = ""
|
var output = ""
|
||||||
while not finished(countTo20):
|
# Raw iterator usage:
|
||||||
output.add($countTo20() & " ")
|
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
|
echo output
|
||||||
|
|
||||||
output = ""
|
output = ""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue