blob: 75fda40fd438482db20776759e335151a1ccad28 (
plain) (
blame)
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
|
/* 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";
|