Implement a map2() function in std._vec
This commit is contained in:
parent
89946609f2
commit
3e482d5f69
2 changed files with 36 additions and 1 deletions
|
|
@ -1,6 +1,8 @@
|
|||
import vbuf = rustrt.vbuf;
|
||||
import std.option;
|
||||
|
||||
type operator2[T,U,V] = fn(&T, &U) -> V;
|
||||
|
||||
native "rust" mod rustrt {
|
||||
type vbuf;
|
||||
|
||||
|
|
@ -142,6 +144,22 @@ fn map[T, U](&option.operator[T,U] f, &vec[T] v) -> vec[U] {
|
|||
ret u;
|
||||
}
|
||||
|
||||
fn map2[T,U,V](&operator2[T,U,V] f, &vec[T] v0, &vec[U] v1) -> vec[V] {
|
||||
auto v0_len = len[T](v0);
|
||||
if (v0_len != len[U](v1)) {
|
||||
fail;
|
||||
}
|
||||
|
||||
let vec[V] u = alloc[V](v0_len);
|
||||
auto i = 0u;
|
||||
while (i < v0_len) {
|
||||
u += f(v0.(i), v1.(i));
|
||||
i += 1u;
|
||||
}
|
||||
|
||||
ret u;
|
||||
}
|
||||
|
||||
// Local Variables:
|
||||
// mode: rust;
|
||||
// fill-column: 78;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue