diff options
Diffstat (limited to 'js/baba-yaga/scratch/baba/test_functional_enhancements.baba')
-rw-r--r-- | js/baba-yaga/scratch/baba/test_functional_enhancements.baba | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/js/baba-yaga/scratch/baba/test_functional_enhancements.baba b/js/baba-yaga/scratch/baba/test_functional_enhancements.baba new file mode 100644 index 0000000..e8e922a --- /dev/null +++ b/js/baba-yaga/scratch/baba/test_functional_enhancements.baba @@ -0,0 +1,132 @@ +// Test file for new functional programming enhancements +// This file tests: scan operations, indexing operations, combinators, and flatMap + +io.out "=== Testing Scan Operations ==="; + +// Test basic scan operation +numbers : [1, 2, 3, 4, 5]; +addFunc : acc x -> acc + x; +scanned : scan addFunc 0 numbers; +io.out "scan (+) 0 [1,2,3,4,5]:"; +io.out scanned; // Should be [0, 1, 3, 6, 10, 15] + +// Test cumsum utility +cumsumResult : cumsum numbers; +io.out "cumsum [1,2,3,4,5]:"; +io.out cumsumResult; // Should be [0, 1, 3, 6, 10, 15] + +// Test cumprod utility +cumprodResult : cumprod numbers; +io.out "cumprod [1,2,3,4,5]:"; +io.out cumprodResult; // Should be [1, 1, 2, 6, 24, 120] + +io.out "=== Testing Array Indexing Operations ==="; + +data : [10, 21, 30, 43, 50]; + +// Test 'at' operation +indices : [0, 2, 4]; +selected : at indices data; +io.out "at [0,2,4] [10,21,30,43,50]:"; +io.out selected; // Should be [10, 30, 50] + +// Test 'where' operation +evenPredicate : x -> x % 2 = 0; +evenIndices : where evenPredicate data; +io.out "where (even?) [10,21,30,43,50]:"; +io.out evenIndices; // Should be [0, 2, 4] (indices of 10, 30, 50) + +// Test 'take' operation +firstThree : take 3 data; +io.out "take 3 [10,21,30,43,50]:"; +io.out firstThree; // Should be [10, 21, 30] + +// Test 'drop' operation +lastTwo : drop 3 data; +io.out "drop 3 [10,21,30,43,50]:"; +io.out lastTwo; // Should be [43, 50] + +io.out "=== Testing Function Combinators ==="; + +// Test basic functions for combinators +add : x y -> x + y; +multiply : x y -> x * y; +double : x -> x * 2; +increment : x -> x + 1; + +// Test flip +flippedAdd : flip add; +result1 : flippedAdd 3 5; // Should be 5 + 3 = 8 +io.out "flip add 3 5:"; +io.out result1; + +// Test apply +result2 : apply double 7; // Should be 14 +io.out "apply double 7:"; +io.out result2; + +// Test pipe +result3 : pipe 5 double; // Should be 10 +io.out "pipe 5 double:"; +io.out result3; + +// Test compose +composed : compose increment double; // x -> (x * 2) + 1 +result4 : composed 4; // Should be 9 +io.out "compose increment double 4:"; +io.out result4; + +io.out "=== Testing flatMap ==="; + +// Test flatMap with simple function +duplicateFunc : x -> [x, x]; +original : [1, 2, 3]; +duplicated : flatMap duplicateFunc original; +io.out "flatMap (x -> [x,x]) [1,2,3]:"; +io.out duplicated; // Should be [1, 1, 2, 2, 3, 3] + +// Test flatMap with range generation +rangeFunc : x -> range 1 x; +rangeResult : flatMap rangeFunc [2, 3]; +io.out "flatMap (x -> range 1 x) [2,3]:"; +io.out rangeResult; // Should be [1, 2, 1, 2, 3] + +io.out "=== Combining Operations ==="; + +// Complex example: Find cumulative sums of doubled even numbers +evenNumbers : [2, 4, 6, 8]; +doubled : map double evenNumbers; +cumulative : cumsum doubled; +io.out "Cumsum of doubled evens [2,4,6,8]:"; +io.out cumulative; // [0, 4, 12, 24, 40] + +// Using combinators in a pipeline-like fashion +processNumber : x -> pipe x (compose increment double); +processed : map processNumber [1, 2, 3]; +io.out "Map (pipe x (compose inc double)) [1,2,3]:"; +io.out processed; // Should be [3, 5, 7] + +io.out "=== Testing Broadcasting Operations ==="; + +// Test broadcast (scalar-array operation) +addOp : x y -> x + y; +numbers2 : [1, 2, 3, 4]; +broadcasted : broadcast addOp 10 numbers2; +io.out "broadcast (+) 10 [1,2,3,4]:"; +io.out broadcasted; // Should be [11, 12, 13, 14] + +// Test zipWith (element-wise array-array operation) +array1 : [1, 2, 3]; +array2 : [10, 20, 30]; +zipped : zipWith addOp array1 array2; +io.out "zipWith (+) [1,2,3] [10,20,30]:"; +io.out zipped; // Should be [11, 22, 33] + +// Test reshape (2D matrix creation) +flatArray : [1, 2, 3, 4, 5, 6]; +matrixShape : [2, 3]; // 2 rows, 3 columns +matrix : reshape matrixShape flatArray; +io.out "reshape [2,3] [1,2,3,4,5,6]:"; +io.print matrix; // Should display as 2x3 matrix + +io.out "=== All tests completed ==="; |