<p>Some programming languages allow you to create generators - lazily evaluated iterable objects that use a function to determine what the next item should be.</p>
<p>For example, Python allows:</p>
<pre class="lang-python prettyprint-override"><code>def f():
    x = 0
    while True:
        yield x
        x += 1
</code></pre>
<p>To generate an infinite list of numbers.</p>
<p>What other syntax options exist for generators?</p>

<syntax><language-design><generators>
What are some syntax options for generators?
<h1>Enumerators</h1>
<p>Ruby uses this:</p>
<pre class="lang-ruby prettyprint-override"><code>Enumerator.new do |y|
  1.step do |num|
    y &lt;&lt; num
  end
end
</code></pre>
<p><code>Enumerator.new</code> just creates a new enumerator, taking a block, and passes a &quot;yielder&quot; object to the block, where you can call <code>y.yield</code> or <code>y &lt;&lt;</code> to yield a value. There is no special syntax for generators, just normal blocks.</p>

CC BY-SA 4.0
2023-05-19T20:27:40.340Z
2023-05-19T20:27:40.340Z
naffetS
<h1>Infinite recursive evaluation</h1>
<p>For example, in Scala, for an infinite list of positive numbers:</p>
<pre class="lang-scala prettyprint-override"><code>lazy val numbers: LazyList[Int] = 1 #:: numbers.map(_ + 1)
</code></pre>
<p><code>#::</code> prepends here. So we start with a list of <code>[1]</code>. Then recurse, add one to each, and prepend one again. Now it's <code>[1, 2]</code>. And so on.</p>
<p>A more elaborate example with fibonacci numbers:</p>
<pre class="lang-scala prettyprint-override"><code>val fib: LazyList[BigInt] = 0 #:: 1 #:: fib.zip(fib.tail).map(p =&gt; p._1 + p._2)
</code></pre>
<p>Or in Haskell:</p>
<pre class="lang-haskell prettyprint-override"><code>fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
</code></pre>

CC BY-SA 4.0
2023-05-19T20:34:42.730Z
2023-05-19T20:34:42.730Z
naffetS
<h1>Single-line Python-style</h1>
<pre class="lang-python prettyprint-override"><code>(expr for x in iterable)
</code></pre>
<p>Basically a list comprehension but with parentheses instead of brackets. It's concise, but the downside is that it only allows one expression and you have to loop through an iterable (no <code>while</code> loops allowed).</p>

CC BY-SA 4.0
2023-05-19T06:22:51.787Z
2023-05-19T06:22:51.787Z
The Thonnu
# What are some syntax options for generators?
Posted by: username_0 on 2023-05-19
Some programming languages allow you to create generators - lazily evaluated iterable objects that use a function to determine what the next item should be.
For example, Python allows:
def f():
    x = 0
    while True:
        yield x
        x += 1

To generate an infinite list of numbers.
What other syntax options exist for generators?
**Tags:** syntax language-design generators
---
## 3 answers
### Answer by username_1 on 2023-05-19. Score: 4
Enumerators
Ruby uses this:
Enumerator.new do |y|
  1.step do |num|
    y << num
  end
end

Enumerator.new just creates a new enumerator, taking a block, and passes a "yielder" object to the block, where you can call y.yield or y << to yield a value. There is no special syntax for generators, just normal blocks.
---
### Answer by username_1 on 2023-05-19. Score: 2
Infinite recursive evaluation
For example, in Scala, for an infinite list of positive numbers:
lazy val numbers: LazyList[Int] = 1 #:: numbers.map(_ + 1)

#:: prepends here. So we start with a list of [1]. Then recurse, add one to each, and prepend one again. Now it's [1, 2]. And so on.
A more elaborate example with fibonacci numbers:
val fib: LazyList[BigInt] = 0 #:: 1 #:: fib.zip(fib.tail).map(p => p._1 + p._2)

Or in Haskell:
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
---
### Answer by username_2 on 2023-05-19. Score: 2
Single-line Python-style
(expr for x in iterable)

Basically a list comprehension but with parentheses instead of brackets. It's concise, but the downside is that it only allows one expression and you have to loop through an iterable (no while loops allowed).
---
