Skip to content

Commit

Permalink
Refactored list::append() to be based on List<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Feb 27, 2014
1 parent 65f1993 commit 1c27c90
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/libcollections/list.rs
Expand Up @@ -86,17 +86,6 @@ impl<T:Eq> List<T> {
}
}

/// Appends one list to another
pub fn append<T:Clone + 'static>(list: @List<T>, other: @List<T>) -> @List<T> {
match *list {
Nil => return other,
Cons(ref head, tail) => {
let rest = append(tail, other);
return @Cons((*head).clone(), rest);
}
}
}

impl<T:'static + Clone> List<T> {
/// Create a list from a vector
pub fn from_vec(v: &[T]) -> List<T> {
Expand All @@ -105,6 +94,17 @@ impl<T:'static + Clone> List<T> {
_ => v.rev_iter().fold(Nil, |tail, value: &T| Cons(value.clone(), @tail))
}
}

/// Appends one list to another, returning a new list
pub fn append(&self, other: List<T>) -> List<T> {
match other {
Nil => return self.clone(),
_ => match *self {
Nil => return other,
Cons(ref value, tail) => Cons(value.clone(), @tail.append(other))
}
}
}
}

/*
Expand Down Expand Up @@ -225,7 +225,7 @@ mod tests {

#[test]
fn test_append() {
assert!(@List::from_vec([1,2,3,4])
== list::append(@List::from_vec([1,2]), @List::from_vec([3,4])));
assert_eq!(List::from_vec([1, 2, 3, 4]),
List::from_vec([1, 2]).append(List::from_vec([3, 4])));
}
}

0 comments on commit 1c27c90

Please sign in to comment.