{"id":15574345,"url":"https://github.com/gpmueller/ranges-syntax","last_synced_at":"2025-08-25T22:14:55.699Z","repository":{"id":152302610,"uuid":"119179563","full_name":"GPMueller/ranges-syntax","owner":"GPMueller","description":"Trying to create a consistent shorthand syntax for creation and usage of ranges","archived":false,"fork":false,"pushed_at":"2018-01-27T17:35:11.000Z","size":326,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T07:12:44.224Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","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/GPMueller.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-27T16:10:23.000Z","updated_at":"2018-01-27T16:41:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"f0887427-1cb6-4eed-b19a-3ca17865a360","html_url":"https://github.com/GPMueller/ranges-syntax","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/GPMueller/ranges-syntax","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GPMueller%2Franges-syntax","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GPMueller%2Franges-syntax/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GPMueller%2Franges-syntax/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GPMueller%2Franges-syntax/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GPMueller","download_url":"https://codeload.github.com/GPMueller/ranges-syntax/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GPMueller%2Franges-syntax/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272140212,"owners_count":24880462,"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","status":"online","status_checked_at":"2025-08-25T02:00:12.092Z","response_time":1107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-02T18:16:41.697Z","updated_at":"2025-08-25T22:14:55.640Z","avatar_url":"https://github.com/GPMueller.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"Shorthand syntax for creation and usage of ranges\n=================================================\n\nThis document tries to define a reasonable and meaningful definition of a more concise syntax for\ncreating and using ranges in C++.\nUsing ranges of iterable containers is currently quite cumbersome. Eric Niebler's range library\nimproves on a lot of the present problems, but a more concise way of writing mathematical and\nnumerical code would be a great improvement. Especially with respect to slicing of multidimensional\ndata arrays, a more pythonic syntax of ranges would make code shorter and easier to read.\n\nAs references, see for example\n- http://ericniebler.com/2014/12/07/a-slice-of-python-in-c/\n- http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4671.pdf\n- https://www.reddit.com/r/cpp/comments/6ngkgc/2017_toronto_iso_c_committee_discussion_thread/\n\n\nPython\n-------------------------------------------------\n\nIn Python, ranges are either explicitly created, e.g. `for i in range(10)`, or given with the `:` syntax in\nthe default array/list/whatever `operator[]`, e.g. `x = y[:10]`. `for i in :10` is illegal.\n\n\nDefinitions\n-------------------------------------------------\n\n- any `type..type` is interpreted as a range construction, where the operands may determine the type of range\n- a `range..difference_t` statement is interpreted as a range construction with a stride\n- therefore a `3..12..2` is a `range\u003cint\u003e(3,12,2)`, i.e. a range from 3 to (including) 11 with stride 2\n- therefore a `'a'..'g'` is a `range\u003cchar\u003e('a', 'g')`\n\nThis would allow free-standing range declarations and lots of potential use-cases.\n\n\nLanguage changes\n-------------------------------------------------\n\nTODO...\n\n\nStandard library changes\n-------------------------------------------------\n\nTODO...\n\n\nUsage examples\n-------------------------------------------------\n\nBasic integer ranges:\n```C++\n// Without stride\nrange r = 5..10;     // range [5..10]  stride 1\nrange r = ..10;      // range [0..10]  stride 1\nrange r = 5..;       // range [5..end] stride 1\nrange r = ..;        // range [0..end] stride 1\n// With stride\nrange r = 5..10..2;   // range [5..10]  stride 2\nrange r = ..10:2;    // range [0..10]  stride 2\nrange r = 5....2;     // range [5..end] stride 2 \u003c----- this one is not nice to read!\nrange r = ....2;      // range [0..end] stride 2 \u003c----- this one is not nice to read!\n```\n\nRange-based for loop:\n```C++\n// Loop over index range\nfor (int i : 5..10) // same as: for (int i : range(5,10))\n{\n  vec[i] ...;\n}\n// Loop over array slice\nfor (double\u0026 val : vec[5..10]) // same as: for (double\u0026 val :  vec[range(5,10)])\n{\n  sum += val;\n}\n// Loop over enumerate of array slice\nfor ([int i, double\u0026 val] : enumerate(vec[5..10])) // quite close to python: for i, val in enumerate(vec[5:10])\n{\n  vec2[i] += val;\n}\n```\n\nShould overloads be defined on top of basic and std types?\n```C++\nint * c_arr;             // a simple pointer, i.e. we don't know the array's size\nstd::array\u003cint, 10\u003e arr; // std::array, i.e. we know the size\n\nstd::array\u003cint, 5\u003e a1 = c_arr[..];   // illegal, as `end` is unknown\nstd::array\u003cint, 5\u003e a2 = c_arr[3..8]; // legal, as `range` has the correct compile-time size. However, this may cause a runtime exception running out of bounds\nstd::array\u003cint, 5\u003e a3 = arr[3..8];   // legal, as `range` has the correct compile-time size. Cannot fail at runtime, as the bounds are checked at compile-time\n```\n\n\nUser implementations\n-------------------------------------------------\n\nBy implementing a `Cursor`, which is used by a `Range Facade`, a user can define his own\nrange behaviour simply by implementing `current`, `next` and `done`. If a strided range is\nneeded, probably `distance_to` and `advance` are needed, too.\n\n\n\nProblems\n-------------------------------------------------\n\n- Unconstrained ranges are syntactically ugly, e.g. `....5` for an infinite range of stride 5\n\n\nOpen Questions\n-------------------------------------------------\n\n- which types should the `..` syntax support? My use cases are only integer...\n- could we emulate this with string literals, e.g. `\"3..5\"r`?\n- what features of range-v3 should be used?\n- what exactly needs to change in the language itself and what in the stdlib?\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgpmueller%2Franges-syntax","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgpmueller%2Franges-syntax","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgpmueller%2Franges-syntax/lists"}