/**
* Applies op to the pairwise elements from `ss` and `ts`, aborting on
- * error. This could be implemented using `map2()` but it is more efficient
+ * error. This could be implemented using `map_zip()` but it is more efficient
* on its own as no result vector is built.
*/
#[inline(always)]
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C] {
match *self {
(ref a, ref b) => {
- vec::map2(*a, *b, f)
+ vec::map_zip(*a, *b, f)
}
}
}
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C] {
match *self {
(ref a, ref b) => {
- vec::map2(*a, *b, f)
+ vec::map_zip(*a, *b, f)
}
}
}
result
}
-/// Apply a function to each pair of elements and return the results
-pub fn map2<T:Copy,U:Copy,V>(v0: &[T], v1: &[U],
+/**
+ * Apply a function to each pair of elements and return the results.
+ * Equivalent to `map(zip(v0, v1), f)`.
+ */
+pub fn map_zip<T:Copy,U:Copy,V>(v0: &[T], v1: &[U],
f: &fn(t: &T, v: &U) -> V) -> ~[V] {
let v0_len = len(v0);
if v0_len != len(v1) { fail!(); }
}
#[test]
- fn test_map2() {
+ fn test_map_zip() {
fn times(x: &int, y: &int) -> int { *x * *y }
let f = times;
let v0 = ~[1, 2, 3, 4, 5];
let v1 = ~[5, 4, 3, 2, 1];
- let u = map2::<int, int, int>(v0, v1, f);
+ let u = map_zip::<int, int, int>(v0, v1, f);
let mut i = 0;
while i < 5 { assert!(v0[i] * v1[i] == u[i]); i += 1; }
}
#[ignore(windows)]
#[should_fail]
#[allow(non_implicitly_copyable_typarams)]
- fn test_map2_fail() {
+ fn test_map_zip_fail() {
let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
let mut i = 0;
- do map2(v, v) |_elt1, _elt2| {
+ do map_zip(v, v) |_elt1, _elt2| {
if i == 2 {
fail!()
}
Some(vts) => {
let item_ty = ty::lookup_item_type(ccx.tcx, item);
let mut i = 0;
- vec::map2(*item_ty.generics.type_param_defs, substs, |type_param_def, subst| {
+ vec::map_zip(*item_ty.generics.type_param_defs, substs, |type_param_def, subst| {
let mut v = ~[];
for type_param_def.bounds.each |bound| {
match *bound {
};
let param_ids = match param_uses {
Some(ref uses) => {
- vec::map2(precise_param_ids, **uses, |id, uses| {
+ vec::map_zip(precise_param_ids, **uses, |id, uses| {
if ccx.sess.no_monomorphic_collapse() {
match copy *id {
(a, b) => mono_precise(a, b)
}
};
- do vec::map2(docs, attrs) |doc, attrs| {
+ do vec::map_zip(docs, attrs) |doc, attrs| {
assert!(doc.name == attrs.first());
let desc = attrs.second();
}
}
let field_tuples =
- do vec::map2(*self_vec,
+ do vec::map_zip(*self_vec,
enum_matching_fields) |&(id, self_f), &other| {
(id, self_f, other)
};
assert!(next_state.tys.len() ==
next.generics.ty_params.len());
let arg_names = tys.mapi(|i, _ty| cx.ident_of(~"x_"+i.to_str()));
- let args_ast = vec::map2(arg_names, *tys, |n, t| cx.arg(*n, *t));
+ let args_ast = vec::map_zip(arg_names, *tys, |n, t| cx.arg(*n, *t));
let pipe_ty = cx.ty_path_ast_builder(
path(~[this.data_name()], span)
debug!("pipec: no next state");
let arg_names = tys.mapi(|i, _ty| (~"x_" + i.to_str()));
- let args_ast = do vec::map2(arg_names, *tys) |n, t| {
+ let args_ast = do vec::map_zip(arg_names, *tys) |n, t| {
cx.arg(cx.ident_of(*n), *t)
};
+++ /dev/null
-// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-extern mod std;
-
-pub fn main() {
- let v =
- vec::map2(~[1, 2, 3, 4, 5],
- ~[true, false, false, true, true],
- |i, b| if *b { -(*i) } else { *i } );
- error!(v.clone());
- assert!((v == ~[-1, 2, 3, -4, -5]));
-}
--- /dev/null
+// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+extern mod std;
+
+pub fn main() {
+ let v =
+ vec::map_zip(~[1, 2, 3, 4, 5],
+ ~[true, false, false, true, true],
+ |i, b| if *b { -(*i) } else { *i } );
+ error!(v.clone());
+ assert!((v == ~[-1, 2, 3, -4, -5]));
+}