likebike / fasteval

Fast and safe evaluation of algebraic expressions
https://crates.io/crates/fasteval/
MIT License
267 stars 26 forks source link

Evaluate with strings and numbers #13

Closed thadev closed 3 years ago

thadev commented 3 years ago

Hi, thanks for the fast eval crate. is there a way to use numbers and strings as variables? Something like this: Evaluate: x > 1 && y == 'Y' ? where x will be a number and y a string.

likebike commented 3 years ago

Hello thadev,

Right now, the only type of value that can exist is f64.

The 'print' statement has special parsing logic that can also handle string values, but these strings can't exist outside of a 'print' call, and they can't participate in expressions (like comparisons or addition).

It is theoretically possible to add string values, but we would need to think about the following issues:

It is possible to avoid the above performance problems by having two different parse results: one "pure_f64" expression type that consists only of f64, and one "mixed" expression type that has strings or other non-f64 data types. The "pure" type would maintain all of the current high performance. The "mixed" would sacrifice performance to enable string values.

In the long run, I do want to support more data types (like integer, rational, complex, etc) and it makes sense to include strings in this list. The above list of "things to think about" will need to be done for each different type that is added -- strings are no worse than any other type.

thadev commented 3 years ago

Hello likebike,

The performance is impressive. I would go for the solution with the two parsers - one with f64 only and one with mixed types OR at least one parser for each type.

Personally, I would use the mixed types parser for simple conditions and the f64 parser for calculation expressions. f64 is pretty versatile when it comes to calculations, so I don't miss the other types at all (integers etc.), but I definitely miss string for conditions that other crates have. So I have to stick with one of the others for the time being (at least for conditions, and I hope you will add string soon.

Thanks

likebike commented 3 years ago

Thanks for the feedback.