2011-06-16 16:55:46 -07:00
|
|
|
|
2011-06-15 12:46:30 -07:00
|
|
|
import option;
|
|
|
|
import option::some;
|
|
|
|
import option::none;
|
|
|
|
|
2011-06-16 16:55:46 -07:00
|
|
|
tag t[T, U] { left(T); right(U); }
|
2011-06-15 12:46:30 -07:00
|
|
|
|
2011-08-12 11:47:44 -07:00
|
|
|
fn either[T, U, V](f_left: &block(&T) -> V, f_right: &block(&U) -> V,
|
|
|
|
value: &t[T, U]) -> V {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt value { left(l) { f_left(l) } right(r) { f_right(r) } }
|
2011-06-15 12:46:30 -07:00
|
|
|
}
|
|
|
|
|
2011-08-04 17:24:54 -07:00
|
|
|
fn lefts[T, U](eithers: &[t[T, U]]) -> [T] {
|
|
|
|
let result: [T] = ~[];
|
2011-08-15 21:54:52 -07:00
|
|
|
for elt: t[T, U] in eithers {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt elt { left(l) { result += ~[l] } _ {/* fallthrough */ } }
|
2011-06-15 12:46:30 -07:00
|
|
|
}
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2011-08-04 17:24:54 -07:00
|
|
|
fn rights[T, U](eithers: &[t[T, U]]) -> [U] {
|
|
|
|
let result: [U] = ~[];
|
2011-08-15 21:54:52 -07:00
|
|
|
for elt: t[T, U] in eithers {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt elt { right(r) { result += ~[r] } _ {/* fallthrough */ } }
|
2011-06-15 12:46:30 -07:00
|
|
|
}
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2011-08-04 17:24:54 -07:00
|
|
|
fn partition[T, U](eithers: &[t[T, U]]) -> {lefts: [T], rights: [U]} {
|
|
|
|
let lefts: [T] = ~[];
|
|
|
|
let rights: [U] = ~[];
|
2011-08-15 21:54:52 -07:00
|
|
|
for elt: t[T, U] in eithers {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt elt { left(l) { lefts += ~[l] } right(r) { rights += ~[r] } }
|
2011-06-15 12:46:30 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ret {lefts: lefts, rights: rights};
|
2011-06-15 12:46:30 -07: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:
|
|
|
|
//
|