Clarify the use of the backwards index operator (^N) in tut1 (#14681)

* Clarify the use of the backwards index operator (^N) in tut1

For consistency:
- Do `[a .. ^b]` (notice spaces on both sides of `..`)
- Do `[c ..< d]` (notice spaces on both sides of `..<`)

Fixes https://github.com/nim-lang/Nim/issues/14671.

* tut1: Add a note that ^ template calls can be saved to consts
This commit is contained in:
Kaushal Modi 2020-06-19 10:22:48 -04:00 • committed by GitHub
commit ac8ab4c549
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -398,7 +398,9 @@ Since counting up occurs so often in programs, Nim also has a `..
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 .. code-block:: nim
for i in 0 ..< 10: for i in 0 ..< 10:
@ -411,6 +413,13 @@ or
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 Other useful iterators for collections (like arrays and sequences) are
* ``items`` and ``mitems``, which provides immutable and mutable elements respectively, and * ``items`` and ``mitems``, which provides immutable and mutable elements respectively, and
* ``pairs`` and ``mpairs`` which provides the element and an index number (immutable and mutable respectively) * ``pairs`` and ``mpairs`` which provides the element and an index number (immutable and mutable respectively)
@ -1426,7 +1435,8 @@ indices are
^19 ^8 ^2 using ^ syntax ^19 ^8 ^2 using ^ syntax
where ``b[0 .. ^1]`` is equivalent to ``b[0 .. b.len-1]`` and ``b[0 ..< b.len]``, and it 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``. 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 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". string that is "useless" and replace it with "useful".
@ -1434,9 +1444,13 @@ 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." "useless" portion with "useful", giving the result "Slices are useful."
Note: alternate ways of writing this are ``b[^8..^2] = "useful"`` or 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"``. 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 Objects
------- -------