closures
This commit is contained in:
95
examples/35-closures.sx
Normal file
95
examples/35-closures.sx
Normal file
@@ -0,0 +1,95 @@
|
||||
#import "modules/std.sx";
|
||||
|
||||
// --- Closure Basics ---
|
||||
|
||||
// Factory: returns a new closure each time
|
||||
make_adder :: (n: s64) -> Closure(s64) -> s64 {
|
||||
return closure((x: s64) -> s64 => x + n);
|
||||
}
|
||||
|
||||
// Higher-order function: accepts any Closure(s64) -> s64
|
||||
apply :: (f: Closure(s64) -> s64, x: s64) -> s64 { return f(x); }
|
||||
|
||||
// Reduce: fold over a slice with a closure
|
||||
reduce :: (arr: []s64, f: Closure(s64, s64) -> s64, init: s64) -> s64 {
|
||||
acc := init;
|
||||
i : s64 = 0;
|
||||
while i < arr.len { acc = f(acc, arr[i]); i += 1; }
|
||||
return acc;
|
||||
}
|
||||
|
||||
// Auto-promoted bare function
|
||||
triple :: (x: s64) -> s64 { return x * 3; }
|
||||
|
||||
// Struct with optional closure callback
|
||||
Widget :: struct {
|
||||
name: string;
|
||||
on_update: ?Closure(s64) -> void;
|
||||
}
|
||||
|
||||
main :: () {
|
||||
// 1. Basic closure with capture
|
||||
offset := 100;
|
||||
add_offset := closure((x: s64) -> s64 => x + offset);
|
||||
print("basic: {}\n", add_offset(42));
|
||||
|
||||
// 2. Capture by value (snapshot semantics)
|
||||
n := 10;
|
||||
snap := closure((x: s64) -> s64 => x + n);
|
||||
n = 999;
|
||||
print("snapshot: {}\n", snap(5));
|
||||
|
||||
// 3. Block-body closure with control flow
|
||||
clamp := closure((x: s64) -> s64 {
|
||||
if x < 0 { return 0; }
|
||||
if x > 100 { return 100; }
|
||||
return x;
|
||||
});
|
||||
print("clamp: {} {} {}\n", clamp(50), clamp(0 - 10), clamp(200));
|
||||
|
||||
// 4. Void closure with string capture
|
||||
tag := "INFO";
|
||||
logger := closure((msg: string) {
|
||||
print("[{}] {}\n", tag, msg);
|
||||
});
|
||||
logger("system ready");
|
||||
|
||||
// 5. Factory pattern
|
||||
add5 := make_adder(5);
|
||||
add10 := make_adder(10);
|
||||
print("factory: {} {}\n", add5(100), add10(100));
|
||||
|
||||
// 6. Auto-promotion: bare fn passed where Closure expected
|
||||
print("auto-promote: {}\n", apply(triple, 7));
|
||||
|
||||
// 7. Closure passed to higher-order function
|
||||
factor := 4;
|
||||
print("hof: {}\n", apply(closure((x: s64) -> s64 => x * factor), 10));
|
||||
|
||||
// 8. Reduce with closure
|
||||
nums : []s64 = .[1, 2, 3, 4, 5];
|
||||
total := reduce(nums, closure((acc: s64, x: s64) -> s64 => acc + x), 0);
|
||||
print("reduce: {}\n", total);
|
||||
|
||||
// 9. Closure captures closure
|
||||
inner := closure((x: s64) -> s64 => x + 10);
|
||||
outer := closure((x: s64) -> s64 => inner(x) * 2);
|
||||
print("compose: {}\n", outer(5));
|
||||
|
||||
// 10. Multiple closures from same scope
|
||||
base := 100;
|
||||
cl_add := closure((x: s64) -> s64 => x + base);
|
||||
cl_mul := closure((x: s64) -> s64 => x * base);
|
||||
print("multi: {} {}\n", cl_add(5), cl_mul(5));
|
||||
|
||||
// 11. Optional closures
|
||||
w1 := Widget.{ name = "slider", on_update = closure((val: s64) {
|
||||
print("widget: {} = {}\n", "slider", val);
|
||||
}) };
|
||||
w2 := Widget.{ name = "label", on_update = null };
|
||||
|
||||
if h := w1.on_update { h(42); }
|
||||
if h := w2.on_update { h(0); } else { print("widget: no handler\n"); }
|
||||
|
||||
print("=== DONE ===\n");
|
||||
}
|
||||
Reference in New Issue
Block a user