digitalmars.D.learn - Partial application on Nth argument
- axricard (12/12) Dec 04 2024 Hello
- monkyyy (16/28) Dec 04 2024 nothin Im aware of in the std but I think this is trivail
- Salih Dincer (24/36) Dec 04 2024 Why does 'alias' take 3 parameters? Maybe I'm misunderstanding,
- Salih Dincer (36/37) Dec 05 2024 Now I get it! In fact, we bind a chosen argument. This reveals
- monkyyy (2/3) Dec 05 2024 unnecessary, check mine
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 ```
Dec 04 2024
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 ```nothin Im aware of in the std but I think this is trivail ```d import std; template partialN(alias F,int arg,argvalue...){ auto partialN(T...)(T otherargs)=>F(otherargs[0..arg],argvalue[0],otherargs[arg..$]); } unittest{ 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)); } ```
Dec 04 2024
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
Dec 04 2024
On Wednesday, 4 December 2024 at 21:33:46 UTC, Salih Dincer wrote:Maybe I'm misunderstanding...Now I get it! In fact, we bind a chosen argument. This reveals the inadequacy of the name chosen for the library. I've made it better by adding a few lines of code: ```d auto hello(T)(T a, T b, T c, T d, T e) => a~b~c~d~e; enum msg { h = "h", e = "e", l = "l", o = "o" } void main() { with(msg) { assert(bindArgument!(hello, 0, h)(e,l,l,o) == "hello"); assert(bindArgument!(hello, 1, e)(h,l,l,o) == "hello"); assert(bindArgument!(hello, 2, l)(h,e,l,o) == "hello"); assert(bindArgument!(hello, 3, l)(h,e,l,o) == "hello"); assert(bindArgument!(hello, 4, o)(h,e,l,l) == "hello"); alias Fun = bindArgument!(hello, 5, e); //Fun(h, l, l, o).writeln; //error } } template bindArgument(alias func, size_t N, fixedArgs...) { static auto bindArgument(A...)(A argsRest) if(N <= argsRest.length) { static if (N == 0) return func(fixedArgs[0], argsRest); else return func(argsRest[0 .. N], fixedArgs[0], argsRest[N .. $]); } } ``` SDB 79
Dec 05 2024
On Thursday, 5 December 2024 at 21:14:32 UTC, Salih Dincer wrote:static ifunnecessary, check mine
Dec 05 2024