fibonacci.cpp 866 B

123456789101112131415161718192021222324252627282930
  1. // Copyright Oliver Kowalke 2014.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <boost/coroutine2/all.hpp>
  8. int main() {
  9. boost::coroutines2::coroutine< int >::pull_type source(
  10. []( boost::coroutines2::coroutine< int >::push_type & sink) {
  11. int first = 1, second = 1;
  12. sink( first);
  13. sink( second);
  14. for ( int i = 0; i < 8; ++i) {
  15. int third = first + second;
  16. first = second;
  17. second = third;
  18. sink( third);
  19. }
  20. });
  21. for ( auto i : source) {
  22. std::cout << i << " ";
  23. }
  24. std::cout << "\nDone" << std::endl;
  25. return EXIT_SUCCESS;
  26. }