Skip to content

Improve lifetimes on foldl/foldr and lift the Copy requirement #6254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/libcore/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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)
}

Expand Down