diff options
| author | Bruno de Oliveira Abinader <bruno.d@partner.samsung.com> | 2014-02-25 01:06:54 -0400 |
|---|---|---|
| committer | Bruno de Oliveira Abinader <bruno.d@partner.samsung.com> | 2014-02-27 08:35:47 -0400 |
| commit | a14d72d49e4d87b970f899a761dbdfce63239b79 (patch) | |
| tree | 4531618e6d60762c218dcee71334cf9d2d2f5d0b /src | |
| parent | e589fcffcc8da46b5949d15284a466d9ed27a003 (diff) | |
| download | rust-a14d72d49e4d87b970f899a761dbdfce63239b79.tar.gz rust-a14d72d49e4d87b970f899a761dbdfce63239b79.zip | |
Implemented list::is_empty() based on Container trait
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcollections/list.rs | 25 | ||||
| -rw-r--r-- | src/test/run-pass/non-boolean-pure-fns.rs | 4 |
2 files changed, 12 insertions, 17 deletions
diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index 9223669c787..1239ce70220 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -58,6 +58,9 @@ impl<T> List<T> { impl<T> Container for List<T> { /// 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<T:Eq>(list: @List<T>, element: T) -> bool { return found; } -/// Returns true if the list is empty -pub fn is_empty<T>(list: @List<T>) -> bool { - match *list { - Nil => true, - _ => false - } -} - /// Returns all but the first element of a list pub fn tail<T>(list: @List<T>) -> @List<T> { match *list { @@ -153,7 +148,7 @@ pub fn each<T>(list: @List<T>, 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<int> = @List::from_vec([]); - let full1 = @List::from_vec([1]); - let full2 = @List::from_vec(['r', 'u']); + let empty : list::List<int> = 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 cb08c81d9e0..9cbf80c3105 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<T:Clone>(ls: @List<T>, acc: uint) -> uint { match *ls { Nil => { acc } Cons(_, tl) => { pure_length_go(tl, acc + 1u) } } @@ -25,7 +25,7 @@ fn pure_length<T:Clone>(ls: @List<T>) -> uint { pure_length_go(ls, 0u) } fn nonempty_list<T:Clone>(ls: @List<T>) -> bool { pure_length(ls) > 0u } fn safe_head<T:Clone>(ls: @List<T>) -> T { - assert!(!is_empty(ls)); + assert!(!ls.is_empty()); return head(ls); } |
