about summary refs log tree commit diff stats
path: root/js/scripting-lang/tests/08_first_class_functions.txt
diff options
context:
space:
mode:
Diffstat (limited to 'js/scripting-lang/tests/08_first_class_functions.txt')
-rw-r--r--js/scripting-lang/tests/08_first_class_functions.txt51
1 files changed, 51 insertions, 0 deletions
diff --git a/js/scripting-lang/tests/08_first_class_functions.txt b/js/scripting-lang/tests/08_first_class_functions.txt
new file mode 100644
index 0000000..75fda40
--- /dev/null
+++ b/js/scripting-lang/tests/08_first_class_functions.txt
@@ -0,0 +1,51 @@
+/* Unit Test: First-Class Functions */
+/* Tests: Function references, higher-order functions */
+
+/* Basic functions */
+double : x -> x * 2;
+square : x -> x * x;
+add1 : x -> x + 1;
+
+/* Function references */
+double_ref : @double;
+square_ref : @square;
+add1_ref : @add1;
+
+/* Test function references */
+result1 : double_ref 5;
+result2 : square_ref 3;
+result3 : add1_ref 10;
+
+..assert result1 = 10;
+..assert result2 = 9;
+..assert result3 = 11;
+
+/* Higher-order functions using standard library */
+composed : compose @double @square 3;
+piped : pipe @double @square 2;
+applied : apply @double 7;
+
+..assert composed = 18;
+..assert piped = 16;
+..assert applied = 14;
+
+/* Function references in case expressions */
+getFunction : type -> 
+  when type is
+    "double" then @double
+    "square" then @square
+    _        then @add1;
+
+func1 : getFunction "double";
+func2 : getFunction "square";
+func3 : getFunction "unknown";
+
+result4 : func1 4;
+result5 : func2 4;
+result6 : func3 4;
+
+..assert result4 = 8;
+..assert result5 = 16;
+..assert result6 = 5;
+
+..out "First-class functions test completed"; 
\ No newline at end of file