On Tuesday, 4 April 2017 at 13:45:47 UTC, Meta wrote:
I mean what goes on inside fold. If you look at the C++ example it's very concise and IMO beautiful:

<typename ...Args> auto f(Args ...args) { return (0 + ... + args); }

It's special syntax for a very limited (only infix operators) and rather obscure use-case. There are many different ways to do this already, recursive calls, static foreach, string mixin, and they work with any reduce function/op.

It seems the reason C++ needs this, is because parameter pack expansion is so restricted (and complex at the same time http://en.cppreference.com/w/cpp/language/parameter_pack).

Compare this with a fully generic fold in a few lines.

  fold(alias op, Args...)(Args args) if (Args.length > 1)
  {
      static if (Args.length > 2)
          return op(args[0], fold!op(args[1 .. $]));
      else
          return op(args[0], args[1]);
  }

Reply via email to