Created
October 31, 2018 18:45
-
-
Save nikhedonia/602af5f398f91ae5c1df4779ce905439 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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