Skip to main content
added 100 characters in body
Source Link
user1095108
  • 1.4k
  • 2
  • 17
  • 29

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);
}

Usage:

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;

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);
}

Usage:

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;
}

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);
}

Usage:

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;
Source Link
user1095108
  • 1.4k
  • 2
  • 17
  • 29

functor chaining function for c++20

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);
}

Usage:

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;
}