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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
|
import { createLexer } from '../src/core/lexer.js';
import { createParser } from '../src/core/parser.js';
import { createInterpreter } from '../src/core/interpreter.js';
function runBabaYaga(code) {
const lexer = createLexer(code);
const tokens = lexer.allTokens();
const parser = createParser(tokens);
const ast = parser.parse();
const outputs = [];
const debugOutputs = [];
const host = {
io: {
out: (...args) => outputs.push(args.join(' ')),
debug: (...args) => debugOutputs.push(args.join(' ')),
in: () => '',
},
};
const interpreter = createInterpreter(ast, host);
const result = interpreter.interpret();
return { outputs, debugOutputs, result };
}
describe('Functional Programming Enhancements', () => {
describe('Scan Operations', () => {
test('scan with addition function', () => {
const code = `
addFunc : acc x -> acc + x;
numbers : [1, 2, 3, 4, 5];
result : scan addFunc 0 numbers;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('0,1,3,6,10,15');
});
test('cumsum utility function', () => {
const code = `
numbers : [1, 2, 3, 4, 5];
result : cumsum numbers;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('0,1,3,6,10,15');
});
test('cumprod utility function', () => {
const code = `
numbers : [1, 2, 3, 4];
result : cumprod numbers;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('1,1,2,6,24');
});
test('scan with multiplication function', () => {
const code = `
mulFunc : acc x -> acc * x;
numbers : [2, 3, 4];
result : scan mulFunc 1 numbers;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('1,2,6,24');
});
});
describe('Array Indexing Operations', () => {
test('at function selects elements at indices', () => {
const code = `
data : [10, 20, 30, 40, 50];
indices : [0, 2, 4];
result : at indices data;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('10,30,50');
});
test('where function finds matching indices', () => {
const code = `
data : [10, 21, 30, 43, 50];
evenPredicate : x -> x % 2 = 0;
result : where evenPredicate data;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('0,2,4');
});
test('take function gets first n elements', () => {
const code = `
data : [1, 2, 3, 4, 5, 6];
result : take 3 data;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('1,2,3');
});
test('drop function removes first n elements', () => {
const code = `
data : [1, 2, 3, 4, 5, 6];
result : drop 3 data;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('4,5,6');
});
test('at with empty indices returns empty array', () => {
const code = `
data : [1, 2, 3];
indices : [];
result : at indices data;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('');
});
test('take with zero returns empty array', () => {
const code = `
data : [1, 2, 3];
result : take 0 data;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('');
});
});
describe('Function Combinators', () => {
test('flip reverses function argument order', () => {
const code = `
subtract : x y -> x - y;
flippedSubtract : flip subtract;
result : flippedSubtract 3 10;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('7'); // 10 - 3 = 7
});
test('apply applies function to value', () => {
const code = `
double : x -> x * 2;
result : apply double 7;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('14');
});
test('pipe pipes value through function', () => {
const code = `
triple : x -> x * 3;
result : pipe 4 triple;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('12');
});
test('compose creates function composition', () => {
const code = `
increment : x -> x + 1;
double : x -> x * 2;
composed : compose increment double;
result : composed 5;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('11'); // increment(double(5)) = increment(10) = 11
});
test('combinators work with curried functions', () => {
const code = `
add : x -> y -> x + y;
add5 : add 5;
flippedAdd5 : flip add5;
result : flippedAdd5 3;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('8'); // Should still work: 5 + 3 = 8
});
});
describe('Broadcasting Operations', () => {
test('broadcast applies scalar operation to array', () => {
const code = `
addOp : x y -> x + y;
numbers : [1, 2, 3, 4];
result : broadcast addOp 10 numbers;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('11,12,13,14');
});
test('zipWith applies operation element-wise', () => {
const code = `
mulOp : x y -> x * y;
array1 : [1, 2, 3];
array2 : [4, 5, 6];
result : zipWith mulOp array1 array2;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('4,10,18');
});
test('zipWith handles arrays of different lengths', () => {
const code = `
addOp : x y -> x + y;
array1 : [1, 2, 3, 4, 5];
array2 : [10, 20, 30];
result : zipWith addOp array1 array2;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('11,22,33'); // Only processes minimum length
});
test('reshape creates 2D matrix', () => {
const code = `
flatArray : [1, 2, 3, 4, 5, 6];
result : reshape [2, 3] flatArray;
// Check that result is a 2x3 matrix
row1 : result.0;
row2 : result.1;
io.out row1;
io.out row2;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('1,2,3'); // First row
expect(outputs[1]).toBe('4,5,6'); // Second row
});
test('broadcast with subtraction', () => {
const code = `
subOp : x y -> x - y;
numbers : [10, 20, 30];
result : broadcast subOp 5 numbers;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('-5,-15,-25'); // 5 - 10, 5 - 20, 5 - 30
});
});
describe('Monadic Operations', () => {
test('flatMap flattens mapped results', () => {
const code = `
duplicateFunc : x -> [x, x];
original : [1, 2, 3];
result : flatMap duplicateFunc original;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('1,1,2,2,3,3');
});
test('flatMap with range generation', () => {
const code = `
rangeFunc : x -> range 1 x;
original : [2, 3];
result : flatMap rangeFunc original;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('1,2,1,2,3');
});
test('flatMap with empty results', () => {
const code = `
emptyFunc : x -> [];
original : [1, 2, 3];
result : flatMap emptyFunc original;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('');
});
test('flatMap with mixed result lengths', () => {
const code = `
variableFunc : x -> when x is
1 then [x]
2 then [x, x]
_ then [x, x, x];
original : [1, 2, 3];
result : flatMap variableFunc original;
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('1,2,2,3,3,3');
});
});
describe('Pattern Guards', () => {
test('basic pattern guards with numeric conditions', () => {
const code = `
classify : x ->
when x is
n if (n > 0) then "positive"
n if (n < 0) then "negative"
0 then "zero";
result1 : classify 5;
result2 : classify -3;
result3 : classify 0;
io.out result1;
io.out result2;
io.out result3;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('positive');
expect(outputs[1]).toBe('negative');
expect(outputs[2]).toBe('zero');
});
test('pattern guards with range conditions', () => {
const code = `
categorizeAge : age ->
when age is
a if (a >= 0 and a < 18) then "minor"
a if (a >= 18 and a < 65) then "adult"
a if (a >= 65) then "senior"
_ then "invalid";
result1 : categorizeAge 16;
result2 : categorizeAge 30;
result3 : categorizeAge 70;
io.out result1;
io.out result2;
io.out result3;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('minor');
expect(outputs[1]).toBe('adult');
expect(outputs[2]).toBe('senior');
});
test('pattern guards with complex conditions', () => {
const code = `
gradeStudent : score ->
when score is
s if (s >= 90) then "A"
s if (s >= 80 and s < 90) then "B"
s if (s >= 70 and s < 80) then "C"
s if (s < 70) then "F"
_ then "Invalid";
result1 : gradeStudent 95;
result2 : gradeStudent 85;
result3 : gradeStudent 75;
result4 : gradeStudent 65;
io.out result1;
io.out result2;
io.out result3;
io.out result4;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('A');
expect(outputs[1]).toBe('B');
expect(outputs[2]).toBe('C');
expect(outputs[3]).toBe('F');
});
test('pattern guards with wildcard patterns', () => {
const code = `
checkRange : x ->
when x is
_ if (x >= 1 and x <= 10) then "small"
_ if (x >= 11 and x <= 100) then "medium"
_ if (x > 100) then "large"
_ then "invalid";
result1 : checkRange 5;
result2 : checkRange 50;
result3 : checkRange 150;
result4 : checkRange -5;
io.out result1;
io.out result2;
io.out result3;
io.out result4;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('small');
expect(outputs[1]).toBe('medium');
expect(outputs[2]).toBe('large');
expect(outputs[3]).toBe('invalid');
});
test('pattern guards fail when condition is false', () => {
const code = `
testGuard : x ->
when x is
n if (n > 10) then "big"
_ then "small";
result1 : testGuard 15;
result2 : testGuard 5;
io.out result1;
io.out result2;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('big');
expect(outputs[1]).toBe('small');
});
});
describe('Integration Tests', () => {
test('combining scan and broadcast operations', () => {
const code = `
numbers : [1, 2, 3, 4];
cumulative : cumsum numbers;
addTen : broadcast (x y -> x + y) 10 cumulative;
io.out addTen;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('10,11,13,16,20'); // cumsum [1,2,3,4] = [0,1,3,6,10], then +10 each
});
test('combining flatMap with array indexing', () => {
const code = `
data : [[1, 2], [3, 4, 5], [6]];
flattened : flatMap (x -> x) data;
evens : where (x -> x % 2 = 0) flattened;
evenValues : at evens flattened;
io.out evenValues;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('2,4,6');
});
test('combining pattern guards with functional operations', () => {
const code = `
processNumbers : numbers ->
with (
classified : map (n -> when n is
x if (x > 0) then "pos"
x if (x < 0) then "neg"
0 then "zero") numbers;
positives : filter (n -> n > 0) numbers;
posSum : reduce (acc x -> acc + x) 0 positives;
) ->
{classifications: classified, sum: posSum};
result : processNumbers [-2, 0, 3, -1, 5];
io.out result.sum;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('8'); // 3 + 5 = 8
});
test('complex pipeline with multiple new features', () => {
const code = `
data : [1, 2, 3, 4, 5];
// Use scan to get cumulative sums
cumSums : cumsum data;
// Use broadcast to multiply by 2
doubled : broadcast (x y -> x * y) 2 cumSums;
// Use where to find indices of values > 10
bigIndices : where (x -> x > 10) doubled;
// Use at to get those values
bigValues : at bigIndices doubled;
io.out bigValues;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('12,20,30'); // Values > 10 from [0,2,6,12,20,30]
});
});
describe('Error Handling', () => {
test('at throws error for out of bounds index', () => {
const code = `
data : [1, 2, 3];
indices : [0, 5];
result : at indices data;
`;
expect(() => runBabaYaga(code)).toThrow(/Index out of bounds|Can't find variable/);
});
test('reshape throws error for incompatible dimensions', () => {
const code = `
data : [1, 2, 3, 4, 5];
result : reshape [2, 3] data;
`;
expect(() => runBabaYaga(code)).toThrow('Cannot reshape array');
});
test('scan requires function as first argument', () => {
const code = `
result : scan 42 0 [1, 2, 3];
`;
expect(() => runBabaYaga(code)).toThrow('Scan expects a function');
});
test('broadcast requires function as first argument', () => {
const code = `
result : broadcast "not a function" 5 [1, 2, 3];
`;
expect(() => runBabaYaga(code)).toThrow('broadcast expects a function');
});
test('where requires function as first argument', () => {
const code = `
result : where "not a function" [1, 2, 3];
`;
expect(() => runBabaYaga(code)).toThrow('where expects a function');
});
test('flatMap requires function as first argument', () => {
const code = `
result : flatMap 42 [1, 2, 3];
`;
expect(() => runBabaYaga(code)).toThrow('flatMap expects a function');
});
test('take with negative number throws error', () => {
const code = `
result : take -1 [1, 2, 3];
`;
expect(() => runBabaYaga(code)).toThrow('take expects a non-negative number');
});
test('drop with negative number throws error', () => {
const code = `
result : drop -1 [1, 2, 3];
`;
expect(() => runBabaYaga(code)).toThrow('drop expects a non-negative number');
});
});
describe('Edge Cases', () => {
test('scan with empty array', () => {
const code = `
addFunc : acc x -> acc + x;
result : scan addFunc 0 [];
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('0'); // Just the initial value
});
test('broadcast with empty array', () => {
const code = `
addOp : x y -> x + y;
result : broadcast addOp 5 [];
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe(''); // Empty result
});
test('zipWith with empty arrays', () => {
const code = `
addOp : x y -> x + y;
result : zipWith addOp [] [];
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe(''); // Empty result
});
test('where with no matches', () => {
const code = `
neverTrue : x -> false;
result : where neverTrue [1, 2, 3];
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe(''); // No matching indices
});
test('flatMap with single-element arrays', () => {
const code = `
wrapFunc : x -> [x];
result : flatMap wrapFunc [1, 2, 3];
io.out result;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('1,2,3'); // Should flatten to original
});
test('pattern guards with complex boolean expressions', () => {
const code = `
complexTest : x ->
when x is
n if ((n > 5) and (n < 15) and (n % 2 = 0)) then "even between 5 and 15"
n if ((n > 0) or (n < -10)) then "positive or very negative"
_ then "other";
result1 : complexTest 8;
result2 : complexTest 3;
result3 : complexTest -15;
result4 : complexTest -5;
io.out result1;
io.out result2;
io.out result3;
io.out result4;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('even between 5 and 15'); // 8 matches first condition
expect(outputs[1]).toBe('positive or very negative'); // 3 is positive
expect(outputs[2]).toBe('positive or very negative'); // -15 is very negative
expect(outputs[3]).toBe('other'); // -5 doesn't match any condition
});
test('combinators with identity functions', () => {
const code = `
identity : x -> x;
doubled : x -> x * 2;
// Compose with identity should be equivalent to original function
composedWithId : compose identity doubled;
result1 : composedWithId 5;
// Apply identity should return original value
result2 : apply identity 42;
// Pipe through identity should return original value
result3 : pipe 7 identity;
io.out result1;
io.out result2;
io.out result3;
`;
const { outputs } = runBabaYaga(code);
expect(outputs[0]).toBe('10'); // identity(doubled(5)) = 10
expect(outputs[1]).toBe('42'); // identity(42) = 42
expect(outputs[2]).toBe('7'); // pipe 7 identity = 7
});
});
});
|