{"id":18133143,"url":"https://github.com/cemderv/linq","last_synced_at":"2025-04-06T16:20:35.986Z","repository":{"id":258173844,"uuid":"860572092","full_name":"cemderv/linq","owner":"cemderv","description":"A header-only LINQ library for C++","archived":false,"fork":false,"pushed_at":"2024-10-01T17:43:47.000Z","size":99,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-17T22:56:44.630Z","etag":null,"topics":["cpp","linq","modern-cpp","ranges"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cemderv.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2024-09-20T17:31:08.000Z","updated_at":"2024-10-01T17:10:12.000Z","dependencies_parsed_at":"2024-10-18T06:46:40.283Z","dependency_job_id":null,"html_url":"https://github.com/cemderv/linq","commit_stats":null,"previous_names":["cemderv/linq"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cemderv%2Flinq","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cemderv%2Flinq/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cemderv%2Flinq/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cemderv%2Flinq/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cemderv","download_url":"https://codeload.github.com/cemderv/linq/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222576170,"owners_count":17005447,"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":["cpp","linq","modern-cpp","ranges"],"created_at":"2024-11-01T13:07:02.308Z","updated_at":"2024-11-01T13:07:03.084Z","avatar_url":"https://github.com/cemderv.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# linq - LINQ for C++\n\nlinq is a header-only LINQ library for C++ 17 and newer.\nIt has no dependencies and neatly integrates into the STL by taking advantage of modern C++ features.\n\nlinq ...\n\n- resolves all type-related functionality at compile-time; no virtual dispatch is used\n- uses lazy evaluation, so your queries still work even after you modify the container it's based on\n- focuses on immutability, so your queries stay predictable\n- is efficient in the way it works with your data; it avoids copies and instead moves data wherever it can\n- generates an operation chain at compile-time\n- works with all generic container types, not just the STL\n- does not rely on exceptions and instead reports errors at compile-time\n- uses the beloved dot operator!\n\n---\n\nlinq has been tested with the following compilers:\n\n- Clang 16+\n- GCC 10+\n- AppleClang 13+\n- MSVC 2019+\n\n[![Linux-Clang](https://github.com/cemderv/linq/actions/workflows/build-linux-clang.yml/badge.svg)](https://github.com/cemderv/linq/actions/workflows/build-linux-clang.yml) [![Linux-GCC](https://github.com/cemderv/linq/actions/workflows/build-linux-gcc.yml/badge.svg)](https://github.com/cemderv/linq/actions/workflows/build-linux-gcc.yml) [![macOS-AppleClang](https://github.com/cemderv/linq/actions/workflows/build-macos-appleclang.yml/badge.svg)](https://github.com/cemderv/linq/actions/workflows/build-macos-appleclang.yml) [![Windows-MSVC](https://github.com/cemderv/linq/actions/workflows/build-windows-msvc.yml/badge.svg)](https://github.com/cemderv/linq/actions/workflows/build-windows-msvc.yml)\n\n## Examples\n\n#### Intro\n\n```cpp\n#include \u003clinq.hpp\u003e\n\nstruct Person {\n    string name;\n    int age;\n};\n\nconst vector\u003cPerson\u003e people {\n    { .name = \"P1\", .age = 20 },\n    { .name = \"P2\", .age = 21 },\n    { .name = \"P3\", .age = 22 },\n};\n  \nauto query = linq::from(\u0026people)\n                  .where( [](const Person\u0026 p) { return p.age \u003e 20; } );\n                    \nfor (const Person\u0026 p : query) {\n    println(\"{}, {}\", p.name, p.age);\n}\n```\n\nOutput:\n\n```\nP2, 21\nP3, 22\n```\n\n#### Aggregation\n\n```cpp\ndouble average_age = linq::from(\u0026people)\n                          .select( [](const Person\u0026 p) { return p.age; } )\n                          .average();\n\nint people_over_20 = linq::from(\u0026people)\n                          .where( [](const Person\u0026 p) { return p.age \u003e 20; } )\n                          .count();\n```\n\n#### Element access\n\n```cpp\nconst auto over_20 = [](const Person\u0026 p) { return p.age \u003e 20; };\nconst auto under_20 = [](const Person\u0026 p) { return p.age \u003c 20; };\n\noptional\u003cPerson\u003e first_over_20 = linq::from(\u0026people).first(over_20);\n// first_over_20 = P2, 21\n\noptional\u003cPerson\u003e last_over_20 = linq::from(\u0026people).last(over_20);\n// last_over_20 = P3, 22\n\noptional\u003cPerson\u003e first_under_20 = linq::from(\u0026people).last(under_20);\n// first_under_20 = empty optional\n```\n\n#### Partitioning\n\n```cpp\nconst vector numbers { 1, 2, 3, 4, 5, 6 };\n\nauto query1 = linq::from(\u0026numbers).skip(3); // 4, 5, 6\n\nauto query2 = linq::from(\u0026numbers)\n                   .skip_while( [](int i) { return i \u003c 4; } ); // 4, 5, 6\n\nauto query3 = linq::from(\u0026numbers).take(4); // 1, 2, 3, 4\n\nauto query4 = linq::from(\u0026numbers)\n                   .take_while( [](int i) { return i \u003c 4; } ); // 1, 2, 3\n```\n\n#### Sorting\n\n```cpp\nconst vector words { \"hello\"s, \"world\"s, \"here\"s, \"are\"s, \"some\"s, \"sorted\"s, \"words\"s };\n\nauto query = linq::from(\u0026words)\n                  .order_by_ascending( [](const string\u0026 word) { return word.size(); } )\n                  .then_by_ascending( [](const string\u0026 word) { return word; } );\n// query = are, here, some, hello, words, world, sorted\n\nauto rev = query.reverse();\n// rev = sorted, world, words, hello, some, here, are\n```\n\n#### Concatenation\n\n```cpp\nconst vector numbers1 { 1, 2, 3 };\nconst vector numbers2 { 4, 5, 6 };\n\nauto range = linq::from(\u0026numbers1)\n                  .append(linq::from(\u0026numbers2));\n\n// range = 1, 2, 3, 4, 5, 6\n```\n\n#### Removing duplicates\n\n```cpp\nconst vector numbers { 1, 2, 3, 3, 5, 4, 5, 6, 7 };\n\nconst auto query = linq::from(\u0026numbers).distinct();\n// query = 1, 2, 3, 5, 4, 6, 7\n```\n\n#### Composition\n\n```cpp\nauto people = linq::from(\u0026people);\nauto age_over_20 = [](const Person\u0026 p) { return p.age \u003e 20; };\nauto people_over_20 = people.where(age_over_20);\nauto person_age = [](const Person\u0026 p) { return p.age; };\n\ndouble average_age = people_over_20.select(person_age).average();\n```\n\n#### Producing containers\n\n`to_vector`:\n\n```cpp\nconst array numbers { 1, 2, 3, 4 };\n\nvector vec = linq::from(\u0026numbers)\n                  .where( [](int i) { return i \u003e 1; } )\n                  .select_to_string()\n                  .to_vector(); // copy elements to a new std::vector\n\n// 'vec' is of type std::vector\u003cstd::string\u003e\n// with contents [\"2\", \"3\", \"4\"]\n```\n\n`to_map`:\n\n```cpp\nconst array pairs {\n    pair{ 1, \"str1\"s },\n    pair{ 5, \"str5\"s },\n    pair{ -10, \"str-10\"s },\n};\n\nconst auto query = linq::from(\u0026pairs)\n                        .where( [](const auto\u0026 p) { return p.first \u003c 20; } );\n\nmap my_map = query.to_map();\n\nfor (const auto\u0026 [key, value] : my_map) {\n    println(\"[{}: {}]\", key, value);\n}\n```\n\nOutput:\n\n```\n[-10: str-10]\n[1: str1]\n[5: str5]\n```\n\n### Generation\n\n```cpp\nauto range1 = linq::from_to(0, 5);           // 0, 1, 2, 3, 4, 5\n\nauto range2 = linq::from_to(0.0, 1.5, 0.5);  // 0, 0.5, 1, 1.5\n\nauto range3 = linq::from_to(0, 3).repeat(1); // 0, 1, 2, 3, 0, 1, 2, 3\n\nauto range4 = linq::generate([](size_t i) {\n    if (i \u003c 5)\n        return linq::generate_return(i * 2);\n\n    return linq::generate_finish\u003csize_t\u003e();\n});\n// range4 = 0, 2, 4, 6, 8\n```\n\n---\n\nBelow you will find a list of all supported functions and operators.\n\n### Query Constructors\n\n- [from](https://github.com/cemderv/linq/wiki/Query-Constructors#from)\n- [from_mutable](https://github.com/cemderv/linq/wiki/Query-Constructors#from_mutable)\n- [from_copy](https://github.com/cemderv/linq/wiki/Query-Constructors#from_copy)\n\n### Container Producers\n\n- [to_vector](https://github.com/cemderv/linq/wiki/Container-Producers#to_vector)\n- [to_map / to_unordered_map](https://github.com/cemderv/linq/wiki/Container-Producers#to_map--to_unordered_map)\n\n### Operators\n\n- [Aggregation](https://github.com/cemderv/linq/wiki/Aggregate-Operators)\n    - [aggregate](https://github.com/cemderv/linq/wiki/Aggregate-Operators#aggregate)\n    - [average](https://github.com/cemderv/linq/wiki/Aggregate-Operators#average)\n    - [count](https://github.com/cemderv/linq/wiki/Aggregate-Operators#count)\n    - [max](https://github.com/cemderv/linq/wiki/Aggregate-Operators#max)\n    - [min](https://github.com/cemderv/linq/wiki/Aggregate-Operators#min)\n    - [sum](https://github.com/cemderv/linq/wiki/Aggregate-Operators#sum)\n- [Concatenation](https://github.com/cemderv/linq/wiki/Concatenation-Operators)\n    - [append](https://github.com/cemderv/linq/wiki/Concatenation-Operators#append)\n- [Element Access](https://github.com/cemderv/linq/wiki/Element-Operators)\n    - [element_at](https://github.com/cemderv/linq/wiki/Element-Operators#element_at)\n    - [first](https://github.com/cemderv/linq/wiki/Element-Operators#first)\n    - [last](https://github.com/cemderv/linq/wiki/Element-Operators#last)\n- [Filters](https://github.com/cemderv/linq/wiki/Filter-Operators)\n    - [where](https://github.com/cemderv/linq/wiki/Filter-Operators#where)\n- [Generation](https://github.com/cemderv/linq/wiki/Generation-Operators)\n    - [from_to](https://github.com/cemderv/linq/wiki/Generation-Operators#from_to)\n    - [repeat](https://github.com/cemderv/linq/wiki/Generation-Operators#repeat)\n    - [generate](https://github.com/cemderv/linq/wiki/Generation-Operators#generate)\n- [Join](https://github.com/cemderv/linq/wiki/Join-Operators)\n    - [join](https://github.com/cemderv/linq/wiki/Join-Operators#join)\n- [Partition](https://github.com/cemderv/linq/wiki/Partition-Operators)\n    - [skip](https://github.com/cemderv/linq/wiki/Partition-Operators#skip)\n    - [skip_while](https://github.com/cemderv/linq/wiki/Partition-Operators#skip_while)\n    - [take](https://github.com/cemderv/linq/wiki/Partition-Operators#take)\n    - [take_while](https://github.com/cemderv/linq/wiki/Partition-Operators#take_while)\n- [Projection](https://github.com/cemderv/linq/wiki/Projection-Operators)\n    - [select](https://github.com/cemderv/linq/wiki/Projection-Operators#select)\n    - [select_to_string](https://github.com/cemderv/linq/wiki/Projection-Operators#select_to_string)\n    - [select_many](https://github.com/cemderv/linq/wiki/Projection-Operators#select_many)\n- [Quantifiers](https://github.com/cemderv/linq/wiki/Quantifier-Operators)\n    - [all](https://github.com/cemderv/linq/wiki/Quantifier-Operators#all)\n    - [any](https://github.com/cemderv/linq/wiki/Quantifier-Operators#any)\n- [Set](https://github.com/cemderv/linq/wiki/Set-Operators)\n    - [distinct](https://github.com/cemderv/linq/wiki/Set-Operators#distinct)\n- [Sorting](https://github.com/cemderv/linq/wiki/Sorting-Operators)\n    - [order_by](https://github.com/cemderv/linq/wiki/Sorting-Operators#order_by)\n    - [then_by](https://github.com/cemderv/linq/wiki/Sorting-Operators#then_by)\n    - [reverse](https://github.com/cemderv/linq/wiki/Sorting-Operators#reverse)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcemderv%2Flinq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcemderv%2Flinq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcemderv%2Flinq/lists"}