@@ -58,6 +58,9 @@ impl<T> List<T> {
58
58
impl < T > Container for List < T > {
59
59
/// Returns the length of a list
60
60
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 } }
61
64
}
62
65
63
66
/// 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 {
69
72
return found;
70
73
}
71
74
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
-
80
75
/// Returns all but the first element of a list
81
76
pub fn tail < T > ( list : @List < T > ) -> @List < T > {
82
77
match * list {
@@ -153,7 +148,7 @@ pub fn each<T>(list: @List<T>, f: |&T| -> bool) -> bool {
153
148
154
149
#[ cfg( test) ]
155
150
mod tests {
156
- use list:: { List , Nil , head, is_empty , tail} ;
151
+ use list:: { List , Nil , head, tail} ;
157
152
use list;
158
153
159
154
#[ test]
@@ -168,13 +163,13 @@ mod tests {
168
163
169
164
#[ test]
170
165
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' ] ) ;
174
169
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( ) ) ;
178
173
}
179
174
180
175
#[ test]
0 commit comments