-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlambdas.ae
More file actions
29 lines (21 loc) · 918 Bytes
/
lambdas.ae
File metadata and controls
29 lines (21 loc) · 918 Bytes
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
package main
extern function printf(String format, ...) -> I32
function apply(Callable<I64, <I64>> operation, I64 value) -> I64:
return operation(value)
function applyBinary(Callable<I64, <I64, I64>> operation, I64 a, I64 b) -> I64:
return operation(a, b)
public function main() -> I32:
printf("=== Lambda basics ===\n")
Callable<I64, <I64>> doubler = lambda I64 x: x * 2
I64 result = doubler(21)
printf("doubler(21) = %lld\n", result)
printf("\n=== Higher-order functions ===\n")
I64 squared = apply(lambda I64 x: x * x, 5)
printf("apply(square, 5) = %lld\n", squared)
I64 sum = applyBinary(lambda I64 a, I64 b: a + b, 10, 20)
printf("applyBinary(add, 10, 20) = %lld\n", sum)
printf("\n=== Closure capture ===\n")
I64 factor = 3
Callable<I64, <I64>> multiplier = lambda I64 x: x * factor
printf("multiplier(7) = %lld\n", multiplier(7))
return 0