https://github.com/jscheiny/Streams
Lazy evaluation in C++ - http://jscheiny.github.io/Streams/
https://github.com/jscheiny/Streams
Last synced: over 1 year ago
JSON representation
Lazy evaluation in C++ - http://jscheiny.github.io/Streams/
- Host: GitHub
- URL: https://github.com/jscheiny/Streams
- Owner: jscheiny
- License: mit
- Created: 2014-06-05T16:36:27.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2021-07-12T10:40:05.000Z (almost 5 years ago)
- Last Synced: 2024-07-31T22:42:09.547Z (almost 2 years ago)
- Language: C++
- Homepage:
- Size: 377 KB
- Stars: 688
- Watchers: 49
- Forks: 56
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# C++ Streams
Streams 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.
To use, simply `#include "Stream.h"`, and compile using a C++14 compatible compiler. All streams classes/functions can be found in the `stream` namespace.
Links:
* [Documentation](http://jscheiny.github.io/Streams)
* [Github](http://github.com/jscheiny/Streams)
* [Contact](http://scheinerman.net/jonah)
C++ Streams are distributed under the MIT open source license.
Copyright (c) 2014 by Jonah Scheinerman
## Examples
### Coin flip experiment:
```cpp
using namespace stream;
using namespace stream::op;
int number_heads(int flips) {
return MakeStream::coin_flips()
| limit(flips)
| filter()
| count();
};
void experiment(int trials, int flips) {
auto stats = MakeStream::generate(std::bind(number_heads, flips))
| limit(trials)
| reducers::SummaryStats().reducer();
std::cout << stats << std::endl;
}
// Example output for experiment(1000, 1000):
// N=1000, u=499.812, s=252.763, min=452, max=549
```
### Investigating the Collatz conjecture:
```cpp
#include "Stream.h"
#include
using namespace stream;
using namespace stream::op;
int collatz_next(int value) {
if(value % 2 == 0)
return value / 2;
return 3 * value + 1;
}
int collatz_sequence_length(int start) {
return MakeStream::iterate(start, collatz_next)
| take_while([](int x) { return x != 1; })
| count();
}
void print_collatz(int start) {
MakeStream::iterate(start, collatz_next)
| take_while([](int x) { return x != 1; })
| print_to(std::cout, " -> ");
std::cout << "1" << std::endl;
}
int main(int argc, char const *argv[]) {
print_collatz(24);
}
// print_collatz(10):
// 24 -> 12 -> 6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
```
### Vector operations:
```cpp
std::vector x = /* ... */;
std::vector y = /* ... */;
auto to_stream = [](std::vector& vec) {
return MakeStream::from(vec);
};
std::vector sum_vec = to_stream(x) + to_stream(y);
std::vector diff_vec = to_stream(x) - to_stream(y);
double dot_product = (to_stream(x) * to_stream(y)) | sum();
std::vector scaling = to_stream(x) * 10;
std::vector translating = to_stream(x) + 3.7;
```
### Set operations:
```cpp
std::set x = /* ... */;
set::set y = /* ... */;
auto to_stream = [](std::set& vec) {
return MakeStream::from(vec);
};
std::set set_union = to_stream(x) | union_with(to_stream(y));
// Better than:
// std::set result;
// std::set_union(x.begin(), x.end(), y.begin(), y.end(),
// inserter(result, result.end()));
std::set set_intersect = to_stream(x)
| intersection_with(to_stream(y));
std::set set_diff = to_stream(x)
| difference_with(to_stream(y));
std::set set_sym_diff = to_stream(x)
| symmetric_difference_with(to_stream(y));
```
### Adding unique ids:
```cpp
std::vector objects = /* ... */;
std::vector objects_with_ids = MakeStream::from(objects)
| zip_with(MakeStream::counter(1), [](T&& object, int id) {
object.set_id(id);
return object;
});
```
### Printing containers:
```cpp
(MakeStream::from(container) | print_to(std::cout)) << std::endl;
```
### Operator composition:
```cpp
auto square = map_([](auto&& x) { return x * x; });
(MakeStream::range(1, 6) | square | print_to(std::cout)) << std::endl; // 1 4 9 16 25
auto square_and_sum = square | sum();
int result = MakeStream::range(1, 4) | square_and_sum; // 14
auto every_nth = [](int n) {
return zip_with(MakeStream::counter(0))
| filter([=](const auto& tup) { return std::get<1>(tup) % n == 0; })
| map_([](auto&& tup) { return std::get<0>(tup); });
};
MakeStream::from({1, 3, 8, 4, 7}) | every_nth(2) | print_to(std::cout); // 1 8 7
```