Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions language/generators.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
</para>

<para>
A generator allows you to write code that uses &foreach; to iterate over a
set of data without needing to build an array in memory, which may cause
A generator allows you to write a code that can disguise a regular loop
the way it looks as though it's an array iterated over using &foreach;.
In case your code expects an array, but each item is actually an iteration
result of some loop, then you can make it look as an array by creating
a generator, without needing to build an array in memory, which may cause
you to exceed a memory limit, or require a considerable amount of
processing time to generate. Instead, you can write a generator function,
which is the same as a normal
Expand Down Expand Up @@ -95,6 +98,18 @@ Single digit odd numbers from xrange(): 1 3 5 7 9
</screen>
</example>

<note>
<simpara>
It is important to understand that a generator is essentially a syntax sugar
that cannot really reduce the memory consumption. It's the undelying loop
that does the job, providing one item at a time. While a generator simply
allows to convert any other loop, such as <literal>while</literal>
or <literal>for</literal> into <literal>foreach</literal>,
making it possible to use the uniform interface to access either arrays,
streams or sequences generated on the fly.
</simpara>
</note>

<sect2 xml:id="language.generators.object">
<title><classname>Generator</classname> objects</title>
<para>
Expand All @@ -107,7 +122,6 @@ Single digit odd numbers from xrange(): 1 3 5 7 9
</para>
</sect2>
</sect1>

<sect1 xml:id="language.generators.syntax">
<title>Generator syntax</title>

Expand Down