This repository was archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 177
Simplify pattern match for patch_preflight
#1671
Merged
notriddle
merged 1 commit into
bors-ng:master
from
MercuryTechnologies:gabriella/simplify_pattern_match
Jun 1, 2023
Merged
Simplify pattern match for patch_preflight
#1671
notriddle
merged 1 commit into
bors-ng:master
from
MercuryTechnologies:gabriella/simplify_pattern_match
Jun 1, 2023
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This one is easier to explain if you format the pattern match a bit: ```elixir case { passed_label, no_error_status, no_waiting_status, no_unset_status, passed_review, code_owners_approved, passed_up_to_date_review} do { true , true , true , true , :sufficient , true , :sufficient } -> {:ok, toml.max_batch_size} { false , _ , _ , _ , _ , _ , _ } -> {:error, :blocked_labels} { _ , _ , _ , _ , :insufficient, _ , _ } -> {:error, :insufficient_approvals} { _ , _ , _ , _ , :failed , _ , _ } -> {:error, :blocked_review} { _ , _ , _ , _ , _ , false , _ } -> {:error, :missing_code_owner_approval} { _ , false , _ , _ , _ , _ , _ } -> {:error, :pr_status} { true , true , false , _ , :sufficient , true , _ } -> :waiting { true , true , _ , false , :sufficient , true , _ } -> :waiting { _ , _ , _ , _ , :sufficient , _ , :insufficient } -> {:error, :insufficient_up_to_date_approvals} end ``` Notice how after the second case of the pattern match the first value must necessarily be `true`, so you can convert those to wildcard pattern matches: ```elixir case { passed_label, no_error_status, no_waiting_status, no_unset_status, passed_review, code_owners_approved, passed_up_to_date_review} do { true , true , true , true , :sufficient , true , :sufficient } -> {:ok, toml.max_batch_size} { false , _ , _ , _ , _ , _ , _ } -> {:error, :blocked_labels} { _ , _ , _ , _ , :insufficient, _ , _ } -> {:error, :insufficient_approvals} { _ , _ , _ , _ , :failed , _ , _ } -> {:error, :blocked_review} { _ , _ , _ , _ , _ , false , _ } -> {:error, :missing_code_owner_approval} { _ , false , _ , _ , _ , _ , _ } -> {:error, :pr_status} { _ , true , false , _ , :sufficient , true , _ } -> :waiting { _ , true , _ , false , :sufficient , true , _ } -> :waiting { _ , _ , _ , _ , :sufficient , _ , :insufficient } -> {:error, :insufficient_up_to_date_approvals} end # ↑ s/true/_/ after the first false ``` If you keep repeating this process (of replacing pattern matches that are guaranteed to succeed with wildcards) then you get this sequence of pattern matches: ```elixir case { passed_label, no_error_status, no_waiting_status, no_unset_status, passed_review, code_owners_approved, passed_up_to_date_review} do { true , true , true , true , :sufficient , true , :sufficient } -> {:ok, toml.max_batch_size} { false , _ , _ , _ , _ , _ , _ } -> {:error, :blocked_labels} { _ , _ , _ , _ , :insufficient, _ , _ } -> {:error, :insufficient_approvals} { _ , _ , _ , _ , :failed , _ , _ } -> {:error, :blocked_review} { _ , _ , _ , _ , _ , false , _ } -> {:error, :missing_code_owner_approval} { _ , false , _ , _ , _ , _ , _ } -> {:error, :pr_status} { _ , _ , false , _ , _ , _ , _ } -> :waiting { _ , _ , _ , false , _ , _ , _ } -> :waiting { _ , _ , _ , _ , _ , _ , :insufficient } -> {:error, :insufficient_up_to_date_approvals} end ``` … which is clearer (IMO) because now it basically reads as: - either all conditions pass or … - one of the conditions individually fails … which makes it more clear that this sequence of pattern matches is exhaustive.
notriddle
approved these changes
Jun 1, 2023
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This one is easier to explain if you format the pattern match a bit:
Notice how after the second case of the pattern match the first value must necessarily be
true
, so you can convert those to wildcard pattern matches:If you keep repeating this process (of replacing pattern matches that are guaranteed to succeed with wildcards) then you get this sequence of pattern matches:
… which is clearer (IMO) because now it basically reads as:
… which makes it more clear that this sequence of pattern matches is exhaustive.