Package: infseq
Infinite sequences for PythonTheinfseqmodule implements cached lazy infinite sequences for Python 3.Here, the word “lazy” means that values of the sequence will never be calculated
unless they are really used, and the word “cached” means that every value will
be calculated no more than once.Sequences can contain items of any type — such as numbers, strings or even
other sequences.Using this module is pretty straightforward — everything just works. Here are
some usage examples:Creating sequences>>>frominfseqimportInfSequence>>>InfSequence(5)<InfSequence:555555...>>>>InfSequence(5,6,...)<InfSequence:5678910...>>>>InfSequence(lambdaindex:index*2+1)<InfSequence:1357911...>>>>InfSequence.geometric_progression(3)<InfSequence:1392781243...>>>>InfSequence.cycle('a','b','c')<InfSequence:'a''b''c''a''b''c'...>>>>InfSequence.fibonacci()<InfSequence:011235...>Note: for the ease of debugging the first six values are calculated whenrepr()is called on the sequence. If you just create the sequence without
printing it, the values are not calculated. The number of items can be adjusted
by modifying theinfseq.REPR_VALUESnumber (it is set to 6 by default).Retrieving the values>>>a=InfSequence.geometric_progression(2)>>>a<InfSequence:12481632...>>>>a[10]1024>>>a.partial_sum(10)# a[0] + ... + a[9]1023>>>a.partial_sum(4,10)# sum(a[i] for i in range(4, 10))1008>>>a.partial_product(5)# a[0] * ... * a[4]1024Slicing and prepending elements>>>a[5:]<InfSequence:32641282565121024...>>>>a[::2]<InfSequence:1416642561024...>>>>list(a[5:10])# a[5:10] returns a map object, because of laziness[32,64,128,256,512]>>>list(a[4::-1])# reverse slices also work[16,8,4,2,1]>>>(5,7)+a<InfSequence:571248...>Arithmetic operations>>>b=InfSequence(1,2,...)>>>b<InfSequence:123456...>>>>b*2<InfSequence:24681012...>>>>b**2<InfSequence:149162536...>>>>a+b<InfSequence:247122138...>Applying any functions>>>c=InfSequence.geometric_progression(9)>>>c<InfSequence:1981729656159049...>>>>importmath>>>c.apply_function(math.sqrt)<InfSequence:1.03.09.027.081.0243.0...>Using theaccumulatemethodTheaccumulatemethod returns a sequence of partial sums of the original
sequence (similar toitertools.accumulate):result[0] = a[0]
result[1] = a[0] + a[1]
result[2] = a[0] + a[1] + a[2]
...If a custom function is passed as an argument, it is used to do
the reducing instead of the sum function.In the examples below we can get the sequence ofn(n+1)/2and the sequence ofn!using this method:>>>fromoperatorimportmul>>>b<InfSequence:123456...>>>>b.accumulate()<InfSequence:136101521...>>>>b.accumulate(mul)<InfSequence:12624120720...>Using the matrix multiplication operatorIf you are using Python 3.5+, you can use the new “matrix multiplication”
operator that was introduced in that version.The expressiona @ bwill produce the following result:result[0] = a[0] * b[0]
result[1] = a[0] * b[1] + a[1] * b[0]
result[2] = a[0] * b[2] + a[1] * b[1] + a[2] * b[0]
...Example:>>>InfSequence(0,2,...)@InfSequence(1)<InfSequence:149162536...>Installing the module and running the testsThe module is available onPyPI. To install the module, simply use:pip3 install infseqThe source code is hosted onGitHub.To run the doctests in this module, use:python3 -m doctest ./README.rst