How to reuse functions

Luigi via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Apr 30 20:34:52 PDT 2015


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.

Thaks in advance to anyone could help me.
Luigi


More information about the Digitalmars-d-learn mailing list