about summary refs log tree commit diff stats
path: root/js/scripting-lang/tutorials/02_Function_Composition.md
diff options
context:
space:
mode:
Diffstat (limited to 'js/scripting-lang/tutorials/02_Function_Composition.md')
-rw-r--r--js/scripting-lang/tutorials/02_Function_Composition.md138
1 files changed, 138 insertions, 0 deletions
diff --git a/js/scripting-lang/tutorials/02_Function_Composition.md b/js/scripting-lang/tutorials/02_Function_Composition.md
new file mode 100644
index 0000000..a6137b4
--- /dev/null
+++ b/js/scripting-lang/tutorials/02_Function_Composition.md
@@ -0,0 +1,138 @@
+# Function Composition
+
+## What is the `via` Operator?
+
+The `via` operator is a function composition operator that combines functions from right to left.
+
+```plaintext
+/* f via g = compose(f, g) */
+/* f via g via h = compose(f, compose(g, h)) */
+```
+
+The `via` operator is right-associative and matches mathematical notation where `(f ∘ g ∘ h)(x) = f(g(h(x)))`.
+
+```plaintext
+/* Define simple functions */
+double : x -> x * 2;
+increment : x -> x + 1;
+square : x -> x * x;
+
+/* Using via composition */
+result1 : double via increment 5;
+/* Result: 12 (5+1=6, 6*2=12) */
+
+/* Chained via composition */
+result2 : double via increment via square 3;
+/* Result: 20 (3^2=9, 9+1=10, 10*2=20) */
+```
+
+The key insight is that `via` groups from right to left.
+
+```plaintext
+/* This expression: */
+double via increment via square 3
+
+/* Groups as: */
+double via (increment via square) 3
+
+/* Which translates to: */
+compose(double, compose(increment, square))(3)
+
+/* With the execution order of: */
+/* 1. square(3) = 9 */
+/* 2. increment(9) = 10 */
+/* 3. double(10) = 20 */
+```
+
+## Precedence rules and `via`
+
+The `via` operator has higher precedence than function application:
+
+```plaintext
+/* via binds tighter than juxtaposition */
+double via increment 5
+
+/* This is parsed as: */
+(double via increment) 5
+```
+
+## More examples
+
+```plaintext
+/* Data processing pipeline */
+data : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+is_even : x -> x % 2 = 0;
+double : x -> x * 2;
+sum : x -> reduce add 0 x;
+
+/* Pipeline using via */
+process_pipeline : sum via map double via filter is_even;
+result : process_pipeline data;
+/* Reads: sum via (map double via filter is_even) */
+/* Result: 60 */
+```
+
+You'll note that we don't need to use `@` here -- `via` is kinda special-cased because it is an ergonomic feature. It can work with function names directly because it's specifically for function composition. Higher-order functions like `map`, `filter`, and `reduce` require explicit function references using `@` because they need a way to distinguish between calling a function immediately vs passing it as an argument while `via` only ever takes in functions.
+
+
+A goal with the `via` operator is to align with mathematical function composition:
+
+```plaintext
+/* Mathematical: (f ∘ g ∘ h)(x) = f(g(h(x))) */
+/* Baba Yaga: f via g via h x = f(g(h(x))) */
+```
+
+## When to Use `via`
+
+**Use `via` when you want:**
+- Natural reading: `f via g via h` reads as "f then g then h"
+- Mathematical notation: Matches `(f ∘ g ∘ h)` notation
+- Concise syntax: Shorter than nested `compose` calls
+- Right-to-left flow: When you think of data flowing right to left
+
+**Don't use `via` when:**
+- You need left-to-right composition (use `pipe`)
+- You want explicit mathematical style (use `compose`)
+- You're working with simple function calls (use juxtaposition)
+- If you don't wanna
+
+## Common Patterns
+
+```plaintext
+/* Data transformation pipeline */
+data : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+is_even : x -> x % 2 = 0;
+double : x -> x * 2;
+sum : x -> reduce add 0 x;
+
+/* Pipeline: filter → map → reduce */
+process_pipeline : sum via map double via filter is_even;
+result : process_pipeline data;
+/* Result: 60 (filter evens: {2,4,6,8,10}, double: {4,8,12,16,20}, sum: 60) */
+
+/* Validation chain */
+validate_positive : x -> x > 0;
+validate_even : x -> x % 2 = 0;
+validate_small : x -> x < 10;
+
+/* Chain validations */
+all_validations : validate_small via validate_even via validate_positive;
+result : all_validations 6;  /* 6 > 0, 6 % 2 = 0, 6 < 10 */
+/* Result: true */
+```
+
+## Debugging `via` Chains
+
+To understand execution order, break down the chain:
+
+```plaintext
+/* Complex chain: */
+result : square via double via increment via square 2;
+
+/* Break it down: */
+/* 1. square(2) = 4 */
+/* 2. increment(4) = 5 */
+/* 3. double(5) = 10 */
+/* 4. square(10) = 100 */
+/* Result: 100 */
+```
\ No newline at end of file
f1dcfae7b9ae659ccea4944b95a29'>dc9afcbd ^
af023b32 ^
9dcbec39 ^
dc9afcbd ^






2ab491c2 ^
d8603e81 ^

2ab491c2 ^
d8603e81 ^
dc9afcbd ^


d8603e81 ^
dc9afcbd ^


2ab491c2 ^





dc9afcbd ^



192d59d3 ^
dc9afcbd ^







2ab491c2 ^
192d59d3 ^
dc9afcbd ^






3473c63a ^


dc9afcbd ^
3473c63a ^

dc9afcbd ^
3473c63a ^

dc9afcbd ^
3473c63a ^



db69ad7a ^

dc9afcbd ^











8dede22e ^
dc9afcbd ^

af023b32 ^
dc9afcbd ^
af023b32 ^
97a41843 ^
dc9afcbd ^



af023b32 ^
9dcbec39 ^
dc9afcbd ^








6c96a437 ^
dc9afcbd ^















192d59d3 ^


dc9afcbd ^






192d59d3 ^



dc9afcbd ^






192d59d3 ^


dc9afcbd ^













192d59d3 ^

dc9afcbd ^










fc19980d ^




192d59d3 ^

fc19980d ^













dc9afcbd ^


192d59d3 ^
dc9afcbd ^
192d59d3 ^
dc9afcbd ^






192d59d3 ^





dc9afcbd ^





b9a78a84 ^

192d59d3 ^


b9a78a84 ^






dc9afcbd ^



192d59d3 ^





dc9afcbd ^

















192d59d3 ^





dc9afcbd ^




192d59d3 ^




dc9afcbd ^
192d59d3 ^

dc9afcbd ^


fc19980d ^



192d59d3 ^





fc19980d ^
192d59d3 ^
fc19980d ^










88298924 ^


192d59d3 ^



88298924 ^



dc9afcbd ^














192d59d3 ^





dc9afcbd ^












d52406cc ^
dc9afcbd ^
dc9afcbd ^
6e793202 ^
dc9afcbd ^





192d59d3 ^
d52406cc ^
dc9afcbd ^
dc9afcbd ^
6e793202 ^
dc9afcbd ^





d52406cc ^

dc9afcbd ^
dc9afcbd ^
6e793202 ^
dc9afcbd ^








































555d95c1 ^
dc9afcbd ^



9dcbec39 ^
dc9afcbd ^









3473c63a ^
dc9afcbd ^



















3473c63a ^
dc9afcbd ^




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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540