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

create case(variable, expression_list) function #52

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

EduardoRSeifert
Copy link
Member

@EduardoRSeifert EduardoRSeifert commented Sep 25, 2024

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:

case("variable", "comparison1;result1", "comparison2;result2", "default;default value")

Using the @ token

Variable will be treated as a string, but can be considered a number when doing an operation in the comparison. example:

case("5", "5;results in simple string") // will test if the string "5" equals "5"
case("5", "@ < 6;results in simple string") // will test if the number 5 is smaller than 6

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

case("5", "@ < 6;@5*5") // will test if the number 5 is smaller than 6 and return 25
case("string variable", "string variable;@5*5") // will match strings and return 25

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 Tautology
case("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 case

Tests

image

@EduardoRSeifert EduardoRSeifert self-assigned this Sep 25, 2024
@EduardoRSeifert EduardoRSeifert changed the title create case(variable, expression_list) function WIP PLEASE DON'T MERGE create case(variable, expression_list) function Sep 29, 2024
@Marinofull Marinofull requested review from tiagogoncalves and removed request for Marinofull October 10, 2024 18:30
@Victorcorcos
Copy link
Contributor

Victorcorcos commented Oct 25, 2024

Hmmm, the @ operator is having two distinct responsibilities while being the same operator, right?

  1. @ < 6 → A replacement of the main value used as comparison in the case statement + Casting it to integer (at the same time).
  2. @5*5 → Casting the right value to integer.

Maybe if we break it into single responsibilities it would be easier to comprehend.

For example
@ → Casting the right value to integer.
$ → Replace of the main value used as comparison in the case statement

  1. @$ < 6
  2. @5*5

Even simpler approach

We can use the number() function that we already have to cast the result to a number

number(case("5", "@ < 6;5*5"))

@EduardoRSeifert
Copy link
Member Author

EduardoRSeifert commented Oct 25, 2024

Hmmm, the @ operator is having two distinct responsibilities while being the same operator, right?

  1. @ < 6 → A replacement of the main value used as comparison in the case statement + Casting it to integer (at the same time).
  2. @5*5 → Casting the right value to integer.

Maybe if we break it into single responsibilities it would be easier to comprehend.

For example @ → Casting the right value to integer. $ → Replace of the main value used as comparison in the case statement

  1. @$ < 6
  2. @5*5

Even simpler approach

We can use the number() function that we already have to cast the result to a number

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?

@Victorcorcos
Copy link
Contributor

@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

Copy link
Contributor

@Victorcorcos Victorcorcos left a 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 👍

Copy link
Contributor

@Victorcorcos Victorcorcos left a 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.

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