diff options
| author | Patrick Walton <pcwalton@mimiga.net> | 2012-03-12 18:26:31 -0700 |
|---|---|---|
| committer | Patrick Walton <pcwalton@mimiga.net> | 2012-03-12 18:28:16 -0700 |
| commit | 4571175568bcce1544d7c6da5e38841cb2377735 (patch) | |
| tree | 940ac3acbad49545612e57c588548388568ccbc2 /src/libstd | |
| parent | 3de30f4ef2f535b4cec24bba6166b8275d8400af (diff) | |
| download | rust-4571175568bcce1544d7c6da5e38841cb2377735.tar.gz rust-4571175568bcce1544d7c6da5e38841cb2377735.zip | |
stdlib: Make list::find do what the docs say it does.
Talked on #rust about this change, got approval from graydon and brson. Will bring up tomorrow at meeting to verify.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/list.rs | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/src/libstd/list.rs b/src/libstd/list.rs index 9f4254f20c4..a33ba0b647a 100644 --- a/src/libstd/list.rs +++ b/src/libstd/list.rs @@ -40,13 +40,13 @@ Apply function `f` to each element of `v`, 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. "] -fn find<T: copy, U: copy>(ls: list<T>, f: fn(T) -> option<U>) - -> option<U> { +fn find<T: copy>(ls: list<T>, f: fn(T) -> bool) -> option<T> { let ls = ls; loop { alt ls { cons(hd, tl) { - alt f(hd) { none { ls = *tl; } some(rs) { ret some(rs); } } + if f(hd) { ret some(hd); } + ls = *tl; } nil { ret none; } } @@ -195,16 +195,14 @@ mod tests { #[test] fn test_find_success() { - fn match(&&i: int) -> option<int> { - ret if i == 2 { option::some(i) } else { option::none::<int> }; - } + fn match(&&i: int) -> bool { ret i == 2; } let l = from_vec([0, 1, 2]); assert (list::find(l, match) == option::some(2)); } #[test] fn test_find_fail() { - fn match(&&_i: int) -> option<int> { ret option::none::<int>; } + fn match(&&_i: int) -> bool { ret false; } let l = from_vec([0, 1, 2]); let empty = list::nil::<int>; assert (list::find(l, match) == option::none::<int>); |
