diff options
| author | Brian Anderson <banderson@mozilla.com> | 2011-10-28 18:09:48 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2011-10-29 01:25:11 -0700 |
| commit | 2b62a80202e2855d47f20d271842e0e9aec6d8e2 (patch) | |
| tree | a37878693d9c0aa58bfa63a3eed957f4502811ba /src/lib | |
| parent | c1092fb6d88efe51e42df3aae2a321cc669e12a0 (diff) | |
stdlib: Add result::chain for composing results
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/result.rs | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/src/lib/result.rs b/src/lib/result.rs index ba5ead5d4db..8b07138d8d0 100644 --- a/src/lib/result.rs +++ b/src/lib/result.rs @@ -19,11 +19,11 @@ tag t<T, U> { */ ok(T); /* - Variant: error + Variant: err Contains the error value */ - error(U); + err(U); } /* Section: Operations */ @@ -40,14 +40,14 @@ If the result is an error fn get<T, U>(res: t<T, U>) -> T { alt res { ok(t) { t } - error(_) { + err(_) { fail "get called on error result"; } } } /* -Function: get +Function: get_err Get the value out of an error result @@ -55,9 +55,9 @@ Failure: If the result is not an error */ -fn get_error<T, U>(res: t<T, U>) -> U { +fn get_err<T, U>(res: t<T, U>) -> U { alt res { - error(u) { u } + err(u) { u } ok(_) { fail "get_error called on ok result"; } @@ -72,7 +72,7 @@ Returns true if the result is <ok> fn success<T, U>(res: t<T, U>) -> bool { alt res { ok(_) { true } - error(_) { false } + err(_) { false } } } @@ -83,4 +83,27 @@ Returns true if the result is <error> */ fn failure<T, U>(res: t<T, U>) -> bool { !success(res) -} \ No newline at end of file +} + +/* +Function: chain + +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: + +> let res = chain(read_file(file), { |buf] +> ok(parse_buf(buf)) +> }) + +*/ +fn chain<T, U, V>(res: t<T, V>, op: block(T) -> t<U, V>) -> t<U, V> { + alt res { + ok(t) { op(t) } + err(e) { err(e) } + } +} |
