mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-26 08:44:35 +00:00
auto merge of #6057 : cmr/rust/map_zip, r=graydon
I think the name is more clear, and fits with filter_map etc.
This commit is contained in:
commit
7c1696b529
@ -358,7 +358,7 @@ pub fn map_vec2<S,T,U:Copy,V:Copy>(ss: &[S], ts: &[T],
|
||||
|
||||
/**
|
||||
* 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)]
|
||||
|
@ -123,7 +123,7 @@ impl<'self,A:Copy,B:Copy> ExtendedTupleOps<A,B> for (&'self [A], &'self [B]) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -144,7 +144,7 @@ impl<A:Copy,B:Copy> ExtendedTupleOps<A,B> for (~[A], ~[B]) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -848,8 +848,11 @@ pub fn flat_map<T, U>(v: &[T], f: &fn(t: &T) -> ~[U]) -> ~[U] {
|
||||
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!(); }
|
||||
@ -3399,12 +3402,12 @@ mod tests {
|
||||
}
|
||||
|
||||
#[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; }
|
||||
}
|
||||
@ -4338,10 +4341,10 @@ mod tests {
|
||||
#[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!()
|
||||
}
|
||||
|
@ -356,7 +356,7 @@ pub fn make_mono_id(ccx: @CrateContext,
|
||||
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 {
|
||||
@ -376,7 +376,7 @@ pub fn make_mono_id(ccx: @CrateContext,
|
||||
};
|
||||
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)
|
||||
|
@ -209,7 +209,7 @@ fn merge_method_attrs(
|
||||
}
|
||||
};
|
||||
|
||||
do vec::map2(docs, attrs) |doc, attrs| {
|
||||
do vec::map_zip(docs, attrs) |doc, attrs| {
|
||||
assert!(doc.name == attrs.first());
|
||||
let desc = attrs.second();
|
||||
|
||||
|
@ -629,7 +629,7 @@ impl<'self> MethodDef<'self> {
|
||||
}
|
||||
}
|
||||
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)
|
||||
};
|
||||
|
@ -57,7 +57,7 @@ impl gen_send for message {
|
||||
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)
|
||||
@ -135,7 +135,7 @@ impl gen_send for message {
|
||||
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)
|
||||
};
|
||||
|
||||
|
@ -12,7 +12,7 @@ extern mod std;
|
||||
|
||||
pub fn main() {
|
||||
let v =
|
||||
vec::map2(~[1, 2, 3, 4, 5],
|
||||
vec::map_zip(~[1, 2, 3, 4, 5],
|
||||
~[true, false, false, true, true],
|
||||
|i, b| if *b { -(*i) } else { *i } );
|
||||
error!(v.clone());
|
Loading…
Reference in New Issue
Block a user