diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2013-05-05 15:04:19 -0400 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2013-05-06 02:21:01 -0400 |
| commit | 44c1e46ef5bb78b61e4aec28c0b8cef312404d18 (patch) | |
| tree | 4b2cc0e465b95a5309cbddcb27add290c4a6a68d /src | |
| parent | 6e6a4be19d8e6a2cedc66be6cc602db8a1e71acd (diff) | |
| download | rust-44c1e46ef5bb78b61e4aec28c0b8cef312404d18.tar.gz rust-44c1e46ef5bb78b61e4aec28c0b8cef312404d18.zip | |
Improve lifetimes on foldl/foldr and lift the Copy requirement
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/vec.rs | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index 6ffb0b30917..53124d13024 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -991,7 +991,7 @@ pub fn connect<T:Copy>(v: &[~[T]], sep: &T) -> ~[T] { * ~~~ * */ -pub fn foldl<T, U>(z: T, v: &[U], p: &fn(t: T, u: &U) -> T) -> T { +pub fn foldl<'a, T, U>(z: T, v: &'a [U], p: &fn(t: T, u: &'a U) -> T) -> T { let mut accum = z; let mut i = 0; let l = v.len(); @@ -1023,12 +1023,13 @@ pub fn foldl<T, U>(z: T, v: &[U], p: &fn(t: T, u: &U) -> T) -> T { * ~~~ * */ -pub fn foldr<T, U: Copy>(v: &[T], z: U, p: &fn(t: &T, u: U) -> U) -> U { - let mut accum = z; - for v.each_reverse |elt| { - accum = p(elt, accum); +pub fn foldr<'a, T, U>(v: &'a [T], mut z: U, p: &fn(t: &'a T, u: U) -> U) -> U { + let mut i = v.len(); + while i > 0 { + i -= 1; + z = p(&v[i], z); } - accum + return z; } /** @@ -1851,7 +1852,7 @@ pub trait ImmutableVector<'self, T> { fn last_opt(&self) -> Option<&'self T>; fn each_reverse(&self, blk: &fn(&T) -> bool); fn eachi_reverse(&self, blk: &fn(uint, &T) -> bool); - fn foldr<U: Copy>(&self, z: U, p: &fn(t: &T, u: U) -> U) -> U; + fn foldr<'a, U>(&'a self, z: U, p: &fn(t: &'a T, u: U) -> U) -> U; fn map<U>(&self, f: &fn(t: &T) -> U) -> ~[U]; fn mapi<U>(&self, f: &fn(uint, t: &T) -> U) -> ~[U]; fn map_r<U>(&self, f: &fn(x: &T) -> U) -> ~[U]; @@ -1924,7 +1925,7 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] { /// Reduce a vector from right to left #[inline] - fn foldr<U:Copy>(&self, z: U, p: &fn(t: &T, u: U) -> U) -> U { + fn foldr<'a, U>(&'a self, z: U, p: &fn(t: &'a T, u: U) -> U) -> U { foldr(*self, z, p) } |
