Repository: rust-ndarray/ndarray
Pull request #907 (2021-02-03 01:16:27)

Rename .visit() to .for_each()
This is more consistent with `std::iter::Iterator` and `ndarray::Zip`.

See also #894, which is similar.

By the way, what do you think about renaming the existing (private) `.unordered_foreach_mut()` method to `.for_each_mut()` and making it public? (I'd also add an explicit lifetime parameter, like the one in `.visit()`, to make it more versatile.)


Patch:
diff --git a/src/doc/ndarray_for_numpy_users/mod.rs b/src/doc/ndarray_for_numpy_users/mod.rs
index fff72ce2..45ef3ed0 100644
--- a/src/doc/ndarray_for_numpy_users/mod.rs
+++ b/src/doc/ndarray_for_numpy_users/mod.rs
@@ -282,7 +282,7 @@
 //! Note that [`.mapv()`][.mapv()] has corresponding methods [`.map()`][.map()],
 //! [`.mapv_into()`][.mapv_into()], [`.map_inplace()`][.map_inplace()], and
 //! [`.mapv_inplace()`][.mapv_inplace()]. Also look at [`.fold()`][.fold()],
-//! [`.visit()`][.visit()], [`.fold_axis()`][.fold_axis()], and
+//! [`.for_each()`][.for_each()], [`.fold_axis()`][.fold_axis()], and
 //! [`.map_axis()`][.map_axis()].
 //!
 //! <table>
@@ -648,7 +648,7 @@
 //! [.sum_axis()]: ../../struct.ArrayBase.html#method.sum_axis
 //! [.t()]: ../../struct.ArrayBase.html#method.t
 //! [vec-* dot]: ../../struct.ArrayBase.html#method.dot
-//! [.visit()]: ../../struct.ArrayBase.html#method.visit
+//! [.for_each()]: ../../struct.ArrayBase.html#method.for_each
 //! [::zeros()]: ../../struct.ArrayBase.html#method.zeros
 //! [Zip]: ../../struct.Zip.html
 
diff --git a/src/impl_methods.rs b/src/impl_methods.rs
index f8c0ee91..214d454f 100644
--- a/src/impl_methods.rs
+++ b/src/impl_methods.rs
@@ -2220,17 +2220,30 @@ where
         self.unordered_foreach_mut(move |x| *x = f(x.clone()));
     }
 
+    /// Call `f` for each element in the array.
+    ///
+    /// Elements are visited in arbitrary order.
+    pub fn for_each<'a, F>(&'a self, mut f: F)
+    where
+        F: FnMut(&'a A),
+        A: 'a,
+        S: Data,
+    {
+        self.fold((), move |(), elt| f(elt))
+    }
+
     /// Visit each element in the array by calling `f` by reference
     /// on each element.
     ///
     /// Elements are visited in arbitrary order.
-    pub fn visit<'a, F>(&'a self, mut f: F)
+    #[deprecated(note="Renamed to .for_each()", since="0.15.0")]
+    pub fn visit<'a, F>(&'a self, f: F)
     where
         F: FnMut(&'a A),
         A: 'a,
         S: Data,
     {
-        self.fold((), move |(), elt| f(elt))
+        self.for_each(f)
     }
 
     /// Fold along an axis.
diff --git a/src/numeric/impl_numeric.rs b/src/numeric/impl_numeric.rs
index 9d1cef7e..fbec8041 100644
--- a/src/numeric/impl_numeric.rs
+++ b/src/numeric/impl_numeric.rs
@@ -168,7 +168,7 @@ where
         let mut mean = A::zero();
         let mut sum_sq = A::zero();
         let mut i = 0;
-        self.visit(|&x| {
+        self.for_each(|&x| {
             let count = A::from_usize(i + 1).expect("Converting index to `A` must not fail.");
             let delta = x - mean;
             mean = mean + delta / count;


Test patch:
diff --git a/tests/array.rs b/tests/array.rs
index 6581e572..dddcd5e1 100644
--- a/tests/array.rs
+++ b/tests/array.rs
@@ -925,7 +925,7 @@ fn zero_axes() {
     }
     a.map(|_| panic!());
     a.map_inplace(|_| panic!());
-    a.visit(|_| panic!());
+    a.for_each(|_| panic!());
     println!("{:?}", a);
     let b = arr2::<f32, _>(&[[], [], [], []]);
     println!("{:?}\n{:?}", b.shape(), b);