stdlib: Add list::from_vec

This commit is contained in:
Brian Anderson 2011-05-22 00:11:28 -04:00
parent 1e9aef828c
commit b4c9f782e4
2 changed files with 27 additions and 0 deletions

View file

@ -10,6 +10,17 @@ tag list[T] {
nil;
}
fn from_vec[T](vec[T] v) -> list[T] {
auto l = nil[T];
// FIXME: This would be faster and more space efficient if it looped over
// a reverse vector iterator. Unfortunately generic iterators seem not to
// work yet.
for (T item in vec::reversed(v)) {
l = cons[T](item, @l);
}
ret l;
}
fn foldl[T,U](&list[T] ls, &U u, fn(&T t, &U u) -> U f) -> U {
alt(ls) {
case (cons[T](?hd, ?tl)) {

View file

@ -0,0 +1,16 @@
use std;
import std::list;
import std::list::car;
import std::list::cdr;
import std::list::from_vec;
fn test_from_vec() {
auto l = from_vec([0, 1, 2]);
assert (car(l) == 0);
assert (car(cdr(l)) == 1);
assert (car(cdr(cdr(l))) == 2);
}
fn main() {
test_from_vec();
}