about summary refs log tree commit diff stats
path: root/js/scripting-lang/tutorials/05_When_Expressions_Pattern_Matching.md
diff options
context:
space:
mode:
Diffstat (limited to 'js/scripting-lang/tutorials/05_When_Expressions_Pattern_Matching.md')
-rw-r--r--js/scripting-lang/tutorials/05_When_Expressions_Pattern_Matching.md212
1 files changed, 0 insertions, 212 deletions
diff --git a/js/scripting-lang/tutorials/05_When_Expressions_Pattern_Matching.md b/js/scripting-lang/tutorials/05_When_Expressions_Pattern_Matching.md
deleted file mode 100644
index a13f773..0000000
--- a/js/scripting-lang/tutorials/05_When_Expressions_Pattern_Matching.md
+++ /dev/null
@@ -1,212 +0,0 @@
-# `when` Expressions (Pattern Matching)
-
-## What are `when` Expressions?
-
-`when` expressions provide pattern matching functionality, allowing you to match values against patterns and execute different code based on the match.
-
-```plaintext
-/* Pattern matching with when expressions */
-result : when x is
-  0 then "zero"
-  1 then "one"
-  _ then "other";
-```
-
-## Why is This Esoteric?
-
-Pattern matching is common in functional languages but rare in imperative/OOP languages. The `when` syntax with `then` clauses and `_` wildcards is unique to our language.
-
-## Basic Examples
-
-```plaintext
-/* Simple pattern matching */
-x : 5;
-result : when x is
-  0 then "zero"
-  1 then "one"
-  2 then "two"
-  _ then "other";
-/* Result: "other" */
-
-/* Pattern matching with numbers */
-grade : 85;
-letter_grade : when grade is
-  90 then "A"
-  80 then "B"
-  70 then "C"
-  60 then "D"
-  _ then "F";
-/* Result: "B" */
-```
-
-## Pattern Types
-
-### Literal Patterns
-```plaintext
-/* Match exact values */
-result : when value is
-  true then "yes"
-  false then "no"
-  _ then "maybe";
-```
-
-### Wildcard Pattern
-```plaintext
-/* _ matches anything */
-result : when x is
-  0 then "zero"
-  _ then "not zero";
-```
-
-### Function Reference Patterns
-```plaintext
-/* Match function references */
-double : x -> x * 2;
-square : x -> x * x;
-
-result : when function is
-  @double then "doubling function"
-  @square then "squaring function"
-  _ then "other function";
-```
-
-### Boolean Patterns
-```plaintext
-/* Match boolean values */
-result : when condition is
-  true then "condition is true"
-  false then "condition is false";
-```
-
-## Complex Examples
-
-```plaintext
-/* Grade classification with ranges */
-score : 85;
-grade : when score is
-  when score >= 90 then "A"
-  when score >= 80 then "B"
-  when score >= 70 then "C"
-  when score >= 60 then "D"
-  _ then "F";
-/* Result: "B" */
-
-/* Multiple conditions */
-x : 5;
-y : 10;
-result : when x is
-  when x = y then "equal"
-  when x > y then "x is greater"
-  when x < y then "x is less"
-  _ then "impossible";
-/* Result: "x is less" */
-```
-
-## Nested `when` Expressions
-
-You can nest `when` expressions for complex logic:
-
-```plaintext
-/* Nested pattern matching */
-x : 5;
-y : 10;
-result : when x is
-  0 then when y is
-    0 then "both zero"
-    _ then "x is zero"
-  1 then when y is
-    1 then "both one"
-    _ then "x is one"
-  _ then when y is
-    0 then "y is zero"
-    1 then "y is one"
-    _ then "neither special";
-/* Result: "neither special" */
-```
-
-## Using `when` with Functions
-
-```plaintext
-/* Function that uses pattern matching */
-classify_number : x -> when x is
-  0 then "zero"
-  when x % 2 = 0 then "even"
-  when x % 2 = 1 then "odd"
-  _ then "unknown";
-
-/* Use the function */
-result1 : classify_number 0;   /* "zero" */
-result2 : classify_number 4;   /* "even" */
-result3 : classify_number 7;   /* "odd" */
-```
-
-## Common Patterns
-
-```plaintext
-/* Pattern 1: Value classification */
-classify_age : age -> when age is
-  when age < 13 then "child"
-  when age < 20 then "teenager"
-  when age < 65 then "adult"
-  _ then "senior";
-
-/* Pattern 2: Error handling */
-safe_divide : x y -> when y is
-  0 then "error: division by zero"
-  _ then x / y;
-
-/* Pattern 3: Status mapping */
-status_code : 404;
-status_message : when status_code is
-  200 then "OK"
-  404 then "Not Found"
-  500 then "Internal Server Error"
-  _ then "Unknown Error";
-```
-
-## When to Use `when` Expressions
-
-**Use `when` expressions when:**
-- You need to match values against multiple patterns
-- You want to replace complex if/else chains
-- You're working with enumerated values
-- You need to handle different cases based on value types
-- You want to make conditional logic more readable
-
-**Don't use `when` expressions when:**
-- You only have a simple true/false condition (use logical operators)
-- You need to perform side effects (use regular expressions)
-- You're working with complex nested conditions (consider breaking into functions)
-
-## Comparison with Traditional Conditionals
-
-```plaintext
-/* Traditional if/else approach (not available in our language) */
-/* if x = 0 then "zero" else if x = 1 then "one" else "other" */
-
-/* Our when expression approach */
-result : when x is
-  0 then "zero"
-  1 then "one"
-  _ then "other";
-```
-
-## Key Takeaways
-
-1. **Pattern matching** - match values against specific patterns
-2. **Wildcard support** - `_` matches anything
-3. **Exhaustive matching** - all cases must be covered
-4. **Nested support** - can nest `when` expressions
-5. **Functional style** - expressions return values
-
-## Why This Matters
-
-`when` expressions make conditional logic more functional and readable:
-
-- **Pattern-based thinking** - focus on what values match, not how to test them
-- **Exhaustive coverage** - ensures all cases are handled
-- **Functional style** - expressions return values rather than performing actions
-- **Readable syntax** - clear separation between patterns and results
-- **Composable** - can be used in function composition
-
-This feature makes the language feel more like functional programming languages like Haskell or ML! 🚀 
\ No newline at end of file