diff options
Diffstat (limited to 'js/baba-yaga/scratch/baba/functional-features-demo.baba')
-rw-r--r-- | js/baba-yaga/scratch/baba/functional-features-demo.baba | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/js/baba-yaga/scratch/baba/functional-features-demo.baba b/js/baba-yaga/scratch/baba/functional-features-demo.baba new file mode 100644 index 0000000..c9cb12b --- /dev/null +++ b/js/baba-yaga/scratch/baba/functional-features-demo.baba @@ -0,0 +1,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!"; |