Add methods iter, iter_err, map, map_err to the result type.

This commit is contained in:
Erick Tryzelaar 2012-05-26 20:33:08 -07:00
parent 46173e98ef
commit e45ed323c9

View File

@ -107,6 +107,79 @@ fn chain_err<T: copy, U: copy, V: copy>(
}
}
#[doc = "
Call a function based on a previous result
If `res` is `ok` then the value is extracted and passed to `op` whereupon
`op`s result is returned. if `res` is `err` then it is immediately returned.
This function can be used to compose the results of two functions.
Example:
iter(read_file(file)) { |buf|
print_buf(buf)
}
"]
fn iter<T, E>(res: result<T, E>, f: fn(T)) {
alt res {
ok(t) { f(t) }
err(_) { }
}
}
#[doc = "
Call a function based on a previous result
If `res` is `err` then the value is extracted and passed to `op` whereupon
`op`s result is returned. if `res` is `ok` then it is immediately returned.
This function can be used to pass through a successful result while handling
an error.
"]
fn iter_err<T, E>(res: result<T, E>, f: fn(E)) {
alt res {
ok(_) { }
err(e) { f(e) }
}
}
#[doc = "
Call a function based on a previous result
If `res` is `ok` then the value is extracted and passed to `op` whereupon
`op`s result is wrapped in `ok` and returned. if `res` is `err` then it is
immediately returned. This function can be used to compose the results of two
functions.
Example:
let res = map(read_file(file)) { |buf|
parse_buf(buf)
}
"]
fn map<T, E: copy, U: copy>(res: result<T, E>, op: fn(T) -> U)
-> result<U, E> {
alt res {
ok(t) { ok(op(t)) }
err(e) { err(e) }
}
}
#[doc = "
Call a function based on a previous result
If `res` is `err` then the value is extracted and passed to `op` whereupon
`op`s result is wrapped in an `err` and returned. if `res` is `ok` then it is
immediately returned. This function can be used to pass through a successful
result while handling an error.
"]
fn map_err<T: copy, E, F: copy>(res: result<T, E>, op: fn(E) -> F)
-> result<T, F> {
alt res {
ok(t) { ok(t) }
err(e) { err(op(e)) }
}
}
impl extensions<T:copy, E:copy> for result<T,E> {
fn get() -> T { get(self) }
@ -123,6 +196,34 @@ impl extensions<T:copy, E:copy> for result<T,E> {
fn chain_err<F:copy>(op: fn(E) -> result<T,F>) -> result<T,F> {
chain_err(self, op)
}
fn iter(f: fn(T)) {
alt self {
ok(t) { f(t) }
err(_) { }
}
}
fn iter_err(f: fn(E)) {
alt self {
ok(_) { }
err(e) { f(e) }
}
}
fn map<U:copy>(op: fn(T) -> U) -> result<U,E> {
alt self {
ok(t) { ok(op(t)) }
err(e) { err(e) }
}
}
fn map_err<F:copy>(op: fn(E) -> F) -> result<T,F> {
alt self {
ok(t) { ok(t) }
err(e) { err(op(e)) }
}
}
}
#[doc = "
@ -248,4 +349,37 @@ mod tests {
fn chain_failure() {
assert get_err(chain(op3(), op2)) == "sadface";
}
#[test]
fn test_impl_iter() {
let mut valid = false;
ok::<str, str>("a").iter { |_x| valid = true; };
assert valid;
err::<str, str>("b").iter { |_x| valid = false; };
assert valid;
}
#[test]
fn test_impl_iter_err() {
let mut valid = true;
ok::<str, str>("a").iter_err { |_x| valid = false; };
assert valid;
valid = false;
err::<str, str>("b").iter_err { |_x| valid = true; };
assert valid;
}
#[test]
fn test_impl_map() {
assert ok::<str, str>("a").map { |_x| "b" } == ok("b");
assert err::<str, str>("a").map { |_x| "b" } == err("a");
}
#[test]
fn test_impl_map_err() {
assert ok::<str, str>("a").map_err { |_x| "b" } == ok("a");
assert err::<str, str>("a").map_err { |_x| "b" } == err("b");
}
}