Skip to content

Commit 2b36276

Browse files
committed
Modified list::from_vec() to return List<T>
1 parent 8846970 commit 2b36276

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

src/libcollections/list.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ pub enum List<T> {
1717
Nil,
1818
}
1919

20-
/// Create a list from a vector
21-
pub fn from_vec<T:Clone + 'static>(v: &[T]) -> @List<T> {
22-
v.rev_iter().fold(@Nil::<T>, |t, h| @Cons((*h).clone(), t))
23-
}
24-
2520
/**
2621
* Left fold
2722
*
@@ -133,6 +128,16 @@ pub fn append<T:Clone + 'static>(list: @List<T>, other: @List<T>) -> @List<T> {
133128
}
134129
}
135130

131+
impl<T:'static + Clone> List<T> {
132+
/// Create a list from a vector
133+
pub fn from_vec(v: &[T]) -> List<T> {
134+
match v.len() {
135+
0 => Nil,
136+
_ => v.rev_iter().fold(Nil, |tail, value: &T| Cons(value.clone(), @tail))
137+
}
138+
}
139+
}
140+
136141
/*
137142
/// Push one element into the front of a list, returning a new list
138143
/// THIS VERSION DOESN'T ACTUALLY WORK
@@ -171,16 +176,16 @@ pub fn each<T>(list: @List<T>, f: |&T| -> bool) -> bool {
171176

172177
#[cfg(test)]
173178
mod tests {
174-
use list::{List, Nil, from_vec, head, is_empty, tail};
179+
use list::{List, Nil, head, is_empty, tail};
175180
use list;
176181

177182
use std::option;
178183

179184
#[test]
180185
fn test_is_empty() {
181-
let empty : @list::List<int> = from_vec([]);
182-
let full1 = from_vec([1]);
183-
let full2 = from_vec(['r', 'u']);
186+
let empty : @list::List<int> = @List::from_vec([]);
187+
let full1 = @List::from_vec([1]);
188+
let full2 = @List::from_vec(['r', 'u']);
184189

185190
assert!(is_empty(empty));
186191
assert!(!is_empty(full1));
@@ -189,7 +194,7 @@ mod tests {
189194

190195
#[test]
191196
fn test_from_vec() {
192-
let list = from_vec([0, 1, 2]);
197+
let list = @List::from_vec([0, 1, 2]);
193198

194199
assert_eq!(head(list), 0);
195200

@@ -202,14 +207,14 @@ mod tests {
202207

203208
#[test]
204209
fn test_from_vec_empty() {
205-
let empty : @list::List<int> = from_vec([]);
206-
assert_eq!(empty, @list::Nil::<int>);
210+
let empty : list::List<int> = List::from_vec([]);
211+
assert_eq!(empty, Nil::<int>);
207212
}
208213

209214
#[test]
210215
fn test_foldl() {
211216
fn add(a: &uint, b: &int) -> uint { return *a + (*b as uint); }
212-
let list = from_vec([0, 1, 2, 3, 4]);
217+
let list = @List::from_vec([0, 1, 2, 3, 4]);
213218
let empty = @list::Nil::<int>;
214219
assert_eq!(list::foldl(0u, list, add), 10u);
215220
assert_eq!(list::foldl(0u, empty, add), 0u);
@@ -220,21 +225,21 @@ mod tests {
220225
fn sub(a: &int, b: &int) -> int {
221226
*a - *b
222227
}
223-
let list = from_vec([1, 2, 3, 4]);
228+
let list = @List::from_vec([1, 2, 3, 4]);
224229
assert_eq!(list::foldl(0, list, sub), -10);
225230
}
226231

227232
#[test]
228233
fn test_find_success() {
229234
fn match_(i: &int) -> bool { return *i == 2; }
230-
let list = from_vec([0, 1, 2]);
235+
let list = @List::from_vec([0, 1, 2]);
231236
assert_eq!(list::find(list, match_), option::Some(2));
232237
}
233238

234239
#[test]
235240
fn test_find_fail() {
236241
fn match_(_i: &int) -> bool { return false; }
237-
let list = from_vec([0, 1, 2]);
242+
let list = @List::from_vec([0, 1, 2]);
238243
let empty = @list::Nil::<int>;
239244
assert_eq!(list::find(list, match_), option::None::<int>);
240245
assert_eq!(list::find(empty, match_), option::None::<int>);
@@ -243,15 +248,15 @@ mod tests {
243248
#[test]
244249
fn test_any() {
245250
fn match_(i: &int) -> bool { return *i == 2; }
246-
let list = from_vec([0, 1, 2]);
251+
let list = @List::from_vec([0, 1, 2]);
247252
let empty = @list::Nil::<int>;
248253
assert_eq!(list::any(list, match_), true);
249254
assert_eq!(list::any(empty, match_), false);
250255
}
251256

252257
#[test]
253258
fn test_has() {
254-
let list = from_vec([5, 8, 6]);
259+
let list = @List::from_vec([5, 8, 6]);
255260
let empty = @list::Nil::<int>;
256261
assert!((list::has(list, 5)));
257262
assert!((!list::has(list, 7)));
@@ -261,15 +266,15 @@ mod tests {
261266

262267
#[test]
263268
fn test_len() {
264-
let list = from_vec([0, 1, 2]);
269+
let list = @List::from_vec([0, 1, 2]);
265270
let empty = @list::Nil::<int>;
266271
assert_eq!(list::len(list), 3u);
267272
assert_eq!(list::len(empty), 0u);
268273
}
269274

270275
#[test]
271276
fn test_append() {
272-
assert!(from_vec([1,2,3,4])
273-
== list::append(list::from_vec([1,2]), list::from_vec([3,4])));
277+
assert!(@List::from_vec([1,2,3,4])
278+
== list::append(@List::from_vec([1,2]), @List::from_vec([3,4])));
274279
}
275280
}

src/test/run-pass/log-knows-the-names-of-variants-in-std.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// except according to those terms.
1212

1313
extern crate collections;
14-
use collections::list;
14+
use collections::list::List;
1515

1616
#[deriving(Clone)]
1717
enum foo {
@@ -24,8 +24,8 @@ fn check_log<T>(exp: ~str, v: T) {
2424
}
2525

2626
pub fn main() {
27-
let x = list::from_vec([a(22u), b(~"hi")]);
28-
let exp = ~"@Cons(a(22u), @Cons(b(~\"hi\"), @Nil))";
27+
let x = List::from_vec([a(22u), b(~"hi")]);
28+
let exp = ~"Cons(a(22u), @Cons(b(~\"hi\"), @Nil))";
2929
let act = format!("{:?}", x);
3030
assert!(act == exp);
3131
check_log(exp, x);

0 commit comments

Comments
 (0)