Mention iterators

This commit is contained in:
flaviut 2014-07-17 13:54:41 -04:00
commit 73d6c89676
3 changed files with 27 additions and 1 deletions

View file

@ -35,6 +35,30 @@ c
d d
``` ```
## Operators
Iterators can also be operators [in the standard way](/procs/#operators), with the name enclosed in backticks. For example, the standard library defines the slice iterator, which allows iterating through [ordinal types](/types/enums/#ordinals).
``` nimrod
# Give it a different name to avoid conflict
iterator `...`*[T](a: T, b: T): T =
var res: T = T(a)
while res <= b:
yield res
inc res
for i in 0...5:
echo i
```
``` console
$ nimrod c -r operatoriterator.nim
0
1
2
3
4
5
```
## Inline Iterators ## Inline Iterators
Inline iterators basically take the body of the for loop and inline it into the iterator. This means that they do not have any overhead from function calling, but if carelessly created may increase code size dramatically. Inline iterators basically take the body of the for loop and inline it into the iterator. This means that they do not have any overhead from function calling, but if carelessly created may increase code size dramatically.

View file

@ -19,6 +19,8 @@ Notice that each element in `CompassDirections` is prepended with `cd` to avoid
Enums can be given custom values and stringify values, as shown by `Colors` and `Signals`. Enums can be given custom values and stringify values, as shown by `Colors` and `Signals`.
## Ordinals
While enums can also have disjoint values, it should not be used for any other reason than compatibility with C because it breaks the idea that enums are ordinal. While enums can also have disjoint values, it should not be used for any other reason than compatibility with C because it breaks the idea that enums are ordinal.
``` nimrod ``` nimrod

View file

@ -8,7 +8,7 @@ Nimrod supports three different types of variables, `let`, `var`, and `const`. A
``` nimrod ``` nimrod
proc getAlphabet(): string = proc getAlphabet(): string =
var accm = "" var accm = ""
for letter in 'a'..'z': for letter in 'a'..'z': # see iterators
accm.add(letter) accm.add(letter)
return accm return accm