JHT
JHT

Reputation: 143

iteratively create lambda expression

Is there a way to iteratively create a lambda expression representing for instance a sum of many functions

interface lambda{
   double apply(double x);
}
lambda func = x -> 0;

for(i=0; i < 100; i++){
   func = x -> func.apply(x) + i*x;
}

seems not to work?

Upvotes: 2

Views: 67

Answers (1)

Crammeur
Crammeur

Reputation: 688

Try this

...

lambda func = x -> 0;

for(int i=0; i < 100; i++){
    final int index = i;
    final lambda finalFunc = func;
    func = x -> finalFunc.apply(x) + index*x;
}

Upvotes: 3

Related Questions