Initial Commit
This commit is contained in:
789 files changed
+149185
No files matched your search
@@ -0,0 +1,60 @@
|
||||
import std::io;
|
||||
trait Show { def show(this: &Show): string; }
|
||||
struct point: Show { var x: int; var y: int; }
|
||||
def point::show(this: &point): string { return $"({this.x}, {this.y})"; }
|
||||
struct box<T>: Show where T: Show { var value: T; }
|
||||
def box<T>::show(this: &box<T>): string { return $"[{this.value.show()}]"; }
|
||||
def describe(item: &Show): string { return item.show(); }
|
||||
def show_all<T>(xs: span<T>): string where T: Show {
|
||||
var out := string::new(nothing {});
|
||||
for x in xs { out.append($"{x.show()}{describe(&x)} "); }
|
||||
return out;
|
||||
}
|
||||
union res<T> { var value: T; var failure: span<u8>; }
|
||||
def res<T>::ok(value: T): res<T> { return res::<T>{ value = value }; }
|
||||
def res<T>::or(this: &res<T>, fallback: T): T {
|
||||
match this { value(value) => return value; else => return fallback; }
|
||||
}
|
||||
struct pair<A, B> { var first: A; var second: B; }
|
||||
def pair<A, B>::swap(this: &pair<A, B>): pair<B, A> { return pair::<B, A>{ this.second, this.first }; }
|
||||
def map<T, U>(xs: span<T>, out: *mut U, f: def(T): U): span<U> {
|
||||
for i in 0..xs.length { out[i] = f(xs[i]); }
|
||||
return span::<U>::over(out as *U, xs.length);
|
||||
}
|
||||
def fold<T, A>(xs: span<T>, start: A, f: def(A, T): A): A {
|
||||
var acc := start;
|
||||
for x in xs { acc = f(acc, x); }
|
||||
return acc;
|
||||
}
|
||||
def max<T>(a: T, b: T): T where T: ordered { return a > b ? a : b; }
|
||||
def sum<T>(xs: span<T>): T where T: numeric {
|
||||
var total: T = 0;
|
||||
for x in xs { total += x; }
|
||||
return total;
|
||||
}
|
||||
struct node<T> { var value: T; var next: *node<T>; }
|
||||
def node<T>::length(this: &node<T>): int { return this.next == null ? 1 : 1 + (*this.next).length(); }
|
||||
def main(): void {
|
||||
val p := point{ 1, 2 };
|
||||
val b := box{ value = p };
|
||||
var ps: point[1] = [p];
|
||||
var bs: box<point>[1] = [b];
|
||||
print($"{show_all(span::<point>::over((&ps) as *point, 1))}{describe(&b)} {show_all(span::<box<point>>::over((&bs) as *box<point>, 1))}");
|
||||
val r := res::<int>::ok(3);
|
||||
val e := res::<int>{ failure = "no" };
|
||||
print($"{r.or(0)} {e.or(-1)} ");
|
||||
val q := pair::<int, span<u8>>{ first = 1, second = "one" };
|
||||
val nested: pair<pair<int, span<u8>>, int> = pair{ q, 2 };
|
||||
print($"{q.swap()} {nested.first.second} ");
|
||||
var ns: int[2] = [1, 2];
|
||||
var tripled: int[2];
|
||||
var fs: f64[2] = [1.5, 2.5];
|
||||
print($"{map(span::<int>::over((&ns) as *int, 2), (&mut tripled) as *mut int, def(n) => n * 3)} {fold(span::<f64>::over((&fs) as *f64, 2), 0.0, def(a, x) => a + x)} ");
|
||||
var threes: int[3] = [1, 2, 3];
|
||||
var halves: f64[2] = [0.5, 0.25];
|
||||
print($"{max(3, 7)} {max::<u8>(250, 9)} {sum(span::<int>::over((&threes) as *int, 3))} {sum(span::<f64>::over((&halves) as *f64, 2))} ");
|
||||
val last := node{ 3, null };
|
||||
val first := node{ 1, &last as *node<int> };
|
||||
val biggest := max::<f32>;
|
||||
println($"{first.length()} {biggest(0.5, 1.5)}");
|
||||
}
|
||||
Reference in new issue
Block a user