diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index 9223669c7876..1239ce702204 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -58,6 +58,9 @@ impl List { impl Container for List { /// Returns the length of a list fn len(&self) -> uint { self.iter().len() } + + /// Returns true if the list is empty + fn is_empty(&self) -> bool { match *self { Nil => true, _ => false } } } /// Returns true if a list contains an element with the given value @@ -69,14 +72,6 @@ pub fn has(list: @List, element: T) -> bool { return found; } -/// Returns true if the list is empty -pub fn is_empty(list: @List) -> bool { - match *list { - Nil => true, - _ => false - } -} - /// Returns all but the first element of a list pub fn tail(list: @List) -> @List { match *list { @@ -153,7 +148,7 @@ pub fn each(list: @List, f: |&T| -> bool) -> bool { #[cfg(test)] mod tests { - use list::{List, Nil, head, is_empty, tail}; + use list::{List, Nil, head, tail}; use list; #[test] @@ -168,13 +163,13 @@ mod tests { #[test] fn test_is_empty() { - let empty : @list::List = @List::from_vec([]); - let full1 = @List::from_vec([1]); - let full2 = @List::from_vec(['r', 'u']); + let empty : list::List = List::from_vec([]); + let full1 = List::from_vec([1]); + let full2 = List::from_vec(['r', 'u']); - assert!(is_empty(empty)); - assert!(!is_empty(full1)); - assert!(!is_empty(full2)); + assert!(empty.is_empty()); + assert!(!full1.is_empty()); + assert!(!full2.is_empty()); } #[test] diff --git a/src/test/run-pass/non-boolean-pure-fns.rs b/src/test/run-pass/non-boolean-pure-fns.rs index cb08c81d9e0e..9cbf80c31054 100644 --- a/src/test/run-pass/non-boolean-pure-fns.rs +++ b/src/test/run-pass/non-boolean-pure-fns.rs @@ -14,7 +14,7 @@ extern crate collections; -use collections::list::{List, Cons, Nil, head, is_empty}; +use collections::list::{List, Cons, Nil, head}; fn pure_length_go(ls: @List, acc: uint) -> uint { match *ls { Nil => { acc } Cons(_, tl) => { pure_length_go(tl, acc + 1u) } } @@ -25,7 +25,7 @@ fn pure_length(ls: @List) -> uint { pure_length_go(ls, 0u) } fn nonempty_list(ls: @List) -> bool { pure_length(ls) > 0u } fn safe_head(ls: @List) -> T { - assert!(!is_empty(ls)); + assert!(!ls.is_empty()); return head(ls); }