2011-06-16 18:55:46 -05:00
|
|
|
|
2011-06-15 14:46:30 -05:00
|
|
|
import option;
|
|
|
|
import option::some;
|
|
|
|
import option::none;
|
|
|
|
|
2011-06-16 18:55:46 -05:00
|
|
|
tag t[T, U] { left(T); right(U); }
|
2011-06-15 14:46:30 -05:00
|
|
|
|
2011-06-16 18:55:46 -05:00
|
|
|
type operator[T, U] = fn(&T) -> U ;
|
2011-06-15 14:46:30 -05:00
|
|
|
|
2011-06-16 18:55:46 -05:00
|
|
|
fn either[T, U,
|
|
|
|
V](&operator[T, V] f_left, &operator[U, V] f_right, &t[T, U] value)
|
|
|
|
-> V {
|
2011-06-15 14:46:30 -05:00
|
|
|
alt (value) {
|
|
|
|
case (left(?l)) { f_left(l) }
|
|
|
|
case (right(?r)) { f_right(r) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-12 16:27:46 -05:00
|
|
|
fn lefts[T, U](&(t[T, U])[] eithers) -> T[] {
|
|
|
|
let T[] result = ~[];
|
2011-06-15 14:46:30 -05:00
|
|
|
for (t[T, U] elt in eithers) {
|
|
|
|
alt (elt) {
|
2011-07-12 16:27:46 -05:00
|
|
|
case (left(?l)) { result += ~[l] }
|
2011-06-16 18:55:46 -05:00
|
|
|
case (_) {/* fallthrough */ }
|
2011-06-15 14:46:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2011-07-12 16:27:46 -05:00
|
|
|
fn rights[T, U](&(t[T, U])[] eithers) -> U[] {
|
|
|
|
let U[] result = ~[];
|
2011-06-15 14:46:30 -05:00
|
|
|
for (t[T, U] elt in eithers) {
|
|
|
|
alt (elt) {
|
2011-07-12 16:27:46 -05:00
|
|
|
case (right(?r)) { result += ~[r] }
|
2011-06-16 18:55:46 -05:00
|
|
|
case (_) {/* fallthrough */ }
|
2011-06-15 14:46:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2011-07-26 07:06:02 -05:00
|
|
|
fn partition[T, U](&(t[T, U])[] eithers)
|
|
|
|
-> rec(T[] lefts, U[] rights) {
|
2011-07-12 16:27:46 -05:00
|
|
|
let T[] lefts = ~[];
|
|
|
|
let U[] rights = ~[];
|
2011-06-15 14:46:30 -05:00
|
|
|
for (t[T, U] elt in eithers) {
|
|
|
|
alt (elt) {
|
2011-07-12 16:27:46 -05:00
|
|
|
case (left(?l)) { lefts += ~[l] }
|
|
|
|
case (right(?r)) { rights += ~[r] }
|
2011-06-15 14:46:30 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-26 07:06:02 -05:00
|
|
|
ret rec(lefts=lefts, rights=rights);
|
2011-06-15 14:46:30 -05:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|
|
|
|
//
|