On Wednesday, 4 December 2024 at 08:50:21 UTC, axricard wrote:
Hello

I believe std.functional.partial can only apply to the first argument.
Is there an equivalent for the Nth argument ?

Something like :

``` D
int fun(int a, int b) { return a - b; }

// create a function with the argument n°1 being equal to 5
// fun5 = fun(a, 5);
alias fun5 = partialN!(fun, 1, 5);
writeln(fun5(7)); // print 2
```

Why does 'alias' take 3 parameters? Maybe I'm misunderstanding, but this is what you want:

```d
template partialN(alias func, int idx, args...)
{
  enum error = "Missing argument!";
  static assert(idx < args.length, error);

  auto partialN(T)(T first)
  {
    return func(first, args[idx]);
  }
} unittest {
  auto sum = (int a, int b) => a + b;

  alias sum11= partialN!(sum, 1, 10, 11, 12);
  assert(sum11(9) == 20);

  alias sum12= partialN!(sum, 2, 10, 11, 12);
  assert(sum12(9) == 21);

  auto and = (bool a, bool b) => a & b;
  alias and11= partialN!(and, 1, false, true, false);
  assert(and11(true));
}
```

SDB@79

Reply via email to