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

add builtin functions : pow and switch #579

Closed
wants to merge 5 commits into from

Conversation

tinboxw
Copy link

@tinboxw tinboxw commented Feb 26, 2024

add builtin functions : pow and switch

@antonmedv
Copy link
Member

Please, elaborate why do we need pow() builtin? We already have x**y and x^y.

For switch() builtin we better implement some syntax. Maybe it will be better to do something like match.

@tinboxw
Copy link
Author

tinboxw commented Feb 29, 2024

Sorry, I overlooked that the pow(^/**) has already been implemented while reviewing the documentation. It seems unnecessary to implement pow again.
Regarding the switch() functionality, I hope to achieve a more concise and clear syntax to replace the chaining of ternary operators?:.
input data like:
{"distance": 100}
I hope to calculate a value based on the distance, but it seems that I can only achieve this functionality using the built-in ternary operator, for example:

(distance>300 || distance == 0)? -1
  :(distance > 250? 5
   :(distance > 200? 4
    :(distance > 150? 3
     :(distance > 100 ? 2
      :(distance > 50 ? 1
       :(distance > 0? 0)
       )
      )
     )
    )
   )

So, I implemented a switch to replace the ternary operator.

switch(distance > 300 || distance == 0, -1, 
       distance > 250, 5, 
       distance > 200, 4, 
       distance > 150, 3, 
       distance > 60,  2, 
       distance > 30,  1, 
       distance > 0,   0)

Because I'm not very familiar with the expr project, the implemented switch() might not be particularly user-friendly. I hope you can help optimize it.

@antonmedv
Copy link
Member

image I see the point!

But implementing it as function not the best solution. As it will require to evaluate all conditions and cases before choosing a correct one.

We need to create a proper switch syntax in Expr.

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

Successfully merging this pull request may close these issues.

2 participants