{"id":16695145,"url":"https://github.com/mitya57/infseq","last_synced_at":"2026-04-21T21:33:49.496Z","repository":{"id":62570995,"uuid":"47773330","full_name":"mitya57/infseq","owner":"mitya57","description":"Cached lazy infinite sequences for Python 3","archived":false,"fork":false,"pushed_at":"2016-09-08T11:00:48.000Z","size":13,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-20T19:31:25.462Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://pypi.python.org/pypi/infseq","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mitya57.png","metadata":{"files":{"readme":"README.rst","changelog":"changelog","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-10T16:19:15.000Z","updated_at":"2018-02-19T12:40:44.000Z","dependencies_parsed_at":"2022-11-03T17:01:07.542Z","dependency_job_id":null,"html_url":"https://github.com/mitya57/infseq","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitya57%2Finfseq","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitya57%2Finfseq/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitya57%2Finfseq/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitya57%2Finfseq/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mitya57","download_url":"https://codeload.github.com/mitya57/infseq/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243501174,"owners_count":20300856,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-12T17:05:37.664Z","updated_at":"2025-12-29T21:33:19.438Z","avatar_url":"https://github.com/mitya57.png","language":"Python","readme":".. image:: https://api.travis-ci.org/mitya57/infseq.svg\n   :target: https://travis-ci.org/mitya57/infseq\n   :alt: Travis CI status\n\nInfinite sequences for Python\n=============================\n\nThe ``infseq`` module implements cached lazy infinite sequences for Python 3.\n\nHere, the word “lazy” means that values of the sequence will never be calculated\nunless they are really used, and the word “cached” means that every value will\nbe calculated no more than once.\n\nSequences can contain items of any type — such as numbers, strings or even\nother sequences.\n\nUsing this module is pretty straightforward — everything just works. Here are\nsome usage examples:\n\nCreating sequences\n------------------\n\n.. code:: python\n\n  \u003e\u003e\u003e from infseq import InfSequence\n  \u003e\u003e\u003e InfSequence(5)\n  \u003cInfSequence: 5 5 5 5 5 5 ...\u003e\n  \u003e\u003e\u003e InfSequence(5, 6, ...)\n  \u003cInfSequence: 5 6 7 8 9 10 ...\u003e\n  \u003e\u003e\u003e InfSequence(lambda index: index * 2 + 1)\n  \u003cInfSequence: 1 3 5 7 9 11 ...\u003e\n  \u003e\u003e\u003e InfSequence.geometric_progression(3)\n  \u003cInfSequence: 1 3 9 27 81 243 ...\u003e\n  \u003e\u003e\u003e InfSequence.cycle('a', 'b', 'c')\n  \u003cInfSequence: 'a' 'b' 'c' 'a' 'b' 'c' ...\u003e\n  \u003e\u003e\u003e InfSequence.fibonacci()\n  \u003cInfSequence: 0 1 1 2 3 5 ...\u003e\n\n**Note**: for the ease of debugging the first six values are calculated when\n``repr()`` is called on the sequence. If you just create the sequence without\nprinting it, the values are not calculated. The number of items can be adjusted\nby modifying the ``infseq.REPR_VALUES`` number (it is set to 6 by default).\n\nRetrieving the values\n---------------------\n\n.. code:: python\n\n  \u003e\u003e\u003e a = InfSequence.geometric_progression(2)\n  \u003e\u003e\u003e a\n  \u003cInfSequence: 1 2 4 8 16 32 ...\u003e\n  \u003e\u003e\u003e a[10]\n  1024\n  \u003e\u003e\u003e a.partial_sum(10)  # a[0] + ... + a[9]\n  1023\n  \u003e\u003e\u003e a.partial_sum(4, 10)  # sum(a[i] for i in range(4, 10))\n  1008\n  \u003e\u003e\u003e a.partial_product(5)  # a[0] * ... * a[4]\n  1024\n  \u003e\u003e\u003e a.partial_reduce(5, lambda *args: '%s | %s' % args, initial='start')\n  'start | 1 | 2 | 4 | 8 | 16'\n\nFor loops\n---------\n\n.. code:: python\n\n  \u003e\u003e\u003e for item in a:\n  ...     if item \u003e 30:\n  ...         print(item)\n  ...         break\n  32\n\nSlicing and prepending elements\n-------------------------------\n\n.. code:: python\n\n  \u003e\u003e\u003e a[5:]\n  \u003cInfSequence: 32 64 128 256 512 1024 ...\u003e\n  \u003e\u003e\u003e a[::2]\n  \u003cInfSequence: 1 4 16 64 256 1024 ...\u003e\n  \u003e\u003e\u003e list(a[5:10])  # a[5:10] returns a map object, because of laziness\n  [32, 64, 128, 256, 512]\n  \u003e\u003e\u003e list(a[4::-1])  # reverse slices also work\n  [16, 8, 4, 2, 1]\n  \u003e\u003e\u003e (5, 7) + a\n  \u003cInfSequence: 5 7 1 2 4 8 ...\u003e\n\nZipping and enumerating sequences\n---------------------------------\n\nThese work like Python’s own ``zip()`` and ``enumerate()``, yielding sequences\nof tuples.\n\n.. code:: python\n\n  \u003e\u003e\u003e a.zip(InfSequence.geometric_progression(3))\n  \u003cInfSequence: (1, 1) (2, 3) (4, 9) (8, 27) (16, 81) (32, 243) ...\u003e\n  \u003e\u003e\u003e a.enumerate()\n  \u003cInfSequence: (0, 1) (1, 2) (2, 4) (3, 8) (4, 16) (5, 32) ...\u003e\n  \u003e\u003e\u003e a.enumerate(start=2)\n  \u003cInfSequence: (2, 4) (3, 8) (4, 16) (5, 32) (6, 64) (7, 128) ...\u003e\n\nArithmetic operations\n---------------------\n\n.. code:: python\n\n  \u003e\u003e\u003e b = InfSequence(1, 2, ...)\n  \u003e\u003e\u003e b\n  \u003cInfSequence: 1 2 3 4 5 6 ...\u003e\n  \u003e\u003e\u003e b * 2\n  \u003cInfSequence: 2 4 6 8 10 12 ...\u003e\n  \u003e\u003e\u003e b ** 2\n  \u003cInfSequence: 1 4 9 16 25 36 ...\u003e\n  \u003e\u003e\u003e a + b\n  \u003cInfSequence: 2 4 7 12 21 38 ...\u003e\n\nApplying any functions\n----------------------\n\n.. code:: python\n\n  \u003e\u003e\u003e c = InfSequence.geometric_progression(9)\n  \u003e\u003e\u003e c\n  \u003cInfSequence: 1 9 81 729 6561 59049 ...\u003e\n  \u003e\u003e\u003e import math\n  \u003e\u003e\u003e c.apply_function(math.sqrt)\n  \u003cInfSequence: 1.0 3.0 9.0 27.0 81.0 243.0 ...\u003e\n\nUsing the ``accumulate`` method\n-------------------------------\n\nThe ``accumulate`` method returns a sequence of partial sums of the original\nsequence (similar to itertools.accumulate_)::\n\n  result[0] = a[0]\n  result[1] = a[0] + a[1]\n  result[2] = a[0] + a[1] + a[2]\n  ...\n\n.. _itertools.accumulate: https://docs.python.org/3/library/itertools.html#itertools.accumulate\n\nIf a custom function is passed as an argument, it is used to do\nthe reducing instead of the sum function.\n\nIn the examples below we can get the sequence of *n(n+1)/2* and the sequence of\n*n!* using this method:\n\n.. code:: python\n\n  \u003e\u003e\u003e from operator import mul\n  \u003e\u003e\u003e b\n  \u003cInfSequence: 1 2 3 4 5 6 ...\u003e\n  \u003e\u003e\u003e b.accumulate()\n  \u003cInfSequence: 1 3 6 10 15 21 ...\u003e\n  \u003e\u003e\u003e b.accumulate(mul)\n  \u003cInfSequence: 1 2 6 24 120 720 ...\u003e\n\nUsing the matrix multiplication operator\n----------------------------------------\n\nIf you are using Python 3.5+, you can use the new “matrix multiplication”\noperator that was introduced in that version.\n\nThe expression ``a @ b`` will produce the following result::\n\n  result[0] = a[0] * b[0]\n  result[1] = a[0] * b[1] + a[1] * b[0]\n  result[2] = a[0] * b[2] + a[1] * b[1] + a[2] * b[0]\n  ...\n\nExample:\n\n.. code:: python\n\n  \u003e\u003e\u003e InfSequence(0, 2, ...) @ InfSequence(1)\n  \u003cInfSequence: 1 4 9 16 25 36 ...\u003e\n\nInstalling the module and running the tests\n-------------------------------------------\n\nThe module is available on PyPI_. To install the module, simply use::\n\n  pip3 install infseq\n\nThe source code is hosted on GitHub_.\n\nTo run the doctests in this module, use::\n\n  python3 -m doctest ./README.rst\n\n.. _PyPI: https://pypi.python.org/pypi/infseq\n.. _GitHub: https://github.com/mitya57/infseq\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitya57%2Finfseq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmitya57%2Finfseq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitya57%2Finfseq/lists"}