Magic infinite sequences for Python
Today I have released a small module for Python 3 that implements cached lazy infinite sequences.
Here are some examples that demonstrate what this module can do:
>>> InfSequence(5, 6, ...)
<InfSequence: 5 6 7 8 9 10 ...>
>>> InfSequence.geometric_progression(9)
<InfSequence: 1 9 81 729 6561 59049 ...>
>>> InfSequence.cycle('foo', 'bar')
<InfSequence: 'foo' 'bar' 'foo' 'bar' 'foo' 'bar' ...>
>>> InfSequence.fibonacci()
<InfSequence: 0 1 1 2 3 5 ...>
Slicing works:
>>> InfSequence(5, 6, ...)[5:]
<InfSequence: 10 11 12 13 14 15 ...>
>>> InfSequence(5, 6, ...)[5::2]
<InfSequence: 10 12 14 16 18 20 ...>
A somewhat reverse operation:
>>> (2, 3, 4) + InfSequence(5, 6, ...)
<InfSequence: 2 3 4 5 6 7 ...>
Various arithmetic operations are supported:
>>> InfSequence(1, 2, ...) ** 2
<InfSequence: 1 4 9 16 25 36 ...>
>>> InfSequence(3, 5, ...) - InfSequence(1, 2, ...)
<InfSequence: 2 3 4 5 6 7 ...>
>>> InfSequence(1, 2, ...).partial_sum(10)
55
More magic methods:
>>> InfSequence(1, 2, ...).accumulate()
<InfSequence: 1 3 6 10 15 21 ...>
>>> InfSequence(0, 2, ...) @ InfSequence(1)
<InfSequence: 1 4 9 16 25 36 ...>
For the complete API overview, please look at the documentation.
You can install the module from PyPI. It is licensed under 3-clause BSD license. The source code is available on GitHub.