Lazy evaluation in C++ - http://jscheiny.github.io/Streams/
Lazy evaluation in C++ - http://jscheiny.github.io/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:
C++ Streams are distributed under the MIT open source license. Copyright (c) 2014 by Jonah Scheinerman
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 ");
std::cout 12 -> 6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1std::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;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));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;
});(MakeStream::from(container) | print_to(std::cout)) (tup) % n == 0; })
| map_([](auto&& tup) { return std::get(tup); });
};
MakeStream::from({1, 3, 8, 4, 7}) | every_nth(2) | print_to(std::cout); // 1 8 7No open issues yet, or sync has not completed.