On Friday, 1 May 2015 at 03:34:53 UTC, Luigi wrote:
Hi everybody.

I am tring to use a function where its parameter is another function, and at the same time are both already made - they cannot be modified - and the second one has to be conditioned before to be passed as argument.

Let's say I have these function and I cannot modify:

-jac(+d) that works on function and return real

real jac(real function(real) fun, real x) {return d(fun, x);}
real d(real function(real) fun, real x) {return fun(x);}

-F1 that works on two reals and return real

real F1(real a, real b) {return a + b;}

So I need a way to condition F1 fixing b and then pass it to jac as argument.

To do that I've created a delegate 'simp' conditionig F1:

real delegate(real) simp(real function(real, real) f, real x) {
        real _simp(real z) {
                return f(z, x);
                }
        return &_simp;
        }
(here 'simp' fixes b at x).

My main is:

void main() {
        real x_n = 1;
        real x_m = -1;
        real delegate(real) s_n = simp(&F1, x_n);
//auto J = jac(s_n, x_m); //Error: function app.jac (real function(real) fun, real x) is not callable using argument types (real delegate(real), real)
        }

the code fails because jac expect as argument a function but I found only a delegate to obtain a simplified function without any touch at already made functions jac, d and F1.

There is a clean way to make it possible? I mean: without to touch (neither rewrite) jac, d and F1, obtain simplied F1 to pass to jac.

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

Reply via email to