Initial Commit
This commit is contained in:
782 files changed
+145156
No files matched your search
@@ -0,0 +1,30 @@
|
||||
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));
|
||||
}
|
||||
Reference in new issue
Block a user