{"id":13418242,"url":"https://github.com/jscheiny/Streams","last_synced_at":"2025-03-15T03:30:45.900Z","repository":{"id":17713574,"uuid":"20532849","full_name":"jscheiny/Streams","owner":"jscheiny","description":"Lazy evaluation in C++ - http://jscheiny.github.io/Streams/","archived":false,"fork":false,"pushed_at":"2021-07-12T10:40:05.000Z","size":386,"stargazers_count":688,"open_issues_count":10,"forks_count":56,"subscribers_count":49,"default_branch":"master","last_synced_at":"2024-07-31T22:42:09.547Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C++","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/jscheiny.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}},"created_at":"2014-06-05T16:36:27.000Z","updated_at":"2024-06-19T11:02:30.000Z","dependencies_parsed_at":"2022-08-25T11:01:15.347Z","dependency_job_id":null,"html_url":"https://github.com/jscheiny/Streams","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jscheiny%2FStreams","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jscheiny%2FStreams/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jscheiny%2FStreams/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jscheiny%2FStreams/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jscheiny","download_url":"https://codeload.github.com/jscheiny/Streams/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243681024,"owners_count":20330152,"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-07-30T22:01:00.056Z","updated_at":"2025-03-15T03:30:45.890Z","avatar_url":"https://github.com/jscheiny.png","language":"C++","funding_links":[],"categories":["TODO scan for Android support in followings","C++"],"sub_categories":[],"readme":"# C++ Streams\n\nStreams is a C++ library that provides lazy evaluation and functional-style transformations on the data, to ease the use of C++ standard library containers and algorithms. Streams support many common functional operations such as map, filter, and reduce, as well as various other useful operations such as various set operations (union, intersection, difference), partial sum, and adjacent difference, as well as many others.\n\nTo use, simply `#include \"Stream.h\"`, and compile using a C++14 compatible compiler. All streams classes/functions can be found in the `stream` namespace.\n\nLinks:\n\n* [Documentation](http://jscheiny.github.io/Streams)\n* [Github](http://github.com/jscheiny/Streams)\n* [Contact](http://scheinerman.net/jonah)\n\nC++ Streams are distributed under the MIT open source license.\nCopyright (c) 2014 by Jonah Scheinerman\n\n## Examples\n\n### Coin flip experiment:\n\n```cpp\nusing namespace stream;\nusing namespace stream::op;\n\nint number_heads(int flips) {\n    return MakeStream::coin_flips()\n        | limit(flips)\n        | filter()\n        | count();\n};\n\nvoid experiment(int trials, int flips) {\n    auto stats = MakeStream::generate(std::bind(number_heads, flips))\n        | limit(trials)\n        | reducers::SummaryStats\u003cint\u003e().reducer();\n    std::cout \u003c\u003c stats \u003c\u003c std::endl;\n}\n\n// Example output for experiment(1000, 1000):\n// N=1000, u=499.812, s=252.763, min=452, max=549\n```\n\n### Investigating the Collatz conjecture:\n\n```cpp\n#include \"Stream.h\"\n#include \u003ciostream\u003e\n\nusing namespace stream;\nusing namespace stream::op;\n\nint collatz_next(int value) {\n    if(value % 2 == 0)\n        return value / 2;\n    return 3 * value + 1;\n}\n\nint collatz_sequence_length(int start) {\n    return MakeStream::iterate(start, collatz_next)\n        | take_while([](int x) { return x != 1; })\n        | count();\n}\n\nvoid print_collatz(int start) {\n    MakeStream::iterate(start, collatz_next)\n        | take_while([](int x) { return x != 1; })\n        | print_to(std::cout, \" -\u003e \");\n    std::cout \u003c\u003c \"1\" \u003c\u003c std::endl;\n}\n\nint main(int argc, char const *argv[]) {\n    print_collatz(24);\n}\n\n// print_collatz(10):\n// 24 -\u003e 12 -\u003e 6 -\u003e 3 -\u003e 10 -\u003e 5 -\u003e 16 -\u003e 8 -\u003e 4 -\u003e 2 -\u003e 1\n```\n\n### Vector operations:\n\n```cpp\nstd::vector\u003cdouble\u003e x = /* ... */;\nstd::vector\u003cdouble\u003e y = /* ... */;\n\nauto to_stream = [](std::vector\u003cdouble\u003e\u0026 vec) {\n    return MakeStream::from(vec);\n};\n\nstd::vector\u003cdouble\u003e sum_vec = to_stream(x) + to_stream(y);\nstd::vector\u003cdouble\u003e diff_vec = to_stream(x) - to_stream(y);\ndouble dot_product = (to_stream(x) * to_stream(y)) | sum();\nstd::vector\u003cdouble\u003e scaling = to_stream(x) * 10;\nstd::vector\u003cdouble\u003e translating = to_stream(x) + 3.7;\n```\n\n### Set operations:\n\n```cpp\nstd::set\u003cint\u003e x = /* ... */;\nset::set\u003cint\u003e y = /* ... */;\n\nauto to_stream = [](std::set\u003cint\u003e\u0026 vec) {\n    return MakeStream::from(vec);\n};\n\nstd::set\u003cint\u003e set_union = to_stream(x) | union_with(to_stream(y));\n// Better than:\n//   std::set\u003cint\u003e result;\n//   std::set_union(x.begin(), x.end(), y.begin(), y.end(),\n//                  inserter(result, result.end()));\nstd::set\u003cint\u003e set_intersect = to_stream(x)\n    | intersection_with(to_stream(y));\nstd::set\u003cint\u003e set_diff = to_stream(x)\n    | difference_with(to_stream(y));\nstd::set\u003cint\u003e set_sym_diff = to_stream(x)\n    | symmetric_difference_with(to_stream(y));\n```\n\n### Adding unique ids:\n\n```cpp\nstd::vector\u003cT\u003e objects = /* ... */;\n\nstd::vector\u003cT\u003e objects_with_ids = MakeStream::from(objects)\n    | zip_with(MakeStream::counter(1), [](T\u0026\u0026 object, int id) {\n        object.set_id(id);\n        return object;\n    });\n```\n\n### Printing containers:\n\n```cpp\n(MakeStream::from(container) | print_to(std::cout)) \u003c\u003c std::endl;\n```\n\n### Operator composition:\n\n```cpp\nauto square = map_([](auto\u0026\u0026 x) { return x * x; });\n(MakeStream::range(1, 6) | square | print_to(std::cout)) \u003c\u003c std::endl; // 1 4 9 16 25\n\nauto square_and_sum = square | sum();\nint result = MakeStream::range(1, 4) | square_and_sum; // 14\n\nauto every_nth = [](int n) {\n    return zip_with(MakeStream::counter(0))\n         | filter([=](const auto\u0026 tup) { return std::get\u003c1\u003e(tup) % n == 0; })\n         | map_([](auto\u0026\u0026 tup) { return std::get\u003c0\u003e(tup); });\n};\n\nMakeStream::from({1, 3, 8, 4, 7}) | every_nth(2) | print_to(std::cout); // 1 8 7\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjscheiny%2FStreams","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjscheiny%2FStreams","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjscheiny%2FStreams/lists"}