Skip to content

Commit 1ad5f7d

Browse files
committed
make list based on boxes
1 parent 239cf80 commit 1ad5f7d

File tree

1 file changed

+39
-52
lines changed

1 file changed

+39
-52
lines changed

src/libstd/list.rs

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ enum list<T> {
1010
}
1111

1212
#[doc = "Create a list from a vector"]
13-
fn from_vec<T: copy>(v: [const T]) -> list<T> {
14-
*vec::foldr(v, @nil::<T>, { |h, t| @cons(h, t) })
13+
fn from_vec<T: copy>(v: [const T]) -> @list<T> {
14+
@vec::foldr(v, @nil::<T>, { |h, t| @cons(h, t) })
1515
}
1616

1717
#[doc = "
@@ -27,7 +27,7 @@ accumulated result.
2727
* z - The initial value
2828
* f - The function to apply
2929
"]
30-
fn foldl<T: copy, U>(z: T, ls: list<U>, f: fn(T, U) -> T) -> T {
30+
fn foldl<T: copy, U>(z: T, ls: @list<U>, f: fn(T, U) -> T) -> T {
3131
let mut accum: T = z;
3232
iter(ls) {|elt| accum = f(accum, elt);}
3333
accum
@@ -40,105 +40,92 @@ Apply function `f` to each element of `v`, starting from the first.
4040
When function `f` returns true then an option containing the element
4141
is returned. If `f` matches no elements then none is returned.
4242
"]
43-
fn find<T: copy>(ls: list<T>, f: fn(T) -> bool) -> option<T> {
43+
fn find<T: copy>(ls: @list<T>, f: fn(T) -> bool) -> option<T> {
4444
let mut ls = ls;
4545
loop {
4646
ls = alt ls {
4747
cons(hd, tl) {
4848
if f(hd) { ret some(hd); }
49-
*tl
49+
tl
5050
}
5151
nil { ret none; }
5252
}
5353
};
5454
}
5555

5656
#[doc = "Returns true if a list contains an element with the given value"]
57-
fn has<T: copy>(ls: list<T>, elt: T) -> bool {
57+
fn has<T: copy>(ls: @list<T>, elt: T) -> bool {
5858
for each(ls) { |e|
5959
if e == elt { ret true; }
6060
}
6161
ret false;
6262
}
6363

6464
#[doc = "Returns true if the list is empty"]
65-
pure fn is_empty<T: copy>(ls: list<T>) -> bool {
66-
alt ls {
65+
pure fn is_empty<T: copy>(ls: @list<T>) -> bool {
66+
alt *ls {
6767
nil { true }
6868
_ { false }
6969
}
7070
}
7171

7272
#[doc = "Returns true if the list is not empty"]
73-
pure fn is_not_empty<T: copy>(ls: list<T>) -> bool {
73+
pure fn is_not_empty<T: copy>(ls: @list<T>) -> bool {
7474
ret !is_empty(ls);
7575
}
7676

7777
#[doc = "Returns the length of a list"]
78-
fn len<T>(ls: list<T>) -> uint {
78+
fn len<T>(ls: @list<T>) -> uint {
7979
let mut count = 0u;
8080
iter(ls) {|_e| count += 1u;}
8181
count
8282
}
8383

8484
#[doc = "Returns all but the first element of a list"]
85-
pure fn tail<T: copy>(ls: list<T>) -> list<T> {
86-
alt ls {
87-
cons(_, tl) { ret *tl; }
85+
pure fn tail<T: copy>(ls: @list<T>) -> list<T> {
86+
alt *ls {
87+
cons(_, tl) { ret tl; }
8888
nil { fail "list empty" }
8989
}
9090
}
9191

9292
#[doc = "Returns the first element of a list"]
93-
pure fn head<T: copy>(ls: list<T>) -> T {
94-
alt check ls { cons(hd, _) { hd } }
93+
pure fn head<T: copy>(ls: @list<T>) -> T {
94+
alt check *ls { cons(hd, _) { hd } }
9595
}
9696

9797
#[doc = "Appends one list to another"]
98-
pure fn append<T: copy>(l: list<T>, m: list<T>) -> list<T> {
99-
alt l {
98+
pure fn append<T: copy>(l: @list<T>, m: @list<T>) -> @list<T> {
99+
alt *l {
100100
nil { ret m; }
101-
cons(x, xs) { let rest = append(*xs, m); ret cons(x, @rest); }
101+
cons(x, xs) { let rest = append(*xs, m); ret @cons(x, @rest); }
102102
}
103103
}
104104

105105
#[doc = "Iterate over a list"]
106-
fn iter<T>(l: list<T>, f: fn(T)) {
107-
alt l {
108-
cons(hd, tl) {
109-
f(hd);
110-
let mut cur = tl;
111-
loop {
112-
alt *cur {
113-
cons(hd, tl) {
114-
f(hd);
115-
cur = tl;
116-
}
117-
nil { break; }
118-
}
106+
fn iter<T>(l: @list<T>, f: fn(T)) {
107+
let mut cur = l;
108+
loop {
109+
cur = alt *cur {
110+
cons(hd, tl) {
111+
f(hd);
112+
tl
113+
}
114+
nil { break; }
119115
}
120-
}
121-
nil {}
122116
}
123117
}
124118

125119
#[doc = "Iterate over a list"]
126120
fn each<T>(l: list<T>, f: fn(T) -> bool) {
127-
alt l {
128-
cons(hd, tl) {
129-
if !f(hd) { ret; }
130-
let mut cur = tl;
131-
loop {
132-
alt *cur {
133-
cons(hd, tl) {
134-
if !f(hd) { ret; }
135-
cur = tl;
136-
}
137-
nil { break; }
138-
}
121+
let mut cur = l;
122+
loop {
123+
cur = alt *cur {
124+
cons(hd, tl) {
125+
if !f(hd) { ret; }
126+
}
127+
nil { break; }
139128
}
140-
}
141-
nil {}
142129
}
143130
}
144131

@@ -147,7 +134,7 @@ mod tests {
147134

148135
#[test]
149136
fn test_is_empty() {
150-
let empty : list::list<int> = from_vec([]);
137+
let empty : @list::list<int> = from_vec([]);
151138
let full1 = from_vec([1]);
152139
let full2 = from_vec(['r', 'u']);
153140

@@ -175,7 +162,7 @@ mod tests {
175162

176163
#[test]
177164
fn test_from_vec_empty() {
178-
let empty : list::list<int> = from_vec([]);
165+
let empty : @list::list<int> = from_vec([]);
179166
assert (empty == list::nil::<int>);
180167
}
181168

@@ -196,7 +183,7 @@ mod tests {
196183
fn test_foldl() {
197184
fn add(&&a: uint, &&b: int) -> uint { ret a + (b as uint); }
198185
let l = from_vec([0, 1, 2, 3, 4]);
199-
let empty = list::nil::<int>;
186+
let empty = @list::nil::<int>;
200187
assert (list::foldl(0u, l, add) == 10u);
201188
assert (list::foldl(0u, empty, add) == 0u);
202189
}
@@ -229,7 +216,7 @@ mod tests {
229216
#[test]
230217
fn test_has() {
231218
let l = from_vec([5, 8, 6]);
232-
let empty = list::nil::<int>;
219+
let empty = @list::nil::<int>;
233220
assert (list::has(l, 5));
234221
assert (!list::has(l, 7));
235222
assert (list::has(l, 8));
@@ -239,7 +226,7 @@ mod tests {
239226
#[test]
240227
fn test_len() {
241228
let l = from_vec([0, 1, 2]);
242-
let empty = list::nil::<int>;
229+
let empty = @list::nil::<int>;
243230
assert (list::len(l) == 3u);
244231
assert (list::len(empty) == 0u);
245232
}

0 commit comments

Comments
 (0)