From 73d6c89676fd1c9e5959188e208ff97f8dc7954a Mon Sep 17 00:00:00 2001 From: flaviut Date: Thu, 17 Jul 2014 13:54:41 -0400 Subject: [PATCH] Mention iterators --- content/for_iterators.md | 24 ++++++++++++++++++++++++ content/types/enums.md | 2 ++ content/variables.md | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/content/for_iterators.md b/content/for_iterators.md index 12407b0..a97d7c2 100644 --- a/content/for_iterators.md +++ b/content/for_iterators.md @@ -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. diff --git a/content/types/enums.md b/content/types/enums.md index 0826d7c..e79811d 100644 --- a/content/types/enums.md +++ b/content/types/enums.md @@ -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 diff --git a/content/variables.md b/content/variables.md index 6db20d2..f859bd5 100644 --- a/content/variables.md +++ b/content/variables.md @@ -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