This file contains hidden or 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
auto ratios = []{ | |
return fib() >> scan(1, [](auto prev, auto next){ | |
return next / prev; | |
}); | |
}; | |
int main() { | |
auto fiveFibs = fib() >> take(5); // 0, 1, 1, 2, 3 | |
auto ratiosTenToTwenty = ratios() | |
>> drop(1) |
This file contains hidden or 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
int main () { | |
int n=100; | |
auto prevRatio = 0.0; | |
for (auto ratio: fibRatios()) { | |
cout << ratio << endl; | |
auto delta = ratio - prevRatio; | |
if (n==0 || delta < 0.0001) | |
break; | |
prevRatio = ratio; | |
n- - ; |
This file contains hidden or 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
auto fib() -> seq<int> { | |
int a = 0; | |
int b = 1; | |
while (1) { | |
co_yield a; | |
tie(a, b) = tuple{b, a+b}; | |
} | |
} |
This file contains hidden or 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
template<class TestF class Map> | |
auto golden(TestF test, Map f) { | |
int a = 0; | |
int b = 1; | |
double ratio = 1; | |
while (1) { | |
tie(a, b) = tuple{b, a+b}; | |
auto delta = b/(double)a - ratio; | |
ratio = b/(double)a; |
This file contains hidden or 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
double golden(double epsilon=0.00001) { | |
int a = 0; | |
int b = 1; | |
double ratio = 0; | |
while (1) { | |
tie(a, b) = tuple{b, a+b}; | |
auto delta = b / (double)a - ratio; | |
ratio = b / (double)a; | |
if ( abs(delta) < epsilon ) |
This file contains hidden or 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
def merge_dicts(x, y): | |
z = x.copy() | |
z.update(y) | |
return z | |
cxx_library( | |
name = "python3.8m", | |
header_namespace= '', | |
compiler_flags = ["-fwrapv"], |
This file contains hidden or 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
#include <type_traits> | |
namespace conduit { | |
template <class T> | |
T id(T const& x) { | |
return x; | |
} | |
template <class T> | |
auto first(T&& xs) -> decltype(id(*xs.begin())) { | |
return *xs.begin(); | |
} |
This file contains hidden or 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
auto fibonacci = []() -> seq<unsigned long long> { | |
auto a = 0ll; | |
auto b = 1ll; | |
while (true) { | |
co_yield a; | |
tie(a, b) = tuple{a + b, a}; | |
} | |
}; |
This file contains hidden or 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> { |
This file contains hidden or 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
std::vector<int> v = range() | |
>> take(5) | |
>> toVector(); | |
// v = { 0, 1, 2, 3, 4 } |