-
Notifications
You must be signed in to change notification settings - Fork 8
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
create case(variable, expression_list) function #52
base: master
Are you sure you want to change the base?
Conversation
Hmmm, the
Maybe if we break it into single responsibilities it would be easier to comprehend. For example
Even simpler approachWe can use the number(case("5", "@ < 6;5*5")) |
You misunderstood. @ means 2 different things, yes, but it always means the same thing within it's argument. I used the same token to avoid creating a new one @ in the comparison means "substitute @ for the variable and evaluate the resulting equation", while @ in the return value means "evaluate the return value as an equation". So case("blabla.blabla", "@ > 5;@10", "default;@8") will result in "blabla.blabla > 5 ? 10 : 8". But don't mistake @10 as cating to integer, it means "evaluate 10 as an equation", but ot RESULTS in the same as casting to integer. Hope that made it clearer. Yes, you could use case("blabla.blabla", "@ > 5;@Number(10)", "default;@Number(8)") to be more explicit that you want a number return, but is that necessary? |
@EduardoRSeifert Got it! case("5", "@ < 6;@5*5", "default;@666") Means case 5
when < 6
5 * 5
else
666
end case("edimar", "xablau;nao", "edimar;sim") Means case "edimar"
when "xablau"
"nao"
when "edimar"
"sim"
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are conflicts in tests.sh
now 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very interesting and useful function!
I'm just waiting for @niltonvasques approval here.
This PR creates the case function.
It receives a variable as the first parameter, then a list of comparisons and results as the other parameters
Syntax
The list should be in this syntax:
Using the @ token
Variable will be treated as a string, but can be considered a number when doing an operation in the comparison. example:
To use the variable in a comparison operation, we use the @ token. It is also used in the result to evaluate the result instead of returning a string
Default value
It is NOT strictly needed, but should be excluded carefully. Good example of removing it:
case("5", "@<4;do this", "@>=4;do that")
since the combination of the comparisons is a Tautologycase("5", "@<5;do this", "@>5;do that")
herein lies the problem: 5 is neither < 5 nor > 5. Make sure there is a Tautology in the comparisons if the default value is not added (the parser will throw an error if no comparisons are satisfied and there isn't a default)case("5", "@<5;do this", "@>5;do that", "default;something wrong happened")
is the safest way to construct the caseTests