Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we pass min and max number of arguments in Function constructor ?? #95

Open
Rabi-Narayan opened this issue Jun 27, 2018 · 1 comment

Comments

@Rabi-Narayan
Copy link

We are getting Expressions dynamically and we have no idea how many of arguments there will be.So how can we achieve this.Can somebody guide us.Thanks in advance.

Ex :- Expression e = new ExpressionBuilder("sum(1,avg(1,2,3))")
.functions(variableArgsFunctions)
.build();

"sum(1,avg(1,2,3))" expression we will get dynamically.In some cases it may be sum(1,avg(1,2,3,4)).

How can we achieve this.

@subramaniant
Copy link

Hi Rabi Narayan,

Go to -> net.objecthunter.exp4j.Expression.evaluate() --> replace or modify as below (removing check for number of arugment...Perfect way is to add one function without num argument ) ..
....
} else if (t.getType() == Token.TOKEN_FUNCTION) {
FunctionToken func = (FunctionToken) t;
final int numArguments = func.getFunction().getNumArguments();
// if (output.size() < numArguments) {
// throw new IllegalArgumentException("Invalid number of arguments available for '" + func.getFunction().getName() + "' function");
// }
/* collect the arguments from the stack */
double[] args = new double[output.size()];
for (int j = numArguments - 1; j >= 0; j--) {
args[j] = output.pop();
}
output.push(func.getFunction().apply(args));
}
...

Please let me know , if you have any issue on this..

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants