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

[New concept + exercise]: Case #1595

Merged
merged 8 commits into from
Nov 13, 2023
Merged

Conversation

meatball133
Copy link
Member

The goal of this concept is to dig deeper into case, and in a later pr remove case from conditionals and expand the conditionals concept with more info.

Copy link
Member

@kotp kotp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "same" example behaves differently than the other example.

"Case equality" is used pretty consistently over the years for the === method as used in a case statement.

@@ -0,0 +1,129 @@
# Case

[Case][case] (often referred to as switch in other languages) is a form of control expression like if-else.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[Case][case] (often referred to as switch in other languages) is a form of control expression like if-else.
[Case][case] (often referred to as switch in other languages) is a form of flow control expression like if-else.

# Case

[Case][case] (often referred to as switch in other languages) is a form of control expression like if-else.
Case allows for chaining of multiple if-else-if statements and can be more readable and also allows for powerful constructs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Case allows for chaining of multiple if-else-if statements and can be more readable and also allows for powerful constructs.
Case allows for chaining of multiple `when` statements and can be more readable and also allows for powerful constructs.

Also, does it really allow for "more powerful constructs"? Or are they just as powerful but different?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesnt really say they are more powerful just that they allow for powerful constructs.
And it is suppose to be if-else-if.

Copy link
Member

@kotp kotp Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see what it says, and yes, not "more" powerful but even "powerful"? It is comparing if to case statements, and suggests that case statements allow for powerful constructs, though if statements also do, so I am kind of "reading into" the "more" since it seems to be a comparison. But should it?

Copy link
Member

@kotp kotp Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, yes, while we can indeed use if-else-if along and even inside of case statements, the case statement does not itself use that syntax.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would even say that it is allowed for, even without case. So the case statement isn't what is allowing for chaining if-else-if statements.

Then for each case, the keyword `when` is used followed by an expression which is compared to the case expression.
The `when` keyword should not be indented from the `case` keyword.
After the `when` keyword is the code that should be executed if the case expression matches the when expression.
Case allows for an optional `else` statement which is executed if no other case matches.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Case allows for an optional `else` statement which is executed if no other case matches.
Case allows for an optional `else` statement which is executed if no other `when` matches.

Case allows for an optional `else` statement which is executed if no other case matches.

The case expression is evaluated and then compared to each `when` expression.
The expression is compared using the case subsumption operator (`===`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Ruby, we call that the "case equality operator".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have seen Jorg (from stack overflow) use it in 2010 and someone else call it this in their blog in 2022, but this is only the third time I have seen this === called that.

The expression is compared using the case subsumption operator (`===`).

```ruby
case 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case 1
case value

concepts/case/about.md Outdated Show resolved Hide resolved
concepts/case/introduction.md Show resolved Hide resolved
config.json Outdated Show resolved Hide resolved
@kotp kotp changed the title [New concept + exericse]: Case [New concept + exercise]: Case Nov 1, 2023
# Case

[Case][case] (often referred to as switch in other languages) is a form of control expression like if-else.
Case allows for chaining of multiple if-else-if statements and can be more readable and also allows for powerful constructs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Case allows for chaining of multiple if-else-if statements and can be more readable and also allows for powerful constructs.
Case allows for chaining of multiple if-else-if statements and can be more readable while still providing flow control.

How about that?

@meatball133 meatball133 merged commit 407f3fe into exercism:main Nov 13, 2023
4 checks passed
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