提交 5b2451c2 编写于 作者: R Roy Frostig

Address _vec.map allocation FIXME. Add test.

上级 5796ebb1
......@@ -76,14 +76,10 @@ fn grow[T](&mutable vec[T] v, int n, &T initval) {
}
}
fn map[T,U](&op[T,U] f, &vec[T] v) -> vec[U] {
// FIXME: should be
// let vec[U] u = alloc[U](len[T](v));
// but this does not work presently.
let vec[U] u = vec();
fn map[T, U](&op[T,U] f, &vec[T] v) -> vec[U] {
let vec[U] u = alloc[U](len[T](v));
for (T ve in v) {
u += vec(f(ve));
}
ret u;
}
......@@ -30,6 +30,18 @@ fn test_slice() {
check (v2.(1) == 4);
}
fn test_map() {
fn square(&int x) -> int { ret x * x; }
let std.util.operator[int, int] op = square;
let vec[int] v = vec(1, 2, 3, 4, 5);
let vec[int] s = std._vec.map[int, int](op, v);
let int i = 0;
while (i < 5) {
check (v.(i) == s.(i));
i += 1;
}
}
fn main() {
test_init_elt();
//XFAIL: test_init_fn(); // Segfaults.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册