From d68190706448b5a1ca09be690f360c08a7a4f831 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 22:40:57 -0400 Subject: [PATCH] Removed list::find() in favor of iter().find() --- src/libcollections/list.rs | 41 ++++++++++---------------------------- 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index ce6084923f9..459b0a78a1e 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -53,26 +53,6 @@ impl List { } } -/** - * Search for an element that matches a given predicate - * - * Apply function `f` to each element of `list`, starting from the first. - * When function `f` returns true then an option containing the element - * is returned. If `f` matches no elements then none is returned. - */ -pub fn find(list: @List, f: |&T| -> bool) -> Option { - let mut list = list; - loop { - list = match *list { - Cons(ref head, tail) => { - if f(head) { return Some((*head).clone()); } - tail - } - Nil => return None - } - }; -} - /** * Returns true if a list contains an element that matches a given predicate * @@ -196,8 +176,6 @@ mod tests { use list::{List, Nil, head, is_empty, tail}; use list; - use std::option; - #[test] fn test_iter() { let list = List::from_vec([0, 1, 2]); @@ -254,18 +232,21 @@ mod tests { #[test] fn test_find_success() { - fn match_(i: &int) -> bool { return *i == 2; } - let list = @List::from_vec([0, 1, 2]); - assert_eq!(list::find(list, match_), option::Some(2)); + fn match_(i: & &int) -> bool { **i == 2 } + + let list = List::from_vec([0, 1, 2]); + assert_eq!(list.iter().find(match_).unwrap(), &2); } #[test] fn test_find_fail() { - fn match_(_i: &int) -> bool { return false; } - let list = @List::from_vec([0, 1, 2]); - let empty = @list::Nil::; - assert_eq!(list::find(list, match_), option::None::); - assert_eq!(list::find(empty, match_), option::None::); + fn match_(_i: & &int) -> bool { false } + + let empty = Nil::; + assert_eq!(empty.iter().find(match_), None); + + let list = List::from_vec([0, 1, 2]); + assert_eq!(list.iter().find(match_), None); } #[test]