-
Notifications
You must be signed in to change notification settings - Fork 20
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
Support for math operators in expressions #419
Comments
(I'm happy to defer this until after we've sorted out our function typing issues.) |
Well ... this might be useful indeed. But implementing arithmethics and specifying it in a clean way are two very different shoes. We might deal then with:
Alternatively, I can imagine, that a function similar to CSS calc would be easy to implement and easier to specify. |
Yes, first class support for mathematical operators will entail a lot of spec work. Function extensions could be used instead. I suggest we defer this issue and tag it "revisit-after-base-done". |
The comparison indicates that many implementations support a path like
I wonder how adding in a couple spaces would do: |
|
Yeah, it's understood that those implementations aren't spec-compliant.
Yeah, it's understood that we'd have to do that stuff. I don't think we should shy away from it, though. I still think this is within our charter. |
Personal bias here but I've found simple math operators ( The one limitation I've had users question is why a |
We currently forbid |
Deferring until after base done. |
Follow up of #449: Take the following arithmetic example: Using a set of small (binary) functions results in the query
whereas using a
I predict, most users will prefer the latter syntax. We need here a function
The string argument must contain a pure arithmetic expression, that means
When Greg says regarding inline arithmetic:
Then implementing the Then there is another charming aspect of this approach. Imagine the following scenario: A user is supplying a set of parts of simple geometry, holding the part-descriptions in a JSON array. Each part description is redundancy-free and holds geometric and material properties. The part mass might be a measure of the selling price. So if we want to find all cuboids with a mass less than 20 (kg), we can start the query
where In case we know - as the JSON author - that the part mass is frequently requested, we can even put into the header section of the JSON data
also the arithmetic expressions for other part masses. This way we can reformulate the query above to
which of course then requires the useful Apart from that, having simple strings holding arithmetic expressions allows us to store them in JSON for reuse in the same way, as we can do it with JSONPath queries or preferrably with normalized pathes as strings. That you cannot do conceptually with the barely readable
... no, due to strong encapsulation and sharp restricted syntax explained above.
I don't see this, please elaborate.
... again no, due to strong encapsulation and sharp restricted syntax of pure arithmetic expressions, implementation should be easy, as Greg already mentioned above. Stefan |
If we are considering the ease of use for the end user I would think It shouldn't really matter how hard it is to implement if it is better for the end users. Anyone undertaking the task of implement the spec will have to be competent anyway so a little more work shouldn't be that large a hurdle. (IMHO) |
@ohler55 ... I do understand this very well from a user's point of view. But on the way there will be a lot of spec work to be done. So we are discussing here a way, how functions - in which form - can help to add arithmetic expressions to queries, while having sufficient user acceptance. I would applaud if some implementers gain experience meanwhile by implementing side by side
Then they can help to identify edge cases, type collisions and handling of numeric anomalies. |
But you don't need a
and query
the result is
That is, it's very straight forward if Daniel |
I took the approach described by @danielaparker in OjG but there is no reason all three of the proposed approaches could not be implemented. Having said that, picking one approach as the minimum and offering the others are extensions might be a way to resolve this. |
I agree with @danielaparker and @ohler55: these operators need to be supported in general expressions, not merely inside some function.
I don't see how the level of effort for supporting them in a function is any less than to support them in general expressions. If anything I think it's more effort because you have to explain why this syntax is valid only inside of this function.
From a parsing perspective, this is much more complicated than
From a user perspective, Regarding the "expressions in data" concept, we don't currently support data specifying a path anywhere, and doing so opens a whole new can of worms that we'd need to consider. It's paving the way for an
No one is advocating for this approach. Sure |
Hmm ... as an outcome of this discussion the realisation matures, that inline arithmetic develops as a de-facto standard in current implementations, which is also the natural thing, users expect. It seems to be best, to defer activities into that direction until after base done, which in fact was the reason, why Glyn closed this issue. |
I came up with this for basic math support: math-expr = binary-math-expr / unary-math-expr
binary-math-expr = math-operand binary-math-operator math-operand
unary-math-expr = unary-math-operator (number / singular-query / value-function-expr / math-group)
math-operand = number / singular-query / value-function-expr / math-expr / math-group
math-group = "(" math-expr ")"
binary-math-operator = "+" / "-" / "*" / "/"
unary-math-operator = "-" We'd then add comparable = literal / singular-query / value-function-expr / math-expr I believe this gives support for addition, subtraction, multiplication, division, and grouping, though it doesn't give operator precedence as yet (I'm working on that). It does allow multiple negations (e.g.
In the end, I'm not sure it makes much of a difference; maybe it saves an operation to have it as "negative 4." Given the outcome is the same, maybe we just let implementations decide how they want to handle it. It also doesn't prevent division by zero, but we'd have to contend with a path or a function returning zero anyway. I think the Similarly any path or This doesn't support string concatenation (yet). |
Does the ABNF need to give operator precedence?
|
The principle of least surprise says yes: Implementers will expect the AST they derive from the ABNF to be directly useful for a tree interpreter. |
I think mathematical operators would be a beneficial addition (😏) to the expression syntax. It would allow things like
to check for model consistency. (Arguably, you just wouldn't serialize
c
as it should be a calculated field, but people do stranger things.)There are doubtless other use cases.
I have support for this currently in my library. It's really easy to implement, and I don't think it would be too hard to specify.
I think this is within our charter as @goessner's original implementations supported "underlying scripting language" for expressions, which undoubtedly supported these operators.
The text was updated successfully, but these errors were encountered: