for_each.h 777 B

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