pascalabcnet/PABCNetHelp/LangGuide/Operators/yieldsequence.html

49 lines
2.5 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<html>
<head>
<object type="application/x-oleobject" classid="clsid:1e2a7bd0-dab9-11d0-b93a-00c04fc99f9e">
<param name="Keyword" value="yield sequence">
</object>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<title></title>
<link rel="StyleSheet" href="../../default.css">
</head>
<body>
<H1>Оператор yield sequence</H1>
<p>Оператор yield sequence используется в функциях, генерирующих последовательности,
вместе с <a href="yield.html">оператором yield</a>, и
имеет вид:</p>
<blockquote>
<p><b><code>yield sequence</code></b> <i>выражение</i></p>
</blockquote>
<p>В отличие от <a href="yield.html">оператора yield</a>, оператор yield
sequence перебирает элементы последовательности, указанной в выражении и
возвращает эти элементы в качестве значений основной функции-итератора.
Например, следующий код:</p>
<blockquote>
<p><code><strong>function</strong> f: <strong>sequence of</strong> real;<br>
<strong>begin</strong><br>&nbsp; <strong>yield</strong> <strong>sequence</strong>
Seq(1,2,3);<br>&nbsp; <strong>yield</strong> 4;<br><strong>end</strong>;<br>
<br><strong>begin</strong><br>&nbsp; f.Println;<br><strong>end</strong>. </code></p>
</blockquote>
<p>выведет последовательность</p>
<blockquote>
<p><code>1 2 3 4 </code></p>
</blockquote>
<p>Следующий пример иллюстрирует формирование последовательности элементов при
обходе бинарного дерева в инфиксном порядке:</p>
<blockquote>
<p><code><strong>function</strong> InfixPrintTree&lt;T&gt;(root: Node&lt;T&gt;): <strong>
sequence of</strong> T;<br><strong>begin</strong><br>&nbsp; <strong>if</strong>
root = nil <strong>then</strong> exit;<br>&nbsp; <strong>yield</strong>
<strong>sequence</strong> InfixPrintTree(root.left);<br>&nbsp; <strong>yield</strong>
root.data;<br>&nbsp; <strong>yield</strong> <strong>sequence</strong>
InfixPrintTree(root.right);<br><strong>end</strong>;</code></p>
</blockquote>
<p>Для функций, в теле которых присутствуют yield sequence, действуют те же
ограничения, что и для функций с yield.</p>
</body>
</html>