Skip to content

Instantly share code, notes, and snippets.

@nikhedonia
Last active November 9, 2018 16:52
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/67df7def9d944d858c5dcaf52b083357 to your computer and use it in GitHub Desktop.
Save nikhedonia/67df7def9d944d858c5dcaf52b083357 to your computer and use it in GitHub Desktop.
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