diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2012-03-22 20:06:01 -0700 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2012-03-23 21:47:28 -0700 |
| commit | 042c532a084fa3871a3a2d8955ff82c246db8015 (patch) | |
| tree | 9b69f961a78deae15a4f8544bec7a4f215a90a5e /src/libcore/result.rs | |
| parent | 40443768b16747cb3ef90a2fb2fd9e7f317ff383 (diff) | |
| download | rust-042c532a084fa3871a3a2d8955ff82c246db8015.tar.gz rust-042c532a084fa3871a3a2d8955ff82c246db8015.zip | |
Implement new inference algorithm.
Diffstat (limited to 'src/libcore/result.rs')
| -rw-r--r-- | src/libcore/result.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 3e040c14a0a..37a55e97aa1 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -91,6 +91,24 @@ fn chain<T, U: copy, V: copy>(res: result<T, V>, op: fn(T) -> result<U, V>) } } +#[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 chain_err<T: copy, U: copy, V: copy>( + res: result<T, V>, + op: fn(V) -> result<T, U>) + -> result<T, U> { + alt res { + ok(t) { ok(t) } + err(v) { op(v) } + } +} + // ______________________________________________________________________ // Note: // @@ -171,6 +189,22 @@ fn map2<S,T,U:copy,V:copy,W>(ss: [S], ts: [T], ret nxt(vs); } +fn iter2<S,T,U:copy>(ss: [S], ts: [T], + op: fn(S,T) -> result<(),U>) + : vec::same_length(ss, ts) + -> result<(),U> { + let n = vec::len(ts); + let mut i = 0u; + while i < n { + alt op(ss[i],ts[i]) { + ok(()) { } + err(u) { ret err(u); } + } + i += 1u; + } + ret ok(()); +} + #[cfg(test)] mod tests { fn op1() -> result::result<int, str> { result::ok(666) } |
