Skip to content

Commit 7c1696b

Browse files
committed
auto merge of #6057 : cmr/rust/map_zip, r=graydon
I think the name is more clear, and fits with filter_map etc.
2 parents b67a7b9 + d53e686 commit 7c1696b

File tree

8 files changed

+19
-16
lines changed

8 files changed

+19
-16
lines changed

src/libcore/result.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ pub fn map_vec2<S,T,U:Copy,V:Copy>(ss: &[S], ts: &[T],
358358
359359
/**
360360
* Applies op to the pairwise elements from `ss` and `ts`, aborting on
361-
* error. This could be implemented using `map2()` but it is more efficient
361+
* error. This could be implemented using `map_zip()` but it is more efficient
362362
* on its own as no result vector is built.
363363
*/
364364
#[inline(always)]

src/libcore/tuple.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl<'self,A:Copy,B:Copy> ExtendedTupleOps<A,B> for (&'self [A], &'self [B]) {
123123
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C] {
124124
match *self {
125125
(ref a, ref b) => {
126-
vec::map2(*a, *b, f)
126+
vec::map_zip(*a, *b, f)
127127
}
128128
}
129129
}
@@ -144,7 +144,7 @@ impl<A:Copy,B:Copy> ExtendedTupleOps<A,B> for (~[A], ~[B]) {
144144
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C] {
145145
match *self {
146146
(ref a, ref b) => {
147-
vec::map2(*a, *b, f)
147+
vec::map_zip(*a, *b, f)
148148
}
149149
}
150150
}

src/libcore/vec.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -848,8 +848,11 @@ pub fn flat_map<T, U>(v: &[T], f: &fn(t: &T) -> ~[U]) -> ~[U] {
848848
result
849849
}
850850
851-
/// Apply a function to each pair of elements and return the results
852-
pub fn map2<T:Copy,U:Copy,V>(v0: &[T], v1: &[U],
851+
/**
852+
* Apply a function to each pair of elements and return the results.
853+
* Equivalent to `map(zip(v0, v1), f)`.
854+
*/
855+
pub fn map_zip<T:Copy,U:Copy,V>(v0: &[T], v1: &[U],
853856
f: &fn(t: &T, v: &U) -> V) -> ~[V] {
854857
let v0_len = len(v0);
855858
if v0_len != len(v1) { fail!(); }
@@ -3399,12 +3402,12 @@ mod tests {
33993402
}
34003403

34013404
#[test]
3402-
fn test_map2() {
3405+
fn test_map_zip() {
34033406
fn times(x: &int, y: &int) -> int { *x * *y }
34043407
let f = times;
34053408
let v0 = ~[1, 2, 3, 4, 5];
34063409
let v1 = ~[5, 4, 3, 2, 1];
3407-
let u = map2::<int, int, int>(v0, v1, f);
3410+
let u = map_zip::<int, int, int>(v0, v1, f);
34083411
let mut i = 0;
34093412
while i < 5 { assert!(v0[i] * v1[i] == u[i]); i += 1; }
34103413
}
@@ -4338,10 +4341,10 @@ mod tests {
43384341
#[ignore(windows)]
43394342
#[should_fail]
43404343
#[allow(non_implicitly_copyable_typarams)]
4341-
fn test_map2_fail() {
4344+
fn test_map_zip_fail() {
43424345
let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
43434346
let mut i = 0;
4344-
do map2(v, v) |_elt1, _elt2| {
4347+
do map_zip(v, v) |_elt1, _elt2| {
43454348
if i == 2 {
43464349
fail!()
43474350
}

src/librustc/middle/trans/monomorphize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ pub fn make_mono_id(ccx: @CrateContext,
356356
Some(vts) => {
357357
let item_ty = ty::lookup_item_type(ccx.tcx, item);
358358
let mut i = 0;
359-
vec::map2(*item_ty.generics.type_param_defs, substs, |type_param_def, subst| {
359+
vec::map_zip(*item_ty.generics.type_param_defs, substs, |type_param_def, subst| {
360360
let mut v = ~[];
361361
for type_param_def.bounds.each |bound| {
362362
match *bound {
@@ -376,7 +376,7 @@ pub fn make_mono_id(ccx: @CrateContext,
376376
};
377377
let param_ids = match param_uses {
378378
Some(ref uses) => {
379-
vec::map2(precise_param_ids, **uses, |id, uses| {
379+
vec::map_zip(precise_param_ids, **uses, |id, uses| {
380380
if ccx.sess.no_monomorphic_collapse() {
381381
match copy *id {
382382
(a, b) => mono_precise(a, b)

src/librustdoc/attr_pass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ fn merge_method_attrs(
209209
}
210210
};
211211
212-
do vec::map2(docs, attrs) |doc, attrs| {
212+
do vec::map_zip(docs, attrs) |doc, attrs| {
213213
assert!(doc.name == attrs.first());
214214
let desc = attrs.second();
215215

src/libsyntax/ext/deriving/generic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ impl<'self> MethodDef<'self> {
629629
}
630630
}
631631
let field_tuples =
632-
do vec::map2(*self_vec,
632+
do vec::map_zip(*self_vec,
633633
enum_matching_fields) |&(id, self_f), &other| {
634634
(id, self_f, other)
635635
};

src/libsyntax/ext/pipes/pipec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl gen_send for message {
5757
assert!(next_state.tys.len() ==
5858
next.generics.ty_params.len());
5959
let arg_names = tys.mapi(|i, _ty| cx.ident_of(~"x_"+i.to_str()));
60-
let args_ast = vec::map2(arg_names, *tys, |n, t| cx.arg(*n, *t));
60+
let args_ast = vec::map_zip(arg_names, *tys, |n, t| cx.arg(*n, *t));
6161

6262
let pipe_ty = cx.ty_path_ast_builder(
6363
path(~[this.data_name()], span)
@@ -135,7 +135,7 @@ impl gen_send for message {
135135
debug!("pipec: no next state");
136136
let arg_names = tys.mapi(|i, _ty| (~"x_" + i.to_str()));
137137
138-
let args_ast = do vec::map2(arg_names, *tys) |n, t| {
138+
let args_ast = do vec::map_zip(arg_names, *tys) |n, t| {
139139
cx.arg(cx.ident_of(*n), *t)
140140
};
141141

src/test/run-pass/block-vec-map2.rs renamed to src/test/run-pass/block-vec-map_zip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extern mod std;
1212

1313
pub fn main() {
1414
let v =
15-
vec::map2(~[1, 2, 3, 4, 5],
15+
vec::map_zip(~[1, 2, 3, 4, 5],
1616
~[true, false, false, true, true],
1717
|i, b| if *b { -(*i) } else { *i } );
1818
error!(v.clone());

0 commit comments

Comments
 (0)