diff options
Diffstat (limited to 'js/scripting-lang/tests/16_advanced_functional.txt')
-rw-r--r-- | js/scripting-lang/tests/16_advanced_functional.txt | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/js/scripting-lang/tests/16_advanced_functional.txt b/js/scripting-lang/tests/16_advanced_functional.txt new file mode 100644 index 0000000..3da9d76 --- /dev/null +++ b/js/scripting-lang/tests/16_advanced_functional.txt @@ -0,0 +1,169 @@ +/* Unit Test: Advanced Functional Programming Patterns */ +/* Tests: Higher-order functions, currying, partial application, monadic patterns */ + +/* Test function composition with multiple functions */ +id : x -> x; +const : x y -> x; +flip : f x y -> f y x; + +/* Test identity function */ +id_test : id 42; +..assert id_test = 42; + +/* Test constant function */ +const_test : const 5 10; +..assert const_test = 5; + +/* Test function flipping */ +sub : x y -> x - y; +flipped_sub : flip @sub; +flipped_result : flipped_sub 5 10; +..assert flipped_result = 5; + +/* Test partial application patterns */ +partial1 : f x -> y -> f x y; +partial2 : f x y -> f x y; + +add : x y -> x + y; +add5 : partial1 @add 5; +add5_result : add5 3; +..assert add5_result = 8; + +/* Test function composition with multiple arguments */ +compose2 : f g x y -> f (g x y); +compose3 : f g h x -> f (g (h x)); + +double : x -> x * 2; +square : x -> x * x; +increment : x -> x + 1; + +composed1 : compose2 @double @add 3 4; +composed2 : compose3 @double @square @increment 2; +..assert composed1 = 14; +..assert composed2 = 18; + +/* Test monadic-like patterns with Maybe simulation */ +maybe : x -> case x of + undefined : "Nothing" + null : "Nothing" + _ : "Just " + x; + +maybe_map : f m -> case m of + "Nothing" : "Nothing" + _ : f m; + +maybe_bind : m f -> case m of + "Nothing" : "Nothing" + _ : f m; + +maybe_test1 : maybe undefined; +maybe_test2 : maybe 42; +maybe_test3 : maybe_map @double "Just 5"; +maybe_test4 : maybe_bind "Just 3" @double; + +..assert maybe_test1 = "Nothing"; +..assert maybe_test2 = "Just 42"; +..assert maybe_test3 = "Just 10"; +..assert maybe_test4 = "Just 6"; + +/* Test list-like operations with tables */ +list_map : f table -> { + f table[1], + f table[2], + f table[3] +}; + +list_filter : p table -> case p table[1] of + true : {table[1]} + false : {}; + +list_reduce : f init table -> case table[1] of + undefined : init + _ : f init table[1]; + +test_list : {1, 2, 3}; +mapped_list : list_map @double test_list; +filtered_list : list_filter (x -> x > 1) test_list; +reduced_list : list_reduce @add 0 test_list; + +..assert mapped_list[1] = 2; +..assert mapped_list[2] = 4; +..assert mapped_list[3] = 6; +..assert filtered_list[1] = 2; +..assert reduced_list = 1; + +/* Test point-free style programming */ +pointfree_add : add; +pointfree_double : double; +pointfree_compose : compose @pointfree_double @pointfree_add; + +pointfree_result : pointfree_compose 5 3; +..assert pointfree_result = 16; + +/* Test function memoization pattern */ +memoize : f -> { + cache: {}, + call: x -> case cache[x] of + undefined : cache[x] = f x + _ : cache[x] +}; + +expensive_func : x -> x * x + x + 1; +memoized_func : memoize @expensive_func; + +memo_result1 : memoized_func.call 5; +memo_result2 : memoized_func.call 5; /* Should use cache */ +..assert memo_result1 = 31; +..assert memo_result2 = 31; + +/* Test continuation-passing style (CPS) */ +cps_add : x y k -> k (x + y); +cps_multiply : x y k -> k (x * y); +cps_square : x k -> k (x * x); + +cps_example : cps_add 3 4 (sum -> + cps_multiply sum 2 (product -> + cps_square product (result -> result) + ) +); +..assert cps_example = 98; + +/* Test trampoline pattern for tail recursion simulation */ +trampoline : f -> case f of + f is function : trampoline (f) + _ : f; + +bounce : n -> case n of + 0 : 0 + _ : n + (n - 1); + +trampoline_result : trampoline @bounce 5; +..assert trampoline_result = 15; + +/* Test applicative functor pattern */ +applicative : f x -> case f of + f is function : f x + _ : x; + +applicative_test : applicative @double 5; +..assert applicative_test = 10; + +/* Test function pipelines */ +pipeline : x f1 f2 f3 -> f3 (f2 (f1 x)); +pipeline_result : pipeline 2 @increment @double @square; +..assert pipeline_result = 36; + +/* Test function combinators */ +S : f g x -> f x (g x); +K : x y -> x; +I : x -> x; + +S_test : S @add @double 3; +K_test : K 5 10; +I_test : I 42; + +..assert S_test = 9; +..assert K_test = 5; +..assert I_test = 42; + +..out "Advanced functional programming test completed successfully"; \ No newline at end of file |