Kaynağa Gözat

SVI Add ch07

SVI 1 ay önce
ebeveyn
işleme
cdcec59c84
1 değiştirilmiş dosya ile 47 ekleme ve 46 silme
  1. 47 46
      doc/part01/ch07.md

+ 47 - 46
doc/part01/ch07.md

@@ -1,50 +1,51 @@
-# Chapter 7. iterators
-
-An iterator is a resumable proc. Iterators are usually invoked in for loops.
-We have already seen the built-in iterators items and ..<. If they were not
-built-in we could easily define them ourselves:
-  iterator `..<`(a, b: int): int = 1
-    var i = a
-    while i < b:
-      yield i 2
-      inc i   3
+# Глава 7. Итераторы
+
+An iterator is a resumable `proc`. Iterators are usually invoked in for loops.
+
+We have already seen the built-in iterators items and `..<`. If they were not built-in we could easily define them ourselves:
+
+```nim
+iterator `..<`(a, b: int): int = # 1
+  var i = a
+   while i < b:
+     yield i                     # 2
+     inc i                       # 3
   iterator items(s: seq[Point]): Point =
-    for i in 0 ..< s.len: 4
+    for i in 0 ..< s.len:        # 4
       yield s[i]
- 1 An iterator is declared much like a proc. The ..< operator takes two
-    integers and produces an integer.
- 2 We do not return a value, we yield it. The for loop that calls the iterator
-    ..< calls ..< again and again, each time the control flow resumes where
-    the iterator left off until the iterator’s while loop finishes. (When i >= b.)
- 3 inc i means to increment the integer i by 1. It can be also be written as i
-    = i + 1 or i += 1.
- 4 The items iterator calls the ..< iterator. Iterators are called in for loops.
-7.1. Yield
-A yield statement can be easily understood as a variation of a return
-statement: A return statement returns the control flow to the caller,
-potentially producing a value that the caller can receive:
-                                                                                 33
- proc find(haystack: string; needle: char): int =
+```
+
+- 1 An iterator is declared much like a `proc`. The `..<` operator takes two integers and produces an integer.
+- 2 We do not return a value, we `yield` it. The for loop that calls the iterator `..<` calls `..<` again and again, each time the control flow resumes where the iterator left off until the iterator’s while loop finishes. (When `i >= b`.)
+- 3 `inc i` means to increment the integer `i` by 1. It can be also be written as `i = i + 1` or `i += 1`.
+- 4 The items iterator calls the `..<` iterator. Iterators are called in for loops.
+
+## 7.1. Yield
+
+A `yield` statement can be easily understood as a variation of a return statement: A return statement returns the control flow to the caller, potentially producing a value that the caller can receive:
+
+```nim
+proc find(haystack: string; needle: char): int =
    for i in 0 ..< haystack.len:
-      if haystack[i] == needle: return i 1
-   return -1 2
- let index = find("abcabc", 'c') 3
- 1 Return the value of i to the caller and do not continue with the execution
-    of find. This implies that the for loop is left too.
- 2 Return the value -1 to indicate that needle did not occur in haystack.
- 3 We assign the value that find returns to a variable called index.
-find returns the index of the first occurrence of needle inside haystack. It is
-not possible to resume its execution in order to retrieve the possible other
-occurrences of needle. An iterator like findAll can do that, thanks to the yield
-keyword:
- iterator findAll(haystack: string; needle: char): int =
+      if haystack[i] == needle: return i # 1
+   return -1                             # 2
+   let index = find("abcabc", 'c')       # 3
+```
+
+- 1 Return the value of `i` to the caller and do not continue with the execution of `find`. This implies that the for loop is left too.
+- 2 Return the value -1 to indicate that needle did not occur in haystack.
+- 3 We assign the value that find returns to a variable called index.
+
+`find` returns the index of the first occurrence of needle inside haystack. It is not possible to resume its execution in order to retrieve the possible other occurrences of needle. An iterator like `findAll` can do that, thanks to the `yield` keyword:
+
+```nim
+iterator findAll(haystack: string; needle: char): int =
    for i in 0 ..< haystack.len:
-      if haystack[i] == needle: yield i 1
-   2
- for index in findAll("abcabc", 'c'): discard 3
- 1 Return the value of i to the caller and continue with the execution of
-    findAll later.
- 2 Notice the absence of a yield -1 statement. If the iterator does not yield
-    more values, the calling for loop will stop.
- 3 We iterate over all values that are produced by findAll and bind the
-    current value to index.
+      if haystack[i] == needle: yield i       # 1
+   # 2
+for index in findAll("abcabc", 'c'): discard # 3
+```
+
+- 1 Return the value of `i` to the caller and continue with the execution of `findAll` later.
+- 2 Notice the absence of a `yield -1` statement. If the iterator does not yield more values, the calling for loop will stop.
+- 3 We iterate over all values that are produced by `findAll` and bind the current value to index.