Skip to content

Commit

Permalink
Replaced list::has() with list::contains()
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Feb 27, 2014
1 parent 223f309 commit 45fd63a
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/libcollections/list.rs
Expand Up @@ -63,13 +63,11 @@ impl<T> Container for List<T> {
fn is_empty(&self) -> bool { match *self { Nil => true, _ => false } }
}

/// Returns true if a list contains an element with the given value
pub fn has<T:Eq>(list: @List<T>, element: T) -> bool {
let mut found = false;
each(list, |e| {
if *e == element { found = true; false } else { true }
});
return found;
impl<T:Eq> List<T> {
/// Returns true if a list contains an element with the given value
pub fn contains(&self, element: T) -> bool {
self.iter().any(|list_element| *list_element == element)
}
}

/// Returns all but the first element of a list
Expand Down Expand Up @@ -208,13 +206,14 @@ mod tests {
}

#[test]
fn test_has() {
let list = @List::from_vec([5, 8, 6]);
let empty = @list::Nil::<int>;
assert!((list::has(list, 5)));
assert!((!list::has(list, 7)));
assert!((list::has(list, 8)));
assert!((!list::has(empty, 5)));
fn test_contains() {
let empty = Nil::<int>;
assert!((!empty.contains(5)));

let list = List::from_vec([5, 8, 6]);
assert!((list.contains(5)));
assert!((!list.contains(7)));
assert!((list.contains(8)));
}

#[test]
Expand Down

0 comments on commit 45fd63a

Please sign in to comment.