{"id":13735661,"url":"https://github.com/narimiran/itertools","last_synced_at":"2025-07-24T01:05:45.571Z","repository":{"id":115375489,"uuid":"126627300","full_name":"narimiran/itertools","owner":"narimiran","description":"Nim rewrite of a very popular Python module of the same name.","archived":false,"fork":false,"pushed_at":"2022-12-19T08:28:32.000Z","size":81,"stargazers_count":136,"open_issues_count":0,"forks_count":6,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-15T04:46:08.687Z","etag":null,"topics":["iterators","itertools","nim","nim-lang","nimble"],"latest_commit_sha":null,"homepage":null,"language":"Nim","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/narimiran.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-03-24T18:13:55.000Z","updated_at":"2025-02-25T18:41:23.000Z","dependencies_parsed_at":"2023-03-13T13:03:27.506Z","dependency_job_id":null,"html_url":"https://github.com/narimiran/itertools","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/narimiran%2Fitertools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/narimiran%2Fitertools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/narimiran%2Fitertools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/narimiran%2Fitertools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/narimiran","download_url":"https://codeload.github.com/narimiran/itertools/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244293328,"owners_count":20429825,"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":["iterators","itertools","nim","nim-lang","nimble"],"created_at":"2024-08-03T03:01:09.502Z","updated_at":"2025-03-18T19:45:53.676Z","avatar_url":"https://github.com/narimiran.png","language":"Nim","funding_links":[],"categories":["Language Features"],"sub_categories":["Iteration"],"readme":"# Itertools\n\nThis package is a Nim rewrite of a [very popular Python module](https://docs.python.org/3/library/itertools.html) of the same name.\n\nIt also includes some of the iterators from [iterutils](https://boltons.readthedocs.io/en/latest/iterutils.html).\n\n\u0026nbsp;\n\n\n\n\n## Installation\n\n```\nnimble install itertools\n```\n\nRequired Nim version is at least 0.18.0.\n\n\u0026nbsp;\n\n\n\n\n## Supported iterators\n\n* infinite iterators:\n    * count\n    * cycle\n    * repeat\n\n* terminating iterators:\n    * accumulate\n    * chain\n    * compress\n    * dropWhile\n    * filterFalse\n    * groupBy\n    * groupConsecutive\n    * islice\n    * takeWhile\n\n* combinatoric iterators:\n    * product\n    * distinctPermutations\n    * permutations\n    * combinations\n\n* iterutils iterators:\n    * chunked\n    * windowed\n    * pairwise\n    * unique\n\n\n\u0026nbsp;\n\n\n\n\n## Usage\n\nFor more comprehensive examples, see the [documentation](https://narimiran.github.io/itertools).\n\n\n\n### Infinite iterators\n\nWARNING: Version 0.3.0 introduced breaking changes regarding how these three iterators are implemented and used.\nSee the examples below for the new behaviour.\n\n```nim\nimport itertools\n\nfor i in count(5):\n  if i \u003e 9: break\n  echo i\n# 5; 6; 7; 8; 9\n\nfor i in count(100, 30):\n  if i \u003e 200: break\n  echo i\n# 100; 130; 160; 190\n\nvar n = 0\nfor i in @[\"I\", \"repeat\", \"myself\"].cycle:\n  inc n\n  if n \u003e 8: break\n  echo i\n# I; repeat; myself; I; repeat; myself; I; repeat\n\nfor i in \"Beetlejuice\".repeat(3):\n  echo i\n# Beetlejuice; Beetlejuice; Beetlejuice\n\nvar k = 0\nfor i in \"forever\".repeat:\n  inc k\n  if k \u003e 6: break\n  echo i\n# forever; forever; forever; forever; forever; forever\n```\n\n\n\n\n### Terminating iterators\n\n```nim\nimport itertools\nimport future # to use `=\u003e` for anonymous proc\n\n\nlet # you can use: sequences, arrays, strings\n  numbers = @[1, 3, 7, 8, 4, 2, 6, 5, 9]\n  constants = [2.7183, 3.1416, 1.4142, 1.7321]\n  word = \"abracadabra\"\n\n\nfor i in accumulate(constants, (x, y) =\u003e x + y):\n  echo i\n# 2.7183; 5.8599; 7.2741; 9.0062\n\nfor i in compress(numbers, [true, true, false, true, false, true]):\n  echo i\n# 1; 3; 8; 2\n\nfor i in dropWhile(numbers, x =\u003e (x != 8)):\n  echo i\n# 8; 4; 2; 6; 5; 9\n\nfor i in filterFalse(word, x =\u003e (x == 'a')):\n  echo i\n# b; r; c; d; b; r\n\nfor key, group in numbers.groupBy(x =\u003e x mod 2 == 0):\n  echo \"key: \", key, \" group: \", group\n# key: true, group: @[8, 4, 2, 6]; key: false, group: @[1, 3, 7, 5, 9]\n\nfor key, group in word.groupBy():\n  echo group\n# @['a', 'a', 'a', 'a', 'a']; @['b', 'b']; @['c']; @['d']; @['r', 'r']\n\nfor key, group in groupConsecutive(\"aaabaabb\"):\n  echo group\n# @['a', 'a', 'a']; @['b']; @['a', 'a']; @['b', 'b']\n\nfor i in islice(numbers, 5):\n  echo i\n# 2; 6; 5; 9\n\nfor i in islice(word, 1, step=2):\n  echo i\n# b; a; a; a; r\n\nfor i in islice(numbers, stop=5, step=2):\n  echo i\n# 1; 7; 4\n\nfor i in takeWhile(constants, x =\u003e (x \u003e= 2.0)):\n  echo i\n# 2.7183; 3.1416\n\nfor i in chain(@[1, 3, 5], @[2, 4, 6], @[7, 8, 9]):\n  echo i\n# 1; 3; 5; 2; 4; 6; 7; 8; 9\n```\n\n\n\n\n### Combinatoric iterators\n\n```nim\nimport itertools\nimport strutils # to join seq[char] into a string\n\n\nlet # you can use: sequences, arrays, strings\n  numbers = @[1, 3, 7, 8, 4]\n  constants = [2.7183, 3.1416]\n  word = \"abba\"\n\n\nfor i in product([0, 1], repeat = 3):\n  echo i\n# @[0, 0, 0]; @[0, 0, 1]; @[0, 1, 0]; @[0, 1, 1]; @[1, 0, 0]; @[1, 0, 1]; @[1, 1, 0]; @[1, 1, 1]\n\nfor i in product(numbers, constants):\n  echo i\n# (a: 1, b: 2.7183); (a: 1, b: 3.1416); (a: 3, b: 2.7183); (a: 3, b: 3.1416); (a: 7, b: 2.7183); (a: 7, b: 3.1416); (a: 8, b: 2.7183); (a: 8, b: 3.1416); (a: 4, b: 2.7183); (a: 4, b: 3.1416)\n\nfor i in distinctPermutations(word):\n  echo i.join\n# aabb; abab; abba; baab; baba; bbaa\n\nfor i in permutations(word):\n  echo i.join\n# abba; abab; abba; abab; aabb; aabb; baba; baab; bbaa; bbaa; baab; baba; baba; baab; bbaa; bbaa; baab; baba; aabb; aabb; abab; abba; abab; abba\n\nfor i in combinations(5, 3):\n  echo i\n# @[0, 1, 2]; @[0, 1, 3]; @[0, 1, 4]; @[0, 2, 3]; @[0, 2, 4]; @[0, 3, 4]; @[1, 2, 3]; @[1, 2, 4]; @[1, 3, 4]; @[2, 3, 4]\n\nfor i in combinations(numbers, 2):\n  echo i\n# @[1, 3]; @[1, 7]; @[1, 8]; @[1, 4]; @[3, 7]; @[3, 8]; @[3, 4]; @[7, 8]; @[7, 4]; @[8, 4]\n```\n\n\n\n\n### Iterutils iterators\n\n```nim\nimport itertools\n\n\nlet # you can use: sequences, arrays, strings\n  numbers = @[1, 3, 7, 8, 4, 2, 6, 5, 9]\n  constants = [2.7183, 3.1416, 1.4142, 1.7321]\n  word = \"abracadabra\"\n\n\nfor i in chunked(numbers, 3):\n  echo i\n# @[1, 3, 7]; @[8, 4, 2]; @[6, 5, 9]\n\nfor i in windowed(numbers, 4):\n  echo i\n# @[1, 3, 7, 8]; @[3, 7, 8, 4]; @[7, 8, 4, 2]; @[8, 4, 2, 6]; @[4, 2, 6, 5]; @[2, 6, 5, 9]\n\nfor i in pairwise(constants):\n  echo i\n# @[2.7183, 3.1416]; @[3.1416, 1.4142]; @[1.4142, 1.7321]\n\nfor i in unique(word):\n  echo i\n# a; b; r; c; d\n```\n\n\n\u0026nbsp;\n\n\n\n\n## Contributing\n\nThere is probably a lot of room for improvement.\nFeel free to fork the repo and submit your PRs.\n\nBefore submitting, run `nim doc -o:./docs/index.html ./src/itertools.nim` to make sure that all the asserts in `runnableExamples` are passing.\n\n\n\u0026nbsp;\n\n\n\n\n## License\n\n[MIT license](LICENSE.txt)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnarimiran%2Fitertools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnarimiran%2Fitertools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnarimiran%2Fitertools/lists"}