Skip to content

Commit a14d72d

Browse files
committed
Implemented list::is_empty() based on Container trait
1 parent e589fcf commit a14d72d

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

src/libcollections/list.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ impl<T> List<T> {
5858
impl<T> Container for List<T> {
5959
/// Returns the length of a list
6060
fn len(&self) -> uint { self.iter().len() }
61+
62+
/// Returns true if the list is empty
63+
fn is_empty(&self) -> bool { match *self { Nil => true, _ => false } }
6164
}
6265

6366
/// 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 {
6972
return found;
7073
}
7174

72-
/// Returns true if the list is empty
73-
pub fn is_empty<T>(list: @List<T>) -> bool {
74-
match *list {
75-
Nil => true,
76-
_ => false
77-
}
78-
}
79-
8075
/// Returns all but the first element of a list
8176
pub fn tail<T>(list: @List<T>) -> @List<T> {
8277
match *list {
@@ -153,7 +148,7 @@ pub fn each<T>(list: @List<T>, f: |&T| -> bool) -> bool {
153148

154149
#[cfg(test)]
155150
mod tests {
156-
use list::{List, Nil, head, is_empty, tail};
151+
use list::{List, Nil, head, tail};
157152
use list;
158153

159154
#[test]
@@ -168,13 +163,13 @@ mod tests {
168163

169164
#[test]
170165
fn test_is_empty() {
171-
let empty : @list::List<int> = @List::from_vec([]);
172-
let full1 = @List::from_vec([1]);
173-
let full2 = @List::from_vec(['r', 'u']);
166+
let empty : list::List<int> = List::from_vec([]);
167+
let full1 = List::from_vec([1]);
168+
let full2 = List::from_vec(['r', 'u']);
174169

175-
assert!(is_empty(empty));
176-
assert!(!is_empty(full1));
177-
assert!(!is_empty(full2));
170+
assert!(empty.is_empty());
171+
assert!(!full1.is_empty());
172+
assert!(!full2.is_empty());
178173
}
179174

180175
#[test]

src/test/run-pass/non-boolean-pure-fns.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
extern crate collections;
1616

17-
use collections::list::{List, Cons, Nil, head, is_empty};
17+
use collections::list::{List, Cons, Nil, head};
1818

1919
fn pure_length_go<T:Clone>(ls: @List<T>, acc: uint) -> uint {
2020
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) }
2525
fn nonempty_list<T:Clone>(ls: @List<T>) -> bool { pure_length(ls) > 0u }
2626

2727
fn safe_head<T:Clone>(ls: @List<T>) -> T {
28-
assert!(!is_empty(ls));
28+
assert!(!ls.is_empty());
2929
return head(ls);
3030
}
3131

0 commit comments

Comments
 (0)