Support for boolean ops (double &&, ||)
This is a JS library, after all :) Bites me almost every time I try to do an AND or OR
Yeah, I understand that it would be convenient to have logical operators && and ||, since most of us are used to these from JavaScript or other programming languages.
The expression parser though tries to offer a math friendly syntax in the first place, not perse a JavaScript compatible one, though being able to keep the syntax consistent with JavaScript and Matlab for example is definitely a pre. I think the current logical operators and and or are way better than && and || because they are way easier to understand. Adding aliases && and || would not add value and make the API larger (and thus more complex). I'm therefore not sure if it's a good idea to add them, but if there are enough people in for it we can certainly consider it.
I agree that the default syntax should be and and or, but it would be nice to be able to set aliases for operators and/or functions:
math.alias({
operators: {
and: '&&',
or: '||'
},
functions: {
log: 'ln',
log10: 'log'
}
})
e1 = math.parse('A && B')
e1.toString() // 'A && B'
e2 = math.parse('log(10)+ln(e)')
e2.eval() // 2
Making it possible to let people configure their own behavior is an interesting idea.
In general though I think that "less is more", and more ways to do the same thing complicates an API, so I'm a bit careful and reserved in that regard.
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away - Antoine de Saint-Exupery
:D
Hi, I know this post is a bit old, but I want to add my two cents. First of; great library! Regarding the operators and/or and ||/&&, I would ideally see the latter ones operate differently than the former ones. Namely in the sense that they short-circuit, meaning that the second expression is not evaluated if the first one suffices to determine the outcome (e.g. suppose one has a || b, when a evaluates to true there is no need to evaluate b).
Consider the example following example. I have a variable a that can be either undefined, an empty string or a number. Only in the latter case I want to perform operations on a. The only way (that I found) to determine if the variable isn't undefined and an empty string is by stringing two conditional expressions together:
a = undefined
// undefined
a == undefined or equalText(a,'')
// TypeError: Unexpected type of argument in function compareText (expected: string or Array or Matrix, actual: undefined, index: 0)
a == undefined? true: equalText(a,'')
// true
a== undefined || equalText(a,'')
// SyntaxError: Value expected (char 17)
Using a short circuit operator in such a case would make the expression a lot easier.
Namely in the sense that they short-circuit, meaning that the second expression is not evaluated if the first one suffices to determine the outcome (e.g. suppose one has a || b, when a evaluates to true there is no need to evaluate b).
Yes! That would would be very nice change/improvement. We already implemented this for the conditional operation (condition ? a : b). I think we should open a separate issue, and close this issue to keep it focused.
I agree that the default syntax should be
andandor, but it would be nice to be able to set aliases for operators and/or functions:
simple solution is just do string substitution
e1 = math.parse('A && B'.replaceAll('&&', 'and')