for_each.h 625 B

1234567891011121314151617181920212223242526272829303132333435
  1. //
  2. // for_each.h
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 3/29/23.
  6. //
  7. #pragma once
  8. #define FWD(x) std::forward<decltype(x)>(x)
  9. namespace stream::ranges {
  10. template <typename S, typename F> void for_each(S && stream, F func) {
  11. for (auto && elem : FWD(stream)) {
  12. func(FWD(elem));
  13. }
  14. }
  15. }
  16. namespace stream::ranges::views {
  17. template <typename F> class for_each {
  18. private:
  19. F operation_;
  20. public:
  21. for_each(F operation) : operation_(operation) {}
  22. template <typename Stream>
  23. friend void operator|(Stream && stream, for_each const & each) {
  24. ranges::for_each(FWD(stream), each.operation_);
  25. }
  26. };
  27. }
  28. #undef FWD