summary refs log tree commit diff stats
path: root/doc/tut1.rst
diff options
context:
space:
mode:
Diffstat (limited to 'doc/tut1.rst')
-rw-r--r--doc/tut1.rst38
1 files changed, 26 insertions, 12 deletions
diff --git a/doc/tut1.rst b/doc/tut1.rst
index 6d54d88a3..24874096c 100644
--- a/doc/tut1.rst
+++ b/doc/tut1.rst
@@ -395,20 +395,29 @@ Since counting up occurs so often in programs, Nim also has a `..
 <system.html#...i,T,T>`_ iterator that does the same:
 
 .. code-block:: nim
-  for i in 1..10:
+  for i in 1 .. 10:
     ...
 
-Zero-indexed counting have two shortcuts ``..<`` and ``..^`` to simplify counting to one less than the higher index:
+Zero-indexed counting has two shortcuts ``..<`` and ``.. ^1``
+(`backwards index operator <system.html#^.t%2Cint>`_) to simplify
+counting to one less than the higher index:
 
 .. code-block:: nim
-  for i in 0..<10:
-    ...  # 0..9
+  for i in 0 ..< 10:
+    ...  # 0 .. 9
 
 or
 
 .. code-block:: nim
   var s = "some string"
-  for i in 0..<s.len:
+  for i in 0 ..< s.len:
+    ...
+
+or
+
+.. code-block:: nim
+  var s = "some string"
+  for idx, c in s[0 .. ^1]:
     ...
 
 Other useful iterators for collections (like arrays and sequences) are
@@ -1402,8 +1411,8 @@ define operators which accept Slice objects to define ranges.
     a = "Nim is a programming language"
     b = "Slices are useless."
 
-  echo a[7..12] # --> 'a prog'
-  b[11..^2] = "useful"
+  echo a[7 .. 12] # --> 'a prog'
+  b[11 .. ^2] = "useful"
   echo b # --> 'Slices are useful.'
 
 In the previous example slices are used to modify a part of a string. The
@@ -1425,17 +1434,22 @@ indices are
    0         11    17   using indices
   ^19        ^8    ^2   using ^ syntax
 
-where ``b[0..^1]`` is equivalent to ``b[0..b.len-1]`` and ``b[0..<b.len]``, and it
-can be seen that the ``^1`` provides a short-hand way of specifying the ``b.len-1``.
+where ``b[0 .. ^1]`` is equivalent to ``b[0 .. b.len-1]`` and ``b[0 ..< b.len]``, and it
+can be seen that the ``^1`` provides a short-hand way of specifying the ``b.len-1``. See
+the `backwards index operator <system.html#^.t%2Cint>`_.
 
 In the above example, because the string ends in a period, to get the portion of the
 string that is "useless" and replace it with "useful".
 
-``b[11..^2]`` is the portion "useless", and ``b[11..^2] = "useful"`` replaces the
+``b[11 .. ^2]`` is the portion "useless", and ``b[11 .. ^2] = "useful"`` replaces the
 "useless" portion with "useful", giving the result "Slices are useful."
 
-Note: alternate ways of writing this are ``b[^8..^2] = "useful"`` or
-as ``b[11..b.len-2] = "useful"`` or as ``b[11..<b.len-1] = "useful"``.
+Note 1: alternate ways of writing this are ``b[^8 .. ^2] = "useful"`` or
+as ``b[11 .. b.len-2] = "useful"`` or as ``b[11 ..< b.len-1] = "useful"``.
+
+Note 2: As the ``^`` template returns a `distinct int <manual.html#types-distinct-type>`_
+of type ``BackwardsIndex``, we can have a ``lastIndex`` constant defined as ``const lastIndex = ^1``,
+and later used as ``b[0 .. lastIndex]``.
 
 Objects
 -------
as Rumpf <rumpf_a@web.de> 2014-04-09 22:56:18 +0200 committer Andreas Rumpf <rumpf_a@web.de> 2014-04-09 22:56:18 +0200 Merge pull request #1075 from flaviut/inlinedocs' href='/ahoang/Nim/commit/tests/misc/tradix.nim?h=devel&id=a690e7b26772a9bb8367acb451a6250449e666ab'>a690e7b26 ^
ddc131cf0 ^
a690e7b26 ^

ddc131cf0 ^
a690e7b26 ^



e80465dac ^
a690e7b26 ^


e80465dac ^
a690e7b26 ^
e80465dac ^
a690e7b26 ^



e80465dac ^
a690e7b26 ^

e80465dac ^
30823c1ce ^
a690e7b26 ^
e80465dac ^
30823c1ce ^
a690e7b26 ^
e80465dac ^
30823c1ce ^
a690e7b26 ^


e80465dac ^
a690e7b26 ^





e80465dac ^
a690e7b26 ^


e80465dac ^
a690e7b26 ^






e80465dac ^

a690e7b26 ^




30823c1ce ^
a690e7b26 ^








30823c1ce ^
a690e7b26 ^








e80465dac ^
a690e7b26 ^

2a4c09ff8 ^
a690e7b26 ^




e80465dac ^

a690e7b26 ^

e80465dac ^
a690e7b26 ^

e80465dac ^
a690e7b26 ^



e80465dac ^
a690e7b26 ^


e80465dac ^
a690e7b26 ^






e80465dac ^

a690e7b26 ^



2a4c09ff8 ^
a690e7b26 ^








e80465dac ^
a690e7b26 ^





e80465dac ^
a690e7b26 ^






e80465dac ^
a690e7b26 ^



e80465dac ^
a690e7b26 ^
e80465dac ^

a690e7b26 ^
e80465dac ^

a690e7b26 ^
e80465dac ^

a690e7b26 ^

e80465dac ^
a690e7b26 ^
e80465dac ^
a690e7b26 ^
e80465dac ^
a690e7b26 ^


e80465dac ^
a690e7b26 ^

e80465dac ^
a690e7b26 ^

e80465dac ^
a690e7b26 ^
e80465dac ^

a690e7b26 ^
e80465dac ^
a690e7b26 ^
e80465dac ^
a690e7b26 ^

e80465dac ^


a690e7b26 ^

e80465dac ^
a690e7b26 ^
e80465dac ^
a690e7b26 ^






28394153a ^



a690e7b26 ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254