diff --git a/include/utempl/utils.hpp b/include/utempl/utils.hpp index d4e9c2a..74adb92 100644 --- a/include/utempl/utils.hpp +++ b/include/utempl/utils.hpp @@ -162,19 +162,6 @@ template concept TupleLike = kForceEnableTuple> || (requires{Get<0>(MakeTuple(42));} && impl::IsSafeTuple>::value); -template -inline constexpr auto Transform(Tuple&& container, F&& f, TypeList = {}) { - return [&](std::index_sequence){ - return MakeTuple(f(Get(std::forward(container)))...); - }(std::make_index_sequence>()); -}; - -template -inline constexpr auto Map(Tuple&& tuple, F&& f, TypeList result = {}) { - return Transform(std::forward(tuple), std::forward(f), result); -}; - - template inline constexpr auto Unpack(Tuple&& tuple, F&& f) -> decltype(auto) { return [&](std::index_sequence) -> decltype(auto) { @@ -183,6 +170,20 @@ inline constexpr auto Unpack(Tuple&& tuple, F&& f) -> decltype(auto) { }; + +template +inline constexpr auto Transform(Tuple&& container, F&& f, TypeList = {}) { + return Unpack(std::forward(container), + [&](Ts&&... args){ + return MakeTuple(f(std::forward(args))...); + }); +}; + +template +inline constexpr auto Map(Tuple&& tuple, F&& f, TypeList result = {}) { + return Transform(std::forward(tuple), std::forward(f), result); +}; + template inline constexpr auto Reverse(Tuple&& tuple) { return [&](std::index_sequence) { @@ -218,13 +219,13 @@ struct LeftFold { template inline constexpr auto LeftFold(Tuple&& tuple, T&& init, F&& f) { - return [&](std::index_sequence){ + return Unpack(std::forward(tuple), [&](Ts&&... args){ return ( impl::LeftFold, std::remove_cvref_t>{.data = std::forward(init), .f = std::forward(f)} | ... - | impl::LeftFold(std::forward(tuple)))>>{.data = Get(std::forward(tuple))} + | impl::LeftFold>{.data = std::forward(args)} ).data; - }(std::make_index_sequence>()); + }); }; @@ -245,6 +246,13 @@ inline constexpr auto TupleCat(Tuples&&... tuples) requires (sizeof...(tuples) > return TupleCat(std::forward(tup), std::forward(tup2)); }); }; + +template +inline constexpr auto Unpack(Tuples&&... tuples, F&& f) { + return Unpack(TupleCat(std::forward(tuples)...), std::forward(f)); +}; + + template inline constexpr auto Tie(Ts&... args) -> Tuple { return {args...}; @@ -302,9 +310,9 @@ inline constexpr auto Filter(Tuple&& tuple, auto&& f) { template inline constexpr auto ForEach(Tuple&& tuple, auto&& f) { - [&](std::index_sequence){ - (f(Get(std::forward(tuple))), ...); - }(std::make_index_sequence>()); + Unpack(std::forward(tuple), [&](Ts&&... args){ + (f(std::forward(args)), ...); + }); }; @@ -329,7 +337,7 @@ inline constexpr auto Curry(F&& f) -> Curryer> { template inline constexpr auto Find(Tuple&& tuple, T&& find) -> std::size_t { - return [&](std::index_sequence) -> std::size_t { + return Unpack(std::forward(tuple), [&](Ts&&... args) { using Type = std::remove_cvref_t; std::array> bs{Overloaded( [&](const Type& element) { @@ -347,8 +355,8 @@ inline constexpr auto Find(Tuple&& tuple, T&& find) -> std::size_t { [](auto&&) { return false; } - )(Get(std::forward(tuple)))...}; + )(std::forward(args))...}; return std::ranges::find(bs, true) - bs.begin(); - }(std::make_index_sequence>()); + }); }; } // namespace utempl