for_each.h 757 B

12345678910111213141516171819202122232425262728293031
  1. //
  2. // for_each.h
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 3/29/23.
  6. //
  7. #pragma once
  8. #include <stream/detail/identity.h>
  9. #include <stream/detail/named_pair.h>
  10. #define FWD(x) std::forward<decltype(x)>(x)
  11. namespace stream::ranges {
  12. template <typename It, typename S, typename F, typename Proj = detail::identity>
  13. auto for_each(It it, S end, F func, Proj proj = {}) {
  14. for (; it != end; ++it) {
  15. std::invoke(func, std::invoke(proj, FWD(*it)));
  16. }
  17. return detail::in_fun_result(it, std::move(func));
  18. }
  19. template <typename Stream, typename F, typename Proj = detail::identity>
  20. auto for_each(Stream && stream, F func, Proj proj = {}) {
  21. return for_each(stream.begin(), stream.end(), std::move(func),
  22. std::move(proj));
  23. }
  24. }
  25. #undef FWD