How might chain_apply()
be improved?
auto chain_apply(auto&& a, auto&& ...f)
{
auto const execute([&]<auto I>(auto&& self)
{
if constexpr(I)
{
return std::get<I>(
std::forward_as_tuple(f...))(
self.template operator()<I - 1>(self)
);
}
else
{
return std::apply(
std::get<0>(std::forward_as_tuple(f...)),
std::forward<decltype(a)>(a)
);
}
}
);
return execute.template operator()<sizeof...(f) - 1>(execute);
}
int main()
{
auto const inc([](auto const i) noexcept
{
return i + 1;
}
);
std::cout << chain_apply(std::tuple(0), inc, inc, inc, inc) << std::endl;
return 0;
}
EDIT:
No pipe operator this time, but consider:
std::tuple(0) | inc | inc | inc | inc;
void
andtuple<>
returns (tuple<>
returns are auto-applied to the next function in the chain). \$\endgroup\$