Recursive sequence generator
<p>Does Python have a recursive sequence generating function? For instance,</p>

<pre><code>def generateSequence(seed, f, n):
    sequence = list(seed)
    for i in range(n):
        sequence.append(f(sequence, i))
    return sequence
</code></pre>

<p>Which may be used like so:</p>

<pre><code>fibSequence = generateSequence([0, 1], lambda x, i: x[-1] + x[-2], 8)
</code></pre>

<p>To produce:</p>

<pre><code>[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
</code></pre>

<p>I think the <a href="https://docs.python.org/3/library/itertools.html#itertools.accumulate" rel="nofollow"><code>itertools.accumulate</code></a> may meet your needs, but it's return value may be different from what you expect.</p>

<p>For instance:</p>

<pre><code>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from itertools import accumulate

def callback(seq, i):
    """
        seq: the sequence you will modified
        i: the number of times this function is called
    """
    seq.append(seq[-1] + seq[-2])
    return seq

res = accumulate([[0, 1]] + list(range(1, 8 + 1)), callback)
for item in res:
    print(item)
</code></pre>

<p>The <code>[0, 1]</code> is the init sequence, and the <code>8</code> is the number of times you want to call the <code>callback</code> function. </p>

<p>And the result of above code is this:</p>

<pre><code>In [48]: run test.py
[0, 1]
[0, 1, 1]
[0, 1, 1, 2]
[0, 1, 1, 2, 3]
[0, 1, 1, 2, 3, 5]
[0, 1, 1, 2, 3, 5, 8]
[0, 1, 1, 2, 3, 5, 8, 13]
[0, 1, 1, 2, 3, 5, 8, 13, 21]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
</code></pre>

<p>The last one is you wanted.</p>

python