Skip to content

Commit

Permalink
Refactored list::tail() 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 fed034c commit 65f1993
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/libcollections/list.rs
Expand Up @@ -61,6 +61,14 @@ impl<T> List<T> {
Cons(ref head, _) => Some(head)
}
}

/// Returns all but the first element of a list
pub fn tail(&self) -> Option<@List<T>> {
match *self {
Nil => None,
Cons(_, tail) => Some(tail)
}
}
}

impl<T> Container for List<T> {
Expand All @@ -78,14 +86,6 @@ impl<T:Eq> List<T> {
}
}

/// Returns all but the first element of a list
pub fn tail<T>(list: @List<T>) -> @List<T> {
match *list {
Cons(_, tail) => return tail,
Nil => fail!("list empty")
}
}

/// Appends one list to another
pub fn append<T:Clone + 'static>(list: @List<T>, other: @List<T>) -> @List<T> {
match *list {
Expand Down Expand Up @@ -117,7 +117,7 @@ fn push<T:Clone>(ll: &mut @list<T>, vv: T) {

#[cfg(test)]
mod tests {
use list::{List, Nil, tail};
use list::{List, Nil};
use list;

#[test]
Expand All @@ -143,13 +143,13 @@ mod tests {

#[test]
fn test_from_vec() {
let list = @List::from_vec([0, 1, 2]);
let list = List::from_vec([0, 1, 2]);
assert_eq!(list.head().unwrap(), &0);

let mut tail = tail(list);
let mut tail = list.tail().unwrap();
assert_eq!(tail.head().unwrap(), &1);

tail = tail(tail);
tail = tail.tail().unwrap();
assert_eq!(tail.head().unwrap(), &2);
}

Expand Down

0 comments on commit 65f1993

Please sign in to comment.