Giter Site home page Giter Site logo

streams's Introduction

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:

C++ Streams are distributed under the MIT open source license. Copyright (c) 2014 by Jonah Scheinerman

Examples

Coin flip experiment:

using namespace stream;
using namespace stream::op;

int number_heads(int flips) {
    return MakeStream::coin_flips()
        | limit(1000)
        | filter()
        | count();
};

void experiment(int trials, int flips) {
    auto stats = MakeStream::generate(std::bind(number_heads, flips))
        | limit(trials)
        | reducers::SummaryStats<int>().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:

#include "Stream.h"
#include <iostream>

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:

std::vector<double> x = /* ... */;
std::vector<double> y = /* ... */;

auto to_stream = [](std::vector<double>& vec) {
    return MakeStream::from(vec);
};

std::vector<double> sum_vec = to_stream(x) + to_stream(y);
std::vector<double> diff_vec = to_stream(x) - to_stream(y);
double dot_product = (to_stream(x) * to_stream(y)) | sum();
std::vector<double> scaling = to_stream(x) * 10;
std::vector<double> translating = to_stream(x) + 3.7;

Set operations:

std::set<int> x = /* ... */;
set::set<int> y = /* ... */;

auto to_stream = [](std::set<int>& vec) {
    return MakeStream::from(vec);
};

std::set<int> set_union = to_stream(x) | union_with(to_stream(y));
// Better than:
//   std::set<int> result;
//   std::set_union(x.begin(), x.end(), y.begin(), y.end(),
//                  inserter(result, result.end()));
std::set<int> set_intersect = to_stream(x)
    | intersection_with(to_stream(y));
std::set<int> set_diff = to_stream(x)
    | difference_with(to_stream(y));
std::set<int> set_sym_diff = to_stream(x)
    | symmetric_difference_with(to_stream(y));

Adding unique ids:

std::vector<T> objects = /* ... */;

std::vector<T> objects_with_ids = MakeStream::from(objects)
    | zip_with(MakeStream::counter(1), [](T&& object, int id) {
        object.set_id(id);
        return object;
    });

Printing containers:

(MakeStream::from(container) | print_to(std::cout)) << std::endl;

Operator composition:

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

streams's People

Contributors

jscheiny avatar jiripospisil avatar

Watchers

Bharath avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.