Skip to content

A very simple implementation of java streams in C++ using template templates

Notifications You must be signed in to change notification settings

amiryeshurun/Streams

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Streams

This is a very simple implementation of java streams in C++.


Requirments:

In order to use the stream with your own container, you have to make sure that your container is iterable. You are also allowed to specify which container type should be used in order to manage the stream behind the scenes. For example if you would like the stream to be implemented using a list, then define it as follows:

Stream<Type, std::list>

If no value is specified here (second template parameter), then the default value std::vector is selected.

Example:

std::vector<int> vec = {1, 2, 3, 4, 5};
std::cout << Stream(vec).map([](const auto& elem) {
    return elem + 1;
}).reduce(0, [](const auto& elm1, const auto& elm2) {
    return elm1 + elm2;
}) << std::endl;

Here the vector vec is mapped to a new vector {2, 3, 4, 5, 6}. Then, the reduce function is calculating the sum of the new vector. The result is being printed to the standard output.

About

A very simple implementation of java streams in C++ using template templates

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages