31 lines
787 B
Plaintext
31 lines
787 B
Plaintext
import std::io;
|
|
|
|
def sum(xs: i64...): i64 {
|
|
var total: i64 = 0;
|
|
for x in xs { total += x; }
|
|
return total;
|
|
}
|
|
|
|
def joined(head: strview, rest: strview...): string {
|
|
var out: string = head;
|
|
for each in rest { out.append(","); out.append(each); }
|
|
return out;
|
|
}
|
|
|
|
# A variadic function type is written with `...` after its last parameter type.
|
|
def twice(f: def(i64...): i64, xs: span<i64>): i64 { return f(...xs) * 2; }
|
|
|
|
def main(): void {
|
|
val f: def(i64...): i64 = sum;
|
|
println($"{f()} {f(1)} {f(1, 2, 3)}");
|
|
val numbers: i64[4] = [4, 5, 6, 7];
|
|
println($"{f(...numbers)} {f(...numbers[1:2])}");
|
|
println($"{twice(sum, numbers[:])}");
|
|
|
|
val g := joined;
|
|
println(g("a"));
|
|
println(g("a", "b", "c"));
|
|
val words: strview[2] = ["x", "y"];
|
|
println(g("w", ...words));
|
|
}
|