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
t;vc@akkartik.com> 2015-04-24 20:00:56 -0700 1170' href='/akkartik/mu/commit/cpp/020run?h=hlt&id=69e14325e3eaa3722766bb9706d7da5862b6ea26'>69e14325 ^
1228ec73 ^

7da71d03 ^
0edf822f ^
e0551d78 ^
c062021a ^

7da71d03 ^
9fc64bbc ^
363be37f ^
b75e94b3 ^


9fc64bbc ^

b75e94b3 ^
68329610 ^
69e14325 ^
c475ccc3 ^
a7d70735 ^
35379c9a ^
a767dbd3 ^
35064671 ^


e140ae63 ^
35064671 ^
98f3a942 ^


fca0ebbe ^
e74a2940 ^


35379c9a ^

e74a2940 ^
0487a30e ^
1066660e ^
fca0ebbe ^
dcfca05e ^
f1a6f323 ^
d241c9c4 ^
6d2b1168 ^













827898fc ^
d241c9c4 ^

f1a6f323 ^
d241c9c4 ^
dcfca05e ^
d241c9c4 ^
9cf71627 ^
6d2b1168 ^
cfb142b9 ^
35064671 ^
cfb142b9 ^




0487a30e ^
2dab0edb ^
1066660e ^
9fc64bbc ^
9d670bb5 ^
9fc64bbc ^

e74a2940 ^




3c435756 ^


2199940a ^
ac0e9db5 ^
8eff7919 ^
ec926027 ^

7c8493b3 ^
b75e94b3 ^
ec926027 ^

dcfca05e ^
05d17773 ^
dcfca05e ^

69e14325 ^
ac0e9db5 ^
ec926027 ^

b39ceb27 ^
3663ca6c ^
e7f76736 ^
e7f76736 ^
6ace0363 ^
e7f76736 ^
b39ceb27 ^
e7f76736 ^
b39ceb27 ^


f78f92c5 ^

7f402c85 ^
a8007cc4 ^
363be37f ^
35379c9a ^
e7f76736 ^
4caa718f ^
fb4836dc ^
e7f76736 ^

267ebb59 ^
e0551d78 ^



c062021a ^







e0551d78 ^
e0551d78 ^

f78f92c5 ^
11312cec ^
24047016 ^
f78f92c5 ^







6ace0363 ^
267ebb59 ^
bb8a644b ^
f5f4b698 ^
35064671 ^
f5f4b698 ^

ad8e984f ^
8c9e97ae ^
2b7a7498 ^
267ebb59 ^
ccd792da ^

b98d3876 ^
267ebb59 ^

7284d503 ^
96f19e1e ^
6ace0363 ^
3c435756 ^
3c435756 ^
2199940a ^
3c435756 ^
2c7e84c2 ^
363be37f ^
981cc9e8 ^
b1bbe92d ^
96f19e1e ^
3c435756 ^
3c435756 ^
7284d503 ^

fca0ebbe ^
0487a30e ^
fca0ebbe ^
ab6ed192 ^
a4ef18b1 ^
a26cc359 ^

ac0e9db5 ^


fca0ebbe ^
35064671 ^
b291f85b ^

cae5461b ^


fca0ebbe ^
6b6dfb0c ^
18429d40 ^
ac0e9db5 ^
70179d1f ^
c91caafd ^
35064671 ^
70179d1f ^
ac0e9db5 ^
35064671 ^
05d17773 ^
b291f85b ^
cae5461b ^

c1a50c82 ^
ac0e9db5 ^
93b7d983 ^

3076bab4 ^

363be37f ^
93b7d983 ^
f1a6f323 ^
f48f6c14 ^
eaa75c87 ^
decaddb4 ^
70179d1f ^
c91caafd ^

885315fe ^
70179d1f ^


6b6dfb0c ^



ab6ed192 ^
ac0e9db5 ^
c82d0176 ^

6d2b1168 ^



decaddb4 ^


bc643692 ^
5497090a ^
decaddb4 ^
bc643692 ^
d7494165 ^

fc55fea0 ^


bc643692 ^
fc55fea0 ^
bc643692 ^
18429d40 ^
bc643692 ^
18429d40 ^
bc643692 ^
18429d40 ^
bc643692 ^
6d2b1168 ^




















677fa104 ^





578327f1 ^
677fa104 ^
578327f1 ^
677fa104 ^


578327f1 ^
677fa104 ^
578327f1 ^
677fa104 ^


578327f1 ^
677fa104 ^
578327f1 ^
677fa104 ^


578327f1 ^
677fa104 ^
578327f1 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359