Skip to content

Instantly share code, notes, and snippets.

@nikhedonia
Created October 31, 2018 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikhedonia/602af5f398f91ae5c1df4779ce905439 to your computer and use it in GitHub Desktop.
Save nikhedonia/602af5f398f91ae5c1df4779ce905439 to your computer and use it in GitHub Desktop.
// Sum of the first 10 squares
auto total = squares() >> take(10) >> sum();
// Jumps between the first 10 squares
auto jumps = squares()
>> scan(0, [](auto x, auto y) { return y - x; })
>> take(10);
// The obligatory fibonacci
auto fibonacci = []() -> seq<int> {
int a = 0;
int b = 1;
while ( true ) {
co_yield a;
tie(a, b) = tuple{a + b, a};
}
};
// Cumulative sum of 6 pseduo-random dice rolls
auto randoms = [](int seed) -> seq<double> {
// pseudo-random
std::mt19937 engine(seed);
std::uniform_real_distribution<> dist;
while (true) {
co_yield dist(engine);
}
};
auto rolls = randoms(0)
>> map([](auto x) { return ceil(x * 6); })
>> scan(0, [](auto x, auto y) { return x + y; })
>> take(6);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment