Rename all example tests/companions to the XXXX-category-test-name scheme (per-category 100-blocks: basic 0010, types 0100, ... errors 1000, diagnostics 1100, ffi 1200, ffi-objc 1300, ffi-jni 1400, vectors 1500, platform 1600). Companions and dir/C fixtures move in lockstep with their parent test; #import/#source/#include paths rewritten to match. Expected output now lives in examples/expected/ (a sibling dir of the tests) split into three streams per the new convention: <name>.exit / <name>.stdout / <name>.stderr (+ optional <name>.ir) run_examples.sh rewritten: scans examples/ and issues/ for an expected/<name>.exit marker, captures stdout and stderr separately (no more 2>&1), compares each stream + exit + optional IR snapshot. Behavior validated unchanged: every renamed test reproduces its prior merged output + exit (diffs limited to file paths/basenames embedded in diagnostics + traces, which correctly reflect the new names). Suite: 292 passed, 0 failed. 50-smoke.sx split + issue relocation + docs follow in subsequent commits.
97 lines
2.8 KiB
Plaintext
97 lines
2.8 KiB
Plaintext
#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");
|
|
}
|