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
|
// Functional Programming Features Demo
// Testing all the new features we've implemented
io.out "=== Testing New Functional Programming Features ===";
io.out "";
// === Pattern Guards ===
io.out "1. Pattern Guards:";
classifyNumber : n ->
when n is
0 then "zero"
x if (x > 0) then "positive"
x if (x < 0) then "negative"
_ then "unknown";
result1 : classifyNumber 5;
result2 : classifyNumber -3;
result3 : classifyNumber 0;
io.out ("5 is " .. result1);
io.out ("-3 is " .. result2);
io.out ("0 is " .. result3);
io.out "";
// === Scan Operations ===
io.out "2. Scan Operations:";
numbers : [1, 2, 3, 4, 5];
cumulative : cumsum numbers;
product : cumprod numbers;
addFunc : acc x -> acc + x;
customScan : scan addFunc 10 numbers;
io.out ("Numbers: " .. numbers);
io.out ("Cumulative sum: " .. cumulative);
io.out ("Cumulative product: " .. product);
io.out ("Custom scan from 10: " .. customScan);
io.out "";
// === Array Indexing ===
io.out "3. Array Indexing:";
data : [10, 20, 30, 40, 50];
indices : [0, 2, 4];
selected : at indices data;
evenPredicate : x -> x % 2 = 0;
evenIndices : where evenPredicate data;
firstThree : take 3 data;
lastTwo : drop 3 data;
io.out ("Data: " .. data);
io.out ("Selected at [0,2,4]: " .. selected);
io.out ("Even indices: " .. evenIndices);
io.out ("First 3: " .. firstThree);
io.out ("Last 2: " .. lastTwo);
io.out "";
// === Broadcasting ===
io.out "4. Broadcasting Operations:";
values : [1, 2, 3, 4];
addTen : broadcast (a b -> a + b) 10 values;
array1 : [1, 2, 3];
array2 : [4, 5, 6];
multiplied : zipWith (a b -> a * b) array1 array2;
flatData : [1, 2, 3, 4, 5, 6];
matrix : reshape [2, 3] flatData;
io.out ("Values: " .. values);
io.out ("Add 10 to each: " .. addTen);
io.out ("Array 1: " .. array1);
io.out ("Array 2: " .. array2);
io.out ("Element-wise multiply: " .. multiplied);
io.out "Reshaped matrix:";
io.print matrix;
io.out "";
// === Function Combinators ===
io.out "5. Function Combinators:";
addOp : x y -> x + y;
flippedAdd : flip addOp;
flipResult : flippedAdd 3 7; // 7 + 3
doubler : x -> x * 2;
applyResult : apply doubler 5;
tripler : x -> x * 3;
pipeResult : pipe 4 tripler;
increment : x -> x + 1;
composed : compose doubler increment;
composeResult : composed 5; // (5 + 1) * 2
io.out ("Flip add 3 7: " .. flipResult);
io.out ("Apply double to 5: " .. applyResult);
io.out ("Pipe 4 through triple: " .. pipeResult);
io.out ("Compose increment then double on 5: " .. composeResult);
io.out "";
// === FlatMap ===
io.out "6. FlatMap Operations:";
duplicator : x -> [x, x];
original : [1, 2, 3];
duplicated : flatMap duplicator original;
io.out ("Original: " .. original);
io.out ("Duplicated: " .. duplicated);
io.out "";
// === Summary ===
io.out "=== Summary ===";
io.out "All functional programming features working:";
io.out "✓ Pattern Guards with conditional expressions";
io.out "✓ Scan operations (scan, cumsum, cumprod)";
io.out "✓ Array indexing (at, where, take, drop)";
io.out "✓ Broadcasting (broadcast, zipWith, reshape)";
io.out "✓ Function combinators (flip, apply, pipe, compose)";
io.out "✓ Monadic operations (flatMap)";
io.out "";
io.out "Baba Yaga now has powerful functional programming capabilities!";
|