How to reuse functions

Luigi via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 1 18:40:43 PDT 2015


On Friday, 1 May 2015 at 11:03:19 UTC, anonymous wrote:
> If x_n is a constant (enum), you can use that to create a 
> function instead of a delegate:
>
> ----
> void main() {
>     enum real x_n = 1;
>     real x_m = -1;
>     real function(real) s_n = z => F1(z, x_n);
>     auto J = jac(s_n, x_m);
> }
> ----
>
> If x_n is not constant, then the only way I see to make this 
> work, is to use a module variable:
>
> ----
> real x_n;
> void main() {
>     x_n = 1;
>     real x_m = -1;
>     real function(real) s_n = z => F1(z, x_n);
>     auto J = jac(s_n, x_m);
> }
> ----

On Friday, 1 May 2015 at 06:48:46 UTC, John Colvin wrote:
> A combination of std.functional.reverseArgs and 
> std.functional.partial might help. It's unfortunately we don't 
> have a version that can set an arbitrary argument, only the 
> first one; It would be an easy improvement to make.

Thank you both. I understood what you have shown to me.
About the chain 'reverseArg'+'partial' I reached to use it 
partially, only the partial part; so it worked with exception of 
reverseArg. I made several attempt to make it working but without 
succes.

About the scope/declaration suggestion they worked, fine. I've 
also made a change declaring x_s as static and worked as well 
(without change of scope and without use of enum).

Unfortunatly I tried to exted the code by introducing array of 
functions F1->[F1, F2, ...] and I saw such solution cannot apply. 
That why because of built functions passed (as example) to lambda 
function they do not store information. And I went back to 
delegates that store the necessary data to create the correct 
function. I've seen some method to pass from delegates to 
function, but onestly I found a to complex approach and much more 
a workaround that a good programming.
Going back to my study of D I will find the right way of thinking 
to approch such problems (I imagine now the trick could be using 
templates... I will see).

Thanks again. I've learned many things by looking at you solution.


More information about the Digitalmars-d-learn mailing list