Mention iterators
This commit is contained in:
parent
4abd738f6b
commit
73d6c89676
3 changed files with 27 additions and 1 deletions
|
|
@ -35,6 +35,30 @@ c
|
|||
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 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.
|
||||
|
||||
|
|
|
|||
|
|
@ -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`.
|
||||
|
||||
## 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.
|
||||
|
||||
``` nimrod
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ Nimrod supports three different types of variables, `let`, `var`, and `const`. A
|
|||
``` nimrod
|
||||
proc getAlphabet(): string =
|
||||
var accm = ""
|
||||
for letter in 'a'..'z':
|
||||
for letter in 'a'..'z': # see iterators
|
||||
accm.add(letter)
|
||||
return accm
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue