about summary refs log tree commit diff stats
path: root/js/scripting-lang/tutorials/05_Pattern_Matching.md
diff options
context:
space:
mode:
Diffstat (limited to 'js/scripting-lang/tutorials/05_Pattern_Matching.md')
-rw-r--r--js/scripting-lang/tutorials/05_Pattern_Matching.md247
1 files changed, 247 insertions, 0 deletions
diff --git a/js/scripting-lang/tutorials/05_Pattern_Matching.md b/js/scripting-lang/tutorials/05_Pattern_Matching.md
new file mode 100644
index 0000000..c6097c3
--- /dev/null
+++ b/js/scripting-lang/tutorials/05_Pattern_Matching.md
@@ -0,0 +1,247 @@
+# `when` Expressions (Pattern Matching)
+
+## What are `when` Expressions?
+
+This is kinda where the whole idea for Baba Yaga started. Pattern matching is an approach to flow control. We do this in Baba Yaga using the `when` expression. It provides 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";
+```
+
+Baba Yaga's pattern matching syntax has a lot of insporations, but especially `cond` patterns, Gleam's pattern matching, and Roc's, too. 
+
+## 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 using @ operator */
+double : x -> x * 2;
+square : x -> x * x;
+
+which : x -> when x is
+  @double then "doubling function"
+  @square then "squaring function"
+  _ then "other function";
+
+test1 : which double;
+test2 : which square;
+```
+
+As is called out elsewhere, too, the `@` operator is required when matching function references in patterns. This distinguishes between calling a function and matching against the function itself.
+
+### 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" */
+```
+
+## Advanced Pattern Matching
+
+You can match multiple values with complex expressions:
+
+```plaintext
+/* FizzBuzz implementation using multi-value patterns */
+fizzbuzz : n ->
+  when (n % 3) (n % 5) is
+    0 0 then "FizzBuzz"
+    0 _ then "Fizz"
+    _ 0 then "Buzz"
+    _ _ then n;
+
+/* Test the FizzBuzz function */
+result1 : fizzbuzz 15;  /* "FizzBuzz" */
+result2 : fizzbuzz 3;   /* "Fizz" */
+result3 : fizzbuzz 5;   /* "Buzz" */
+result4 : fizzbuzz 7;   /* 7 */
+```
+
+You can access table properties directly in patterns:
+
+```plaintext
+/* User role checking */
+user : {role: "admin", level: 5};
+
+access_level : when user.role is
+  "admin" then "full access"
+  "user" then "limited access"
+  _ then "no access";
+/* Result: "full access" */
+```
+
+You can use function calls in patterns. Be warned, though -- they require parentheses to help disambiguate them from other references, though.
+
+```plaintext
+/* Even/odd classification */
+is_even : n -> n % 2 = 0;
+
+classify : n ->
+  when (is_even n) is
+    true then "even number"
+    false then "odd number";
+
+/* Test the classification */
+result1 : classify 4;  /* "even number" */
+result2 : classify 7;  /* "odd number" */
+```
+
+Function calls in patterns must be wrapped in parentheses!
+
+This'll work:
+```plaintext
+when (is_even n) is true then "even"
+when (complex_func x y) is result then "matched"
+```
+
+This won't work: 
+```plaintext
+when is_even n is true then "even"  /* Ambiguous parsing */
+```
+
+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"
+  (x % 2 = 0) then "even"
+  (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
+/* Value classification */
+classify_age : age -> when age is
+  (age < 13) then "child"
+  (age < 20) then "teenager"
+  (age < 65) then "adult"
+  _ then "senior";
+
+/* Error handling */
+safe_divide : x y -> when y is
+  0 then "error: division by zero"
+  _ then x / y;
+
+/* Status mapping */
+status_code : 404;
+status_message : x -> 
+    when x is
+      200 then "OK"
+      404 then "Not Found"
+      500 then "Internal Server Error"
+        _ then "Unknown Error";
+```
+
+## When to Use `when` pattern matching
+
+**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
+- **You need to match multiple values simultaneously** (multi-value patterns)
+- **You want to access table properties in patterns** (table access)
+- **You need to use function results in patterns** (function calls with parentheses)
+- **You're implementing complex validation logic** (multi-field validation)
+- **You need to match function references** (using `@` operator)
+
+**Don't use `when` expressions when:**
+- You only have a simple true/false condition (use logical operators)
+- You're working with complex nested conditions (consider breaking into functions)
\ No newline at end of file
'Blame the previous revision' href='/akspecs/ranger/blame/Makefile?h=v1.9.2&id=a5d9423cad0a112564447b2519208b9ec5354665'>^
ad75190c ^
e9e4b4ff ^
c7720fff ^



8d21b83c ^


e9e4b4ff ^
25a4162d ^
e9e4b4ff ^
0c2c782d ^
636d9393 ^
b0a216f5 ^
b3bc8431 ^
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














                                                                       
             
                                                      
                                                                                   


                                                                            
                                       
                   
            
               

                  
 
                
                                                            
 



                                  
                                          


                                  
                                             
                                                        

                                                                    
                                                                          
 
        
                                                 

                                                            
              
                                                                      
 
      

                                                                          
 





                                                  
                                                                       
 



                                                                               


                                                           
         
                                                              
 
         
                                                                                              
 
                                                                       
 id='n848' href='#n848'>848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210