Skip to content

Commit a31f1d3

Browse files
Control Structs: Add pattern matching cases, break/continue, and with/using and defer
1 parent 5b3c924 commit a31f1d3

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

web/thesauruses/_meta/control_structures.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"if_elseif_conditional": "If/ElseIf conditional",
1111
"if_elseif_else_conditional": "If/ElseIf/Else conditional",
1212
"switch_statement": "Switch statement",
13+
"pattern_matching": "Pattern matching",
1314
"ternary_conditional": "Ternary conditional"
1415
},
1516
"Loops": {
@@ -19,13 +20,19 @@
1920
"do_until_loop": "Do/Until loop",
2021
"for_loop": "For loop",
2122
"foreach_loop": "Foreach loop",
23+
"break": "Breaking out of a loop",
24+
"continue": "Continuing to the next iteration of a loop",
2225
"list_comprehension": "List Comprehension"
2326
},
2427
"Iterations": {
2528
"each_iteration": "Each iteration",
2629
"map_iteration": "Map iteration",
2730
"filter_iteration": "Filter iteration",
2831
"fold_iteration": "Fold iteration"
32+
},
33+
"Resource Management": {
34+
"context_manager": "Context manager or with/using statement",
35+
"defer": "Defer statement"
2936
}
3037
}
3138
}

web/thesauruses/kotlin/1.5/control_structures.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
},
2929
"switch_statement": {
3030
"name": "Switch statement",
31-
"code": "when (expression) {\n value1 -> {\n // statements\n }\n value2 -> {\n // statements\n }\n else -> {\n // statements\n }\n}"
31+
"code": "when (x) {\n 1 -> print(\"x == 1\")\n 2 -> print(\"x == 2\")\n else -> {\n print(\"x is neither 1 nor 2\")\n }\n}"
32+
},
33+
"pattern_matching": {
34+
"name": "Pattern matching",
35+
"comment": "Kotlin's `when` expression supports advanced pattern matching like type checks and range checks.",
36+
"code": "when (obj) {\n 1 -> \"One\"\n \"Hello\" -> \"Greeting\"\n is Long -> \"Long\"\n !is String -> \"Not a string\"\n else -> \"Unknown\"\n}"
3237
},
3338
"while_loop": {
3439
"name": "While loop",

web/thesauruses/python/3/control_structures.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@
2323
"name": "If/ElseIf/Else conditional"
2424
},
2525
"switch_statement": {
26-
"comment": "This control structure was introduced with Python 3.10 and is more powerful than shown here.",
27-
"code": "match condition:\n case value1:\n statements\n case value2:\n statements",
26+
"comment": "In Python 3.10+, the `match` statement is used for both switch-like behavior and advanced pattern matching.",
27+
"code": "match variable:\n case value1:\n # statements\n case value2:\n # statements\n case _:\n # default case",
2828
"name": "Switch statement"
2929
},
30+
"pattern_matching": {
31+
"comment": "Structural Pattern Matching was introduced in Python 3.10.",
32+
"code": "match command.split():\n case [\"quit\"]:\n quit()\n case [\"load\", filename]:\n load(filename)\n case [\"move\", x, y] if int(y) > 0:\n move(x, y)\n case _: \n print(\"Unknown command\")",
33+
"name": "Pattern matching"
34+
},
3035
"ternary_conditional": {
3136
"code": "expression_if_true if condition else expression_if_false",
3237
"name": "Ternary conditional"

web/thesauruses/rust/1/control_structures.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@
2727
"name": "Ternary conditional"
2828
},
2929
"switch_statement": {
30-
"code": "match expression {\n pattern => { statements }\n _ => { statements } // the special pattern _ matches all cases\n}",
30+
"comment": "Rust uses the `match` keyword for switch-like behavior and exhaustive pattern matching.",
31+
"code": "match expression {\n pattern => { statements }\n _ => { statements } // default case\n}",
3132
"name": "Switch statement"
3233
},
34+
"pattern_matching": {
35+
"comment": "Rust has very powerful pattern matching capabilities with `match` and `if let`.",
36+
"code": "match some_option {\n Some(i) if i > 5 => println!(\"Greater than five: {}\", i),\n Some(i) => println!(\"Value: {}\", i),\n None => println!(\"No value\"),\n}",
37+
"name": "Pattern matching"
38+
},
3339
"while_loop": {
3440
"code": "while condition {\n statements;\n}",
3541
"name": "While loop"

web/thesauruses/swift/5/control_structures.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@
2727
"name": "Ternary conditional"
2828
},
2929
"switch_statement": {
30-
"code": "switch (expression) {\n case constant-expression:\n statements;\n break;\n default:\n statements;\n}",
30+
"comment": "Swift switches must be exhaustive and can match many different patterns.",
31+
"code": "switch someValue {\ncase 1:\n print(\"One\")\ncase 2...5:\n print(\"Between 2 and 5\")\ndefault:\n print(\"Something else\")\n}",
3132
"name": "Switch statement"
3233
},
34+
"pattern_matching": {
35+
"comment": "Swift has powerful pattern matching in `switch`, `if case`, `guard case`, and loops.",
36+
"code": "switch somePoint {\ncase (0, 0):\n print(\"Origin\")\ncase (_, 0):\n print(\"On the x-axis\")\ncase (0, _):\n print(\"On the y-axis\")\ncase (-2...2, -2...2):\n print(\"Near the origin\")\ndefault:\n print(\"Outside area\")\n}",
37+
"name": "Pattern matching"
38+
},
3339
"while_loop": {
3440
"code": "while condition {\n statements\n}",
3541
"name": "While loop"

0 commit comments

Comments
 (0)