How elixir keep the accumulator
<p>I am trying to understand elixir stream.<br>
First I have a list that iterate through a range and multiple with 2.</p>

<pre><code>iex(5)&gt;   stream = 1..3 |&gt;
...(5)&gt;   Enum.map(&amp;IO.inspect(&amp;1)) |&gt;
...(5)&gt;   Enum.map(&amp;(&amp;1 * 2)) |&gt;
...(5)&gt;   Enum.map(&amp;IO.inspect(&amp;1)) |&gt;
...(5)&gt;   Enum.reduce(4, &amp;+/2)
1
2
3
2
4
6
16
</code></pre>

<p>Here everything is clear, every enum return a list for further processing the list. It iterates four times for each element in the list.<br>
Lets build it with stream:</p>

<pre><code>iex(4)&gt;   stream = 1..3 |&gt;
...(4)&gt;   Stream.map(&amp;IO.inspect(&amp;1)) |&gt;
...(4)&gt;   Stream.map(&amp;(&amp;1 * 2)) |&gt;
...(4)&gt;   Stream.map(&amp;IO.inspect(&amp;1)) |&gt;
...(4)&gt;   Enum.reduce(4, &amp;+/2)
1
2
2
4
3
6
16
</code></pre>

<p>Here it iterates only at once for each number in the range not like the example above with <strong>enum</strong>. What I do not understand is, how the accumulator of Enum.reduce keep the value and make a recursive call by them self, when it iterates only once?<br>
When I iterate an enum as follow:</p>

<pre><code>iex(25)&gt; f = fn(x, y) -&gt;
...(25)&gt; IO.puts(y)
...(25)&gt; x + y
...(25)&gt; end
#Function&lt;12.54118792/2 in :erl_eval.expr/5&gt;
iex(26)&gt; iter = 1..5 |&gt; Enum.reduce(4, f)
4
5
7
10
14
19
</code></pre>

<p>then I can imaging, how <strong>enum.reduce</strong> pass the accumulator to the next recursive call.  </p>

<p>But how the stream pass the accumulator for the next recursion call, if the iteration by stream execute only once?</p>