-
Notifications
You must be signed in to change notification settings - Fork 163
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
Added the chance to declare Functions with variable number of arguments #42
base: master
Are you sure you want to change the base?
Conversation
Hola @MFrancesco |
Ok, i'll write some tests to check that. |
@MFrancesco: |
Hey @MFrancesco, public int getNumArgument() {
if (minArguments != maxArguments) {
throw new UnsupportedOperationException("Calling getNumArgument() is not supported for var arg functions, please use getMaxNumArguments() or getMinNumArguments()");
}
return minArguments;
} @MFrancesco, @sarxos, @leogtzr, @dktcoding: What do you guys think? |
It's ok for me. My mistake for not considering this aspect.
|
Hi @fasseg, I like this idea much better than doing refactor to call both |
…k existing user code
I scheduled some time next week to roll a new release and will probably merge this into master. |
Sorry, any example of how to implement custom functions like sum() prod() or avg() ? I see some code but it is for testing: https://github.com/fasseg/exp4j/blob/master/src/test/java/net/objecthunter/exp4j/ExpressionBuilderTest.java |
Here is example of a very simple /**
* Logical "if" function.
*/
public static final Function IF = new Function("if", 3) { // named "if", takes 3 arguments
@Override
public double apply(double... args) {
validate(this, args);
if (args[0] == 1) {
return args[1];
} else {
return args[2];
}
}
}; Then I bind it with expression in this way: String expression = "if (x > sin(2 * y), 1, 0)"; // sample expression
Expression exp = new ExpressionBuilder(expression)
.variable("x")
.variable("y")
.operator(NOT, EQ, NEQ, GT, LT, GTEQ, LTEQ) // some operators I have defined
.function(IF)
.build(); The operator example: /**
* Greater than operator.
*/
protected static final Operator GT = new Operator(">", 2, true, Operator.PRECEDENCE_ADDITION - 1) {
@Override
public double apply(double... values) {
validate(this, values);
return values[0] > values[1] ? 1d : 0d;
}
}; |
Thank you very much @sarxos, I have a problem with variable number of arguments.... can you check this?
It works for one argument but If push two then I get this error: java.lang.IllegalArgumentException: Invalid number of items on the output queue. Might be caused by an invalid number of arguments for a function. Thanks again ...and happy new year! |
What is the current status for this feature? It would be super nice to have it. Can I try and take it up? As far as I understand one of the issues is that this PR changes the public API. Possible solutions:
Which one would be preferred? |
From inspecting the code, it seems like they went with solution 2. I would however like to see some more tests with more complicated nested expressions. Other than that, the PR seems solid. |
Hey Patrick, Sorry to inform you that exp4j is no longer actively developed. I don't have the time to maintain the project further and I have not yet found a dependable person to take over as a maintainer. :( |
Using this library we felt the need to add the chance to declare Functions with variable number arguments such as avg, min, max.
The feature has been tested in FunctionsWithVariableArgsTest and require few changes.