Last active
November 9, 2018 16:52
-
-
Save nikhedonia/67df7def9d944d858c5dcaf52b083357 to your computer and use it in GitHub Desktop.
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) | |
>> take(3); // 1/1, 2/1, 3/2 | |
auto ratiosWithPrecission = ratios() | |
>> zipWith( | |
[]{ return ratios() >> drop(1); }, | |
[](auto prev, auto next) { | |
return tuple{next, next - prev} ; | |
}) >> takeWhile([](auto x){ return abs(get<1>(x)) < 0.0001 }); | |
for (auto [ratio, delta]: ratiosWithPreccision) { | |
cout << ratio << " " << delta << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment