From f4dfa50a582476503839aadda0accd5f306620a4 Mon Sep 17 00:00:00 2001 From: bluss Date: Sat, 5 Dec 2020 12:29:57 +0100 Subject: [PATCH 1/2] API: Rename Zip::apply to Zip::for_each and same for par_apply Rename apply to for_each, par_apply to par_for_each. --- examples/sort-axis.rs | 2 +- examples/zip_many.rs | 4 +-- src/impl_methods.rs | 4 +-- src/linalg/impl_linalg.rs | 4 +-- src/parallel/impl_par_methods.rs | 15 +++++++++- src/parallel/zipmacro.rs | 4 +-- src/zip/mod.rs | 47 +++++++++++++++++++------------- src/zip/zipmacro.rs | 8 +++--- tests/array-construct.rs | 4 +-- tests/azip.rs | 26 +++++++++--------- tests/iterators.rs | 12 ++++---- tests/par_zip.rs | 10 +++---- tests/raw_views.rs | 2 +- tests/windows.rs | 2 +- 14 files changed, 83 insertions(+), 61 deletions(-) diff --git a/examples/sort-axis.rs b/examples/sort-axis.rs index ab5dfdab0..a02dad603 100644 --- a/examples/sort-axis.rs +++ b/examples/sort-axis.rs @@ -112,7 +112,7 @@ where let perm_i = perm.indices[i]; Zip::from(result.index_axis_mut(axis, perm_i)) .and(self.index_axis(axis, i)) - .apply(|to, from| { + .for_each(|to, from| { copy_nonoverlapping(from, to.as_mut_ptr(), 1); moved_elements += 1; }); diff --git a/examples/zip_many.rs b/examples/zip_many.rs index 0059cd650..40b855df2 100644 --- a/examples/zip_many.rs +++ b/examples/zip_many.rs @@ -41,11 +41,11 @@ fn main() { // Let's imagine we split to parallelize { let (x, y) = Zip::indexed(&mut a).split(); - x.apply(|(_, j), elt| { + x.for_each(|(_, j), elt| { *elt = elt.powi(j as i32); }); - y.apply(|(_, j), elt| { + y.for_each(|(_, j), elt| { *elt = elt.powi(j as i32); }); } diff --git a/src/impl_methods.rs b/src/impl_methods.rs index aade25cc6..10918a571 100644 --- a/src/impl_methods.rs +++ b/src/impl_methods.rs @@ -1985,7 +1985,7 @@ where let dim = self.raw_dim(); Zip::from(LanesMut::new(self.view_mut(), Axis(n - 1))) .and(Lanes::new(rhs.broadcast_assume(dim), Axis(n - 1))) - .apply(move |s_row, r_row| Zip::from(s_row).and(r_row).apply(|a, b| f(a, b))); + .for_each(move |s_row, r_row| Zip::from(s_row).and(r_row).for_each(|a, b| f(a, b))); } fn zip_mut_with_elem(&mut self, rhs_elem: &B, mut f: F) @@ -2343,7 +2343,7 @@ where prev.slice_axis_inplace(axis, Slice::from(..-1)); curr.slice_axis_inplace(axis, Slice::from(1..)); // This implementation relies on `Zip` iterating along `axis` in order. - Zip::from(prev).and(curr).apply(|prev, curr| unsafe { + Zip::from(prev).and(curr).for_each(|prev, curr| unsafe { // These pointer dereferences and borrows are safe because: // // 1. They're pointers to elements in the array. diff --git a/src/linalg/impl_linalg.rs b/src/linalg/impl_linalg.rs index c227a68aa..0b559d9c5 100644 --- a/src/linalg/impl_linalg.rs +++ b/src/linalg/impl_linalg.rs @@ -679,11 +679,11 @@ unsafe fn general_mat_vec_mul_impl( if beta.is_zero() { // when beta is zero, c may be uninitialized - Zip::from(a.outer_iter()).and(y).apply(|row, elt| { + Zip::from(a.outer_iter()).and(y).for_each(|row, elt| { elt.write(row.dot(x) * alpha); }); } else { - Zip::from(a.outer_iter()).and(y).apply(|row, elt| { + Zip::from(a.outer_iter()).and(y).for_each(|row, elt| { *elt = *elt * beta + row.dot(x) * alpha; }); } diff --git a/src/parallel/impl_par_methods.rs b/src/parallel/impl_par_methods.rs index a4efa560f..21303736d 100644 --- a/src/parallel/impl_par_methods.rs +++ b/src/parallel/impl_par_methods.rs @@ -59,12 +59,25 @@ macro_rules! zip_impl { D: Dimension, $($p: NdProducer ,)* { + /// The `par_for_each` method for `Zip`. + /// + /// This is a shorthand for using `.into_par_iter().for_each()` on + /// `Zip`. + /// + /// Requires crate feature `rayon`. + pub fn par_for_each(self, function: F) + where F: Fn($($p::Item),*) + Sync + Send + { + self.into_par_iter().for_each(move |($($p,)*)| function($($p),*)) + } + /// The `par_apply` method for `Zip`. /// /// This is a shorthand for using `.into_par_iter().for_each()` on /// `Zip`. /// /// Requires crate feature `rayon`. + #[deprecated(note="Renamed to .par_for_each()", since="0.15.0")] pub fn par_apply(self, function: F) where F: Fn($($p::Item),*) + Sync + Send { @@ -133,7 +146,7 @@ macro_rules! zip_impl { Q::Output: Send, { self.and(into) - .par_apply(move |$($p, )* output_| { + .par_for_each(move |$($p, )* output_| { output_.assign_elem(f($($p ),*)); }); } diff --git a/src/parallel/zipmacro.rs b/src/parallel/zipmacro.rs index eadd36a84..95111c933 100644 --- a/src/parallel/zipmacro.rs +++ b/src/parallel/zipmacro.rs @@ -24,7 +24,7 @@ /// Is equivalent to: /// /// ```rust,ignore -/// Zip::from(&mut a).and(&b).and(&c).par_apply(|a, &b, &c| { +/// Zip::from(&mut a).and(&b).and(&c).par_for_each(|a, &b, &c| { /// *a = b + c; /// }); /// ``` @@ -54,6 +54,6 @@ /// ``` macro_rules! par_azip { ($($t:tt)*) => { - $crate::azip!(@build par_apply $($t)*) + $crate::azip!(@build par_for_each $($t)*) }; } diff --git a/src/zip/mod.rs b/src/zip/mod.rs index 3c61d8110..e6121d99b 100644 --- a/src/zip/mod.rs +++ b/src/zip/mod.rs @@ -518,7 +518,7 @@ impl NdProducer for RawArrayViewMut { /// The order elements are visited is not specified. The producers don’t have to /// have the same item type. /// -/// The `Zip` has two methods for function application: `apply` and +/// The `Zip` has two methods for function application: `for_each` and /// `fold_while`. The zip object can be split, which allows parallelization. /// A read-only zip object (no mutable producers) can be cloned. /// @@ -546,7 +546,7 @@ impl NdProducer for RawArrayViewMut { /// .and(&b) /// .and(&c) /// .and(&d) -/// .apply(|w, &x, &y, &z| { +/// .for_each(|w, &x, &y, &z| { /// *w += x + y * z; /// }); /// @@ -563,7 +563,7 @@ impl NdProducer for RawArrayViewMut { /// /// Zip::from(&mut totals) /// .and(a.rows()) -/// .apply(|totals, row| *totals = row.sum()); +/// .for_each(|totals, row| *totals = row.sum()); /// /// // Check the result against the built in `.sum_axis()` along axis 1. /// assert_eq!(totals, a.sum_axis(Axis(1))); @@ -692,7 +692,7 @@ impl Zip where D: Dimension, { - fn apply_core(&mut self, acc: Acc, mut function: F) -> FoldWhile + fn for_each_core(&mut self, acc: Acc, mut function: F) -> FoldWhile where F: FnMut(Acc, P::Item) -> FoldWhile, P: ZippableTuple, @@ -700,13 +700,13 @@ where if self.dimension.ndim() == 0 { function(acc, unsafe { self.parts.as_ref(self.parts.as_ptr()) }) } else if self.layout.is(CORDER | FORDER) { - self.apply_core_contiguous(acc, function) + self.for_each_core_contiguous(acc, function) } else { - self.apply_core_strided(acc, function) + self.for_each_core_strided(acc, function) } } - fn apply_core_contiguous(&mut self, acc: Acc, mut function: F) -> FoldWhile + fn for_each_core_contiguous(&mut self, acc: Acc, mut function: F) -> FoldWhile where F: FnMut(Acc, P::Item) -> FoldWhile, P: ZippableTuple, @@ -744,7 +744,7 @@ where } - fn apply_core_strided(&mut self, acc: Acc, function: F) -> FoldWhile + fn for_each_core_strided(&mut self, acc: Acc, function: F) -> FoldWhile where F: FnMut(Acc, P::Item) -> FoldWhile, P: ZippableTuple, @@ -754,14 +754,14 @@ where panic!("Unreachable: ndim == 0 is contiguous") } if n == 1 || self.layout_tendency >= 0 { - self.apply_core_strided_c(acc, function) + self.for_each_core_strided_c(acc, function) } else { - self.apply_core_strided_f(acc, function) + self.for_each_core_strided_f(acc, function) } } // Non-contiguous but preference for C - unroll over Axis(ndim - 1) - fn apply_core_strided_c(&mut self, mut acc: Acc, mut function: F) -> FoldWhile + fn for_each_core_strided_c(&mut self, mut acc: Acc, mut function: F) -> FoldWhile where F: FnMut(Acc, P::Item) -> FoldWhile, P: ZippableTuple, @@ -785,7 +785,7 @@ where } // Non-contiguous but preference for F - unroll over Axis(0) - fn apply_core_strided_f(&mut self, mut acc: Acc, mut function: F) -> FoldWhile + fn for_each_core_strided_f(&mut self, mut acc: Acc, mut function: F) -> FoldWhile where F: FnMut(Acc, P::Item) -> FoldWhile, P: ZippableTuple, @@ -939,15 +939,24 @@ macro_rules! map_impl { { /// Apply a function to all elements of the input arrays, /// visiting elements in lock step. - pub fn apply(mut self, mut function: F) + pub fn for_each(mut self, mut function: F) where F: FnMut($($p::Item),*) { - self.apply_core((), move |(), args| { + self.for_each_core((), move |(), args| { let ($($p,)*) = args; FoldWhile::Continue(function($($p),*)) }); } + /// Apply a function to all elements of the input arrays, + /// visiting elements in lock step. + #[deprecated(note="Renamed to .for_each()", since="0.15.0")] + pub fn apply(self, function: F) + where F: FnMut($($p::Item),*) + { + self.for_each(function) + } + /// Apply a fold function to all elements of the input arrays, /// visiting elements in lock step. /// @@ -980,7 +989,7 @@ macro_rules! map_impl { where F: FnMut(Acc, $($p::Item),*) -> Acc, { - self.apply_core(acc, move |acc, args| { + self.for_each_core(acc, move |acc, args| { let ($($p,)*) = args; FoldWhile::Continue(function(acc, $($p),*)) }).into_inner() @@ -993,7 +1002,7 @@ macro_rules! map_impl { -> FoldWhile where F: FnMut(Acc, $($p::Item),*) -> FoldWhile { - self.apply_core(acc, move |acc, args| { + self.for_each_core(acc, move |acc, args| { let ($($p,)*) = args; function(acc, $($p),*) }) @@ -1015,7 +1024,7 @@ macro_rules! map_impl { pub fn all(mut self, mut predicate: F) -> bool where F: FnMut($($p::Item),*) -> bool { - !self.apply_core((), move |_, args| { + !self.for_each_core((), move |_, args| { let ($($p,)*) = args; if predicate($($p),*) { FoldWhile::Continue(()) @@ -1096,7 +1105,7 @@ macro_rules! map_impl { Q::Item: AssignElem { self.and(into) - .apply(move |$($p, )* output_| { + .for_each(move |$($p, )* output_| { output_.assign_elem(f($($p ),*)); }); } @@ -1150,7 +1159,7 @@ macro_rules! map_impl { // Apply the mapping function on this zip // if we panic with unwinding; Partial will drop the written elements. let partial_len = &mut partial.len; - self.apply(move |$($p,)* output_elem: *mut R| { + self.for_each(move |$($p,)* output_elem: *mut R| { output_elem.write(f($($p),*)); if std::mem::needs_drop::() { *partial_len += 1; diff --git a/src/zip/zipmacro.rs b/src/zip/zipmacro.rs index 5353a6378..f25e33d31 100644 --- a/src/zip/zipmacro.rs +++ b/src/zip/zipmacro.rs @@ -12,7 +12,7 @@ /// Is equivalent to: /// /// ```rust,ignore -/// Zip::from(&mut a).and(&b).and(&c).apply(|a, &b, &c| { +/// Zip::from(&mut a).and(&b).and(&c).for_each(|a, &b, &c| { /// *a = b + c /// }); /// ``` @@ -27,8 +27,8 @@ /// /// The *expr* are expressions whose types must implement `IntoNdProducer`, the /// *pat* are the patterns of the parameters to the closure called by -/// `Zip::apply`, and *body_expr* is the body of the closure called by -/// `Zip::apply`. You can think of each *pat* `in` *expr* as being analogous to +/// `Zip::for_each`, and *body_expr* is the body of the closure called by +/// `Zip::for_each`. You can think of each *pat* `in` *expr* as being analogous to /// the `pat in expr` of a normal loop `for pat in expr { statements }`: a /// pattern, followed by `in`, followed by an expression that implements /// `IntoNdProducer` (analogous to `IntoIterator` for a `for` loop). @@ -129,6 +129,6 @@ macro_rules! azip { // catch-all rule (@build $($t:tt)*) => { compile_error!("Invalid syntax in azip!()") }; ($($t:tt)*) => { - $crate::azip!(@build apply $($t)*) + $crate::azip!(@build for_each $($t)*) }; } diff --git a/tests/array-construct.rs b/tests/array-construct.rs index 4513dd453..2c195480f 100644 --- a/tests/array-construct.rs +++ b/tests/array-construct.rs @@ -231,12 +231,12 @@ fn maybe_uninit_1() { let mut a = Mat::maybe_uninit((10, 10)); let v = a.raw_view_mut(); Zip::from(v) - .apply(|ptr| *(*ptr).as_mut_ptr() = 1.); + .for_each(|ptr| *(*ptr).as_mut_ptr() = 1.); let u = a.raw_view_mut().assume_init(); Zip::from(u) - .apply(|ptr| assert_eq!(*ptr, 1.)); + .for_each(|ptr| assert_eq!(*ptr, 1.)); } } diff --git a/tests/azip.rs b/tests/azip.rs index b553d7fb5..34328169a 100644 --- a/tests/azip.rs +++ b/tests/azip.rs @@ -243,7 +243,7 @@ fn test_broadcast() { .and_broadcast(&b) .and_broadcast(&d) .and_broadcast(&e); - z.apply(|x, &y, &z, &w| *x = y + z + w); + z.for_each(|x, &y, &z, &w| *x = y + z + w); } let sum = &b + &d + &e; assert_abs_diff_eq!(a, sum.broadcast((n, n)).unwrap(), epsilon = 1e-4); @@ -293,11 +293,11 @@ fn test_clone() { let z = Zip::from(&a).and(a.exact_chunks((1, 1, 1))); let w = z.clone(); let mut result = Vec::new(); - z.apply(|x, y| { + z.for_each(|x, y| { result.push((x, y)); }); let mut i = 0; - w.apply(|x, y| { + w.for_each(|x, y| { assert_eq!(result[i], (x, y)); i += 1; }); @@ -324,7 +324,7 @@ fn test_indices_1() { } let mut count = 0; - Zip::indexed(&a1).apply(|i, elt| { + Zip::indexed(&a1).for_each(|i, elt| { count += 1; assert_eq!(*elt, i); }); @@ -334,12 +334,12 @@ fn test_indices_1() { let len = a1.len(); let (x, y) = Zip::indexed(&mut a1).split(); - x.apply(|i, elt| { + x.for_each(|i, elt| { count += 1; assert_eq!(*elt, i); }); assert_eq!(count, len / 2); - y.apply(|i, elt| { + y.for_each(|i, elt| { count += 1; assert_eq!(*elt, i); }); @@ -364,12 +364,12 @@ fn test_indices_2() { let len = a1.len(); let (x, y) = Zip::indexed(&mut a1).split(); - x.apply(|i, elt| { + x.for_each(|i, elt| { count += 1; assert_eq!(*elt, i); }); assert_eq!(count, len / 2); - y.apply(|i, elt| { + y.for_each(|i, elt| { count += 1; assert_eq!(*elt, i); }); @@ -384,7 +384,7 @@ fn test_indices_3() { } let mut count = 0; - Zip::indexed(&a1).apply(|i, elt| { + Zip::indexed(&a1).for_each(|i, elt| { count += 1; assert_eq!(*elt, i); }); @@ -394,12 +394,12 @@ fn test_indices_3() { let len = a1.len(); let (x, y) = Zip::indexed(&mut a1).split(); - x.apply(|i, elt| { + x.for_each(|i, elt| { count += 1; assert_eq!(*elt, i); }); assert_eq!(count, len / 2); - y.apply(|i, elt| { + y.for_each(|i, elt| { count += 1; assert_eq!(*elt, i); }); @@ -418,12 +418,12 @@ fn test_indices_split_1() { let mut seen = Vec::new(); let mut ac = 0; - a.apply(|i, _| { + a.for_each(|i, _| { ac += 1; seen.push(i); }); let mut bc = 0; - b.apply(|i, _| { + b.for_each(|i, _| { bc += 1; seen.push(i); }); diff --git a/tests/iterators.rs b/tests/iterators.rs index 7bb856adf..fc345200e 100644 --- a/tests/iterators.rs +++ b/tests/iterators.rs @@ -308,7 +308,7 @@ fn axis_iter_zip() { let a = Array::from_iter(0..5); let iter = a.axis_iter(Axis(0)); let mut b = Array::zeros(5); - Zip::from(&mut b).and(iter).apply(|b, a| *b = a[()]); + Zip::from(&mut b).and(iter).for_each(|b, a| *b = a[()]); assert_eq!(a, b); } @@ -320,7 +320,7 @@ fn axis_iter_zip_partially_consumed() { while iter.next().is_some() { consumed += 1; let mut b = Array::zeros(a.len() - consumed); - Zip::from(&mut b).and(iter.clone()).apply(|b, a| *b = a[()]); + Zip::from(&mut b).and(iter.clone()).for_each(|b, a| *b = a[()]); assert_eq!(a.slice(s![consumed..]), b); } } @@ -334,7 +334,7 @@ fn axis_iter_zip_partially_consumed_discontiguous() { consumed += 1; let mut b = Array::zeros((a.len() - consumed) * 2); b.slice_collapse(s![..;2]); - Zip::from(&mut b).and(iter.clone()).apply(|b, a| *b = a[()]); + Zip::from(&mut b).and(iter.clone()).for_each(|b, a| *b = a[()]); assert_eq!(a.slice(s![consumed..]), b); } } @@ -487,7 +487,7 @@ fn axis_iter_mut_zip() { let mut cloned = orig.clone(); let iter = cloned.axis_iter_mut(Axis(0)); let mut b = Array::zeros(5); - Zip::from(&mut b).and(iter).apply(|b, mut a| { + Zip::from(&mut b).and(iter).for_each(|b, mut a| { a[()] += 1; *b = a[()]; }); @@ -505,7 +505,7 @@ fn axis_iter_mut_zip_partially_consumed() { iter.next(); } let mut b = Array::zeros(remaining); - Zip::from(&mut b).and(iter).apply(|b, a| *b = a[()]); + Zip::from(&mut b).and(iter).for_each(|b, a| *b = a[()]); assert_eq!(a.slice(s![consumed..]), b); } } @@ -521,7 +521,7 @@ fn axis_iter_mut_zip_partially_consumed_discontiguous() { } let mut b = Array::zeros(remaining * 2); b.slice_collapse(s![..;2]); - Zip::from(&mut b).and(iter).apply(|b, a| *b = a[()]); + Zip::from(&mut b).and(iter).for_each(|b, a| *b = a[()]); assert_eq!(a.slice(s![consumed..]), b); } } diff --git a/tests/par_zip.rs b/tests/par_zip.rs index 36d7f43c2..6e508de15 100644 --- a/tests/par_zip.rs +++ b/tests/par_zip.rs @@ -11,14 +11,14 @@ const N: usize = 100; fn test_zip_1() { let mut a = Array2::::zeros((M, N)); - Zip::from(&mut a).par_apply(|x| *x = x.exp()); + Zip::from(&mut a).par_for_each(|x| *x = x.exp()); } #[test] fn test_zip_index_1() { let mut a = Array2::default((10, 10)); - Zip::indexed(&mut a).par_apply(|i, x| { + Zip::indexed(&mut a).par_for_each(|i, x| { *x = i; }); @@ -31,7 +31,7 @@ fn test_zip_index_1() { fn test_zip_index_2() { let mut a = Array2::default((M, N)); - Zip::indexed(&mut a).par_apply(|i, x| { + Zip::indexed(&mut a).par_for_each(|i, x| { *x = i; }); @@ -44,7 +44,7 @@ fn test_zip_index_2() { fn test_zip_index_3() { let mut a = Array::default((1, 2, 1, 2, 3)); - Zip::indexed(&mut a).par_apply(|i, x| { + Zip::indexed(&mut a).par_for_each(|i, x| { *x = i; }); @@ -58,7 +58,7 @@ fn test_zip_index_4() { let mut a = Array2::zeros((M, N)); let mut b = Array2::zeros((M, N)); - Zip::indexed(&mut a).and(&mut b).par_apply(|(i, j), x, y| { + Zip::indexed(&mut a).and(&mut b).par_for_each(|(i, j), x, y| { *x = i; *y = j; }); diff --git a/tests/raw_views.rs b/tests/raw_views.rs index b955cac6b..bb39547e8 100644 --- a/tests/raw_views.rs +++ b/tests/raw_views.rs @@ -14,7 +14,7 @@ fn raw_view_cast_cell() { let raw_cell_view = a.raw_view_mut().cast::>(); let cell_view = unsafe { raw_cell_view.deref_into_view() }; - Zip::from(cell_view).apply(|elt| elt.set(elt.get() + 1.)); + Zip::from(cell_view).for_each(|elt| elt.set(elt.get() + 1.)); } assert_eq!(a, answer); } diff --git a/tests/windows.rs b/tests/windows.rs index eb1a33411..d1c41f017 100644 --- a/tests/windows.rs +++ b/tests/windows.rs @@ -104,7 +104,7 @@ fn test_window_zip() { for x in 1..4 { for y in 1..4 { for z in 1..4 { - Zip::indexed(a.windows((x, y, z))).apply(|(i, j, k), window| { + Zip::indexed(a.windows((x, y, z))).for_each(|(i, j, k), window| { let x = x as isize; let y = y as isize; let z = z as isize; From 1f5b8d5ab81b167fb8d31da74ce3f2617356365c Mon Sep 17 00:00:00 2001 From: bluss Date: Thu, 14 Jan 2021 00:43:42 +0100 Subject: [PATCH 2/2] API: Rename apply_collect, apply_collect_into to map_collect, map_collect_into --- src/parallel/impl_par_methods.rs | 35 ++++++++++++++++++++++++++++---- src/traversal_utils.rs | 2 +- src/zip/mod.rs | 31 +++++++++++++++++++++++----- tests/azip.rs | 14 ++++++------- tests/par_zip.rs | 8 ++++---- tests/views.rs | 2 +- 6 files changed, 70 insertions(+), 22 deletions(-) diff --git a/src/parallel/impl_par_methods.rs b/src/parallel/impl_par_methods.rs index 21303736d..33d393345 100644 --- a/src/parallel/impl_par_methods.rs +++ b/src/parallel/impl_par_methods.rs @@ -86,11 +86,11 @@ macro_rules! zip_impl { expand_if!(@bool [$notlast] - /// Apply and collect the results into a new array, which has the same size as the + /// Map and collect the results into a new array, which has the same size as the /// inputs. /// /// If all inputs are c- or f-order respectively, that is preserved in the output. - pub fn par_apply_collect(self, f: impl Fn($($p::Item,)* ) -> R + Sync + Send) + pub fn par_map_collect(self, f: impl Fn($($p::Item,)* ) -> R + Sync + Send) -> Array where R: Send { @@ -135,12 +135,24 @@ macro_rules! zip_impl { } } - /// Apply and assign the results into the producer `into`, which should have the same + /// Map and collect the results into a new array, which has the same size as the + /// inputs. + /// + /// If all inputs are c- or f-order respectively, that is preserved in the output. + #[deprecated(note="Renamed to .par_map_collect()", since="0.15.0")] + pub fn par_apply_collect(self, f: impl Fn($($p::Item,)* ) -> R + Sync + Send) + -> Array + where R: Send + { + self.par_map_collect(f) + } + + /// Map and assign the results into the producer `into`, which should have the same /// size as the other inputs. /// /// The producer should have assignable items as dictated by the `AssignElem` trait, /// for example `&mut R`. - pub fn par_apply_assign_into(self, into: Q, f: impl Fn($($p::Item,)* ) -> R + Sync + Send) + pub fn par_map_assign_into(self, into: Q, f: impl Fn($($p::Item,)* ) -> R + Sync + Send) where Q: IntoNdProducer, Q::Item: AssignElem + Send, Q::Output: Send, @@ -150,6 +162,21 @@ macro_rules! zip_impl { output_.assign_elem(f($($p ),*)); }); } + + /// Apply and assign the results into the producer `into`, which should have the same + /// size as the other inputs. + /// + /// The producer should have assignable items as dictated by the `AssignElem` trait, + /// for example `&mut R`. + #[deprecated(note="Renamed to .par_map_assign_into()", since="0.15.0")] + pub fn par_apply_assign_into(self, into: Q, f: impl Fn($($p::Item,)* ) -> R + Sync + Send) + where Q: IntoNdProducer, + Q::Item: AssignElem + Send, + Q::Output: Send, + { + self.par_map_assign_into(into, f) + } + ); } )+ diff --git a/src/traversal_utils.rs b/src/traversal_utils.rs index b77d410a2..3f4d017bf 100644 --- a/src/traversal_utils.rs +++ b/src/traversal_utils.rs @@ -21,6 +21,6 @@ pub(crate) fn assign_to<'a, P1, P2, A>(from: P1, to: P2) A: Clone + 'a { Zip::from(from) - .apply_assign_into(to, A::clone); + .map_assign_into(to, A::clone); } diff --git a/src/zip/mod.rs b/src/zip/mod.rs index e6121d99b..dd0bce3f8 100644 --- a/src/zip/mod.rs +++ b/src/zip/mod.rs @@ -1074,12 +1074,11 @@ macro_rules! map_impl { } } - /// Apply and collect the results into a new array, which has the same size as the + /// Map and collect the results into a new array, which has the same size as the /// inputs. /// /// If all inputs are c- or f-order respectively, that is preserved in the output. - pub fn apply_collect(self, f: impl FnMut($($p::Item,)* ) -> R) -> Array - { + pub fn map_collect(self, f: impl FnMut($($p::Item,)* ) -> R) -> Array { // Make uninit result let mut output = self.uninitalized_for_current_layout::(); @@ -1095,12 +1094,21 @@ macro_rules! map_impl { } } - /// Apply and assign the results into the producer `into`, which should have the same + /// Map and collect the results into a new array, which has the same size as the + /// inputs. + /// + /// If all inputs are c- or f-order respectively, that is preserved in the output. + #[deprecated(note="Renamed to .map_collect()", since="0.15.0")] + pub fn apply_collect(self, f: impl FnMut($($p::Item,)* ) -> R) -> Array { + self.map_collect(f) + } + + /// Map and assign the results into the producer `into`, which should have the same /// size as the other inputs. /// /// The producer should have assignable items as dictated by the `AssignElem` trait, /// for example `&mut R`. - pub fn apply_assign_into(self, into: Q, mut f: impl FnMut($($p::Item,)* ) -> R) + pub fn map_assign_into(self, into: Q, mut f: impl FnMut($($p::Item,)* ) -> R) where Q: IntoNdProducer, Q::Item: AssignElem { @@ -1110,6 +1118,19 @@ macro_rules! map_impl { }); } + /// Map and assign the results into the producer `into`, which should have the same + /// size as the other inputs. + /// + /// The producer should have assignable items as dictated by the `AssignElem` trait, + /// for example `&mut R`. + #[deprecated(note="Renamed to .map_assign_into()", since="0.15.0")] + pub fn apply_assign_into(self, into: Q, f: impl FnMut($($p::Item,)* ) -> R) + where Q: IntoNdProducer, + Q::Item: AssignElem + { + self.map_assign_into(into, f) + } + ); diff --git a/tests/azip.rs b/tests/azip.rs index 34328169a..5075a89ba 100644 --- a/tests/azip.rs +++ b/tests/azip.rs @@ -54,13 +54,13 @@ fn test_azip2_3() { fn test_zip_collect() { use approx::assert_abs_diff_eq; - // test Zip::apply_collect and that it preserves c/f layout. + // test Zip::map_collect and that it preserves c/f layout. let b = Array::from_shape_fn((5, 10), |(i, j)| 1. / (i + 2 * j + 1) as f32); let c = Array::from_shape_fn((5, 10), |(i, j)| f32::exp((i + j) as f32)); { - let a = Zip::from(&b).and(&c).apply_collect(|x, y| x + y); + let a = Zip::from(&b).and(&c).map_collect(|x, y| x + y); assert_abs_diff_eq!(a, &b + &c, epsilon = 1e-6); assert_eq!(a.strides(), b.strides()); @@ -70,7 +70,7 @@ fn test_zip_collect() { let b = b.t(); let c = c.t(); - let a = Zip::from(&b).and(&c).apply_collect(|x, y| x + y); + let a = Zip::from(&b).and(&c).map_collect(|x, y| x + y); assert_abs_diff_eq!(a, &b + &c, epsilon = 1e-6); assert_eq!(a.strides(), b.strides()); @@ -86,7 +86,7 @@ fn test_zip_assign_into() { let b = Array::from_shape_fn((5, 10), |(i, j)| 1. / (i + 2 * j + 1) as f32); let c = Array::from_shape_fn((5, 10), |(i, j)| f32::exp((i + j) as f32)); - Zip::from(&b).and(&c).apply_assign_into(&mut a, |x, y| x + y); + Zip::from(&b).and(&c).map_assign_into(&mut a, |x, y| x + y); assert_abs_diff_eq!(a, &b + &c, epsilon = 1e-6); } @@ -101,7 +101,7 @@ fn test_zip_assign_into_cell() { let b = Array::from_shape_fn((5, 10), |(i, j)| 1. / (i + 2 * j + 1) as f32); let c = Array::from_shape_fn((5, 10), |(i, j)| f32::exp((i + j) as f32)); - Zip::from(&b).and(&c).apply_assign_into(&a, |x, y| x + y); + Zip::from(&b).and(&c).map_assign_into(&a, |x, y| x + y); let a2 = a.mapv(|elt| elt.get()); assert_abs_diff_eq!(a2, &b + &c, epsilon = 1e-6); @@ -154,7 +154,7 @@ fn test_zip_collect_drop() { } let _result = panic::catch_unwind(panic::AssertUnwindSafe(|| { - Zip::from(&a).and(&b).apply_collect(|&elt, _| { + Zip::from(&a).and(&b).map_collect(|&elt, _| { if elt.0 > 3 && will_panic { panic!(); } @@ -308,7 +308,7 @@ fn test_indices_0() { let a1 = arr0(3); let mut count = 0; - Zip::indexed(&a1).apply(|i, elt| { + Zip::indexed(&a1).for_each(|i, elt| { count += 1; assert_eq!(i, ()); assert_eq!(*elt, 3); diff --git a/tests/par_zip.rs b/tests/par_zip.rs index 6e508de15..f30d5c7ef 100644 --- a/tests/par_zip.rs +++ b/tests/par_zip.rs @@ -82,7 +82,7 @@ fn test_zip_collect() { let c = Array::from_shape_fn((M, N), |(i, j)| f32::ln((1 + i + j) as f32)); { - let a = Zip::from(&b).and(&c).par_apply_collect(|x, y| x + y); + let a = Zip::from(&b).and(&c).par_map_collect(|x, y| x + y); assert_abs_diff_eq!(a, &b + &c, epsilon = 1e-6); assert_eq!(a.strides(), b.strides()); @@ -92,7 +92,7 @@ fn test_zip_collect() { let b = b.t(); let c = c.t(); - let a = Zip::from(&b).and(&c).par_apply_collect(|x, y| x + y); + let a = Zip::from(&b).and(&c).par_map_collect(|x, y| x + y); assert_abs_diff_eq!(a, &b + &c, epsilon = 1e-6); assert_eq!(a.strides(), b.strides()); @@ -112,7 +112,7 @@ fn test_zip_small_collect() { let c = Array::from_shape_fn(dim, |(i, j)| f32::ln((1 + i + j) as f32)); { - let a = Zip::from(&b).and(&c).par_apply_collect(|x, y| x + y); + let a = Zip::from(&b).and(&c).par_map_collect(|x, y| x + y); assert_abs_diff_eq!(a, &b + &c, epsilon = 1e-6); assert_eq!(a.strides(), b.strides()); @@ -130,7 +130,7 @@ fn test_zip_assign_into() { let b = Array::from_shape_fn((M, N), |(i, j)| 1. / (i + 2 * j + 1) as f32); let c = Array::from_shape_fn((M, N), |(i, j)| f32::ln((1 + i + j) as f32)); - Zip::from(&b).and(&c).par_apply_assign_into(&mut a, |x, y| x + y); + Zip::from(&b).and(&c).par_map_assign_into(&mut a, |x, y| x + y); assert_abs_diff_eq!(a, &b + &c, epsilon = 1e-6); } diff --git a/tests/views.rs b/tests/views.rs index 216e55402..ecef72fe8 100644 --- a/tests/views.rs +++ b/tests/views.rs @@ -10,7 +10,7 @@ fn cell_view() { let cv1 = a.cell_view(); let cv2 = cv1; - Zip::from(cv1).and(cv2).apply(|a, b| a.set(b.get() + 1.)); + Zip::from(cv1).and(cv2).for_each(|a, b| a.set(b.get() + 1.)); } assert_eq!(a, answer); }