diff --git a/src/libstd/list.rs b/src/libstd/list.rs index 310962cb32c..5e99f86297c 100644 --- a/src/libstd/list.rs +++ b/src/libstd/list.rs @@ -10,8 +10,8 @@ enum list { } #[doc = "Create a list from a vector"] -fn from_vec(v: [const T]) -> list { - *vec::foldr(v, @nil::, { |h, t| @cons(h, t) }) +fn from_vec(v: [const T]) -> @list { + @vec::foldr(v, @nil::, { |h, t| @cons(h, t) }) } #[doc = " @@ -27,7 +27,7 @@ accumulated result. * z - The initial value * f - The function to apply "] -fn foldl(z: T, ls: list, f: fn(T, U) -> T) -> T { +fn foldl(z: T, ls: @list, f: fn(T, U) -> T) -> T { let mut accum: T = z; iter(ls) {|elt| accum = f(accum, elt);} accum @@ -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(ls: list, f: fn(T) -> bool) -> option { +fn find(ls: @list, f: fn(T) -> bool) -> option { let mut ls = ls; loop { ls = alt ls { cons(hd, tl) { if f(hd) { ret some(hd); } - *tl + tl } nil { ret none; } } @@ -54,7 +54,7 @@ fn find(ls: list, f: fn(T) -> bool) -> option { } #[doc = "Returns true if a list contains an element with the given value"] -fn has(ls: list, elt: T) -> bool { +fn has(ls: @list, elt: T) -> bool { for each(ls) { |e| if e == elt { ret true; } } @@ -62,83 +62,70 @@ fn has(ls: list, elt: T) -> bool { } #[doc = "Returns true if the list is empty"] -pure fn is_empty(ls: list) -> bool { - alt ls { +pure fn is_empty(ls: @list) -> bool { + alt *ls { nil { true } _ { false } } } #[doc = "Returns true if the list is not empty"] -pure fn is_not_empty(ls: list) -> bool { +pure fn is_not_empty(ls: @list) -> bool { ret !is_empty(ls); } #[doc = "Returns the length of a list"] -fn len(ls: list) -> uint { +fn len(ls: @list) -> uint { let mut count = 0u; iter(ls) {|_e| count += 1u;} count } #[doc = "Returns all but the first element of a list"] -pure fn tail(ls: list) -> list { - alt ls { - cons(_, tl) { ret *tl; } +pure fn tail(ls: @list) -> list { + alt *ls { + cons(_, tl) { ret tl; } nil { fail "list empty" } } } #[doc = "Returns the first element of a list"] -pure fn head(ls: list) -> T { - alt check ls { cons(hd, _) { hd } } +pure fn head(ls: @list) -> T { + alt check *ls { cons(hd, _) { hd } } } #[doc = "Appends one list to another"] -pure fn append(l: list, m: list) -> list { - alt l { +pure fn append(l: @list, m: @list) -> @list { + alt *l { nil { ret m; } - cons(x, xs) { let rest = append(*xs, m); ret cons(x, @rest); } + cons(x, xs) { let rest = append(*xs, m); ret @cons(x, @rest); } } } #[doc = "Iterate over a list"] -fn iter(l: list, f: fn(T)) { - alt l { - cons(hd, tl) { - f(hd); - let mut cur = tl; - loop { - alt *cur { - cons(hd, tl) { - f(hd); - cur = tl; - } - nil { break; } - } +fn iter(l: @list, f: fn(T)) { + let mut cur = l; + loop { + cur = alt *cur { + cons(hd, tl) { + f(hd); + tl + } + nil { break; } } - } - nil {} } } #[doc = "Iterate over a list"] fn each(l: list, f: fn(T) -> bool) { - alt l { - cons(hd, tl) { - if !f(hd) { ret; } - let mut cur = tl; - loop { - alt *cur { - cons(hd, tl) { - if !f(hd) { ret; } - cur = tl; - } - nil { break; } - } + let mut cur = l; + loop { + cur = alt *cur { + cons(hd, tl) { + if !f(hd) { ret; } + } + nil { break; } } - } - nil {} } } @@ -147,7 +134,7 @@ mod tests { #[test] fn test_is_empty() { - let empty : list::list = from_vec([]); + let empty : @list::list = from_vec([]); let full1 = from_vec([1]); let full2 = from_vec(['r', 'u']); @@ -175,7 +162,7 @@ mod tests { #[test] fn test_from_vec_empty() { - let empty : list::list = from_vec([]); + let empty : @list::list = from_vec([]); assert (empty == list::nil::); } @@ -196,7 +183,7 @@ mod tests { fn test_foldl() { fn add(&&a: uint, &&b: int) -> uint { ret a + (b as uint); } let l = from_vec([0, 1, 2, 3, 4]); - let empty = list::nil::; + let empty = @list::nil::; assert (list::foldl(0u, l, add) == 10u); assert (list::foldl(0u, empty, add) == 0u); } @@ -229,7 +216,7 @@ mod tests { #[test] fn test_has() { let l = from_vec([5, 8, 6]); - let empty = list::nil::; + let empty = @list::nil::; assert (list::has(l, 5)); assert (!list::has(l, 7)); assert (list::has(l, 8)); @@ -239,7 +226,7 @@ mod tests { #[test] fn test_len() { let l = from_vec([0, 1, 2]); - let empty = list::nil::; + let empty = @list::nil::; assert (list::len(l) == 3u); assert (list::len(empty) == 0u); }